]> git.kaiwu.me - quickjs.git/commitdiff
inlined the get_length operation
authorFabrice Bellard <fabrice@bellard.org>
Thu, 16 Oct 2025 13:10:58 +0000 (15:10 +0200)
committerFabrice Bellard <fabrice@bellard.org>
Thu, 16 Oct 2025 13:10:58 +0000 (15:10 +0200)
quickjs.c
tests/microbench.js

index 0b57b2a4e9168275862b50f6b202fb673f037c4d..c3290da8b672a32d848e0d8f723d72d8222dfa66 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -17132,18 +17132,6 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
         CASE(OP_push_empty_string):
             *sp++ = JS_AtomToString(ctx, JS_ATOM_empty_string);
             BREAK;
-        CASE(OP_get_length):
-            {
-                JSValue val;
-
-                sf->cur_pc = pc;
-                val = JS_GetProperty(ctx, sp[-1], JS_ATOM_length);
-                if (unlikely(JS_IsException(val)))
-                    goto exception;
-                JS_FreeValue(ctx, sp[-1]);
-                sp[-1] = val;
-            }
-            BREAK;
 #endif
         CASE(OP_push_atom_value):
             *sp++ = JS_AtomToValue(ctx, get_u32(pc));
@@ -18317,7 +18305,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
             }
             BREAK;
 
-#define GET_FIELD_INLINE(name, keep)                                    \
+#define GET_FIELD_INLINE(name, keep, is_length)                         \
             {                                                           \
                 JSValue val, obj;                                       \
                 JSAtom atom;                                            \
@@ -18325,8 +18313,12 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
                 JSProperty *pr;                                         \
                 JSShapeProperty *prs;                                   \
                                                                         \
-                atom = get_u32(pc);                                     \
-                pc += 4;                                                \
+                if (is_length) {                                        \
+                    atom = JS_ATOM_length;                              \
+                } else {                                                \
+                    atom = get_u32(pc);                                 \
+                    pc += 4;                                            \
+                }                                                       \
                                                                         \
                 obj = sp[-1];                                           \
                 if (likely(JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT)) {   \
@@ -18370,13 +18362,19 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
 
             
         CASE(OP_get_field):
-            GET_FIELD_INLINE(get_field, 0);
+            GET_FIELD_INLINE(get_field, 0, 0);
             BREAK;
 
         CASE(OP_get_field2):
-            GET_FIELD_INLINE(get_field2, 1);
+            GET_FIELD_INLINE(get_field2, 1, 0);
             BREAK;
 
+#if SHORT_OPCODES
+        CASE(OP_get_length):
+            GET_FIELD_INLINE(get_length, 0, 1);
+            BREAK;
+#endif
+            
         CASE(OP_put_field):
             {
                 int ret;
index 9000f3643b61a5de6208bd735d3c5e952c3a5417..c4710b189e0f9ab70c8249a7bd9cf057caaf31bf 100644 (file)
@@ -425,6 +425,21 @@ function array_slice(n)
     return len * n;
 }
 
+function array_length_read(n)
+{
+    var tab, sum, j;
+    tab = [1, 2, 3];
+    sum = 0;
+    for(j = 0; j < n; j++) {
+        sum += tab.length;
+        sum += tab.length;
+        sum += tab.length;
+        sum += tab.length;
+    }
+    global_res = sum;
+    return n * 4;
+}
+
 function array_length_decr(n)
 {
     var tab, ref, i, j, len;
@@ -929,6 +944,21 @@ function regexp_utf16(n)
     return n * 1000;
 }
 
+function string_length(n)
+{
+    var str, sum, j;
+    str = "abcde";
+    sum = 0;
+    for(j = 0; j < n; j++) {
+        sum += str.length;
+        sum += str.length;
+        sum += str.length;
+        sum += str.length;
+    }
+    global_res = sum;
+    return n * 4;
+}
+
 /* incremental string contruction as local var */
 function string_build1(n)
 {
@@ -1382,6 +1412,7 @@ function main(argc, argv, g)
         array_update,
         array_prop_create,
         array_slice,
+        array_length_read,
         array_length_decr,
         array_hole_length_decr,
         array_push,
@@ -1411,6 +1442,7 @@ function main(argc, argv, g)
         math_min,
         regexp_ascii,
         regexp_utf16,
+        string_length,
         string_build1,
         string_build1x,
         string_build2c,