Skip to content

Commit 3e5f2bb

Browse files
author
Fabrice Bellard
committed
inlined the get_length operation
1 parent c720e35 commit 3e5f2bb

2 files changed

Lines changed: 47 additions & 17 deletions

File tree

quickjs.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17132,18 +17132,6 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1713217132
CASE(OP_push_empty_string):
1713317133
*sp++ = JS_AtomToString(ctx, JS_ATOM_empty_string);
1713417134
BREAK;
17135-
CASE(OP_get_length):
17136-
{
17137-
JSValue val;
17138-
17139-
sf->cur_pc = pc;
17140-
val = JS_GetProperty(ctx, sp[-1], JS_ATOM_length);
17141-
if (unlikely(JS_IsException(val)))
17142-
goto exception;
17143-
JS_FreeValue(ctx, sp[-1]);
17144-
sp[-1] = val;
17145-
}
17146-
BREAK;
1714717135
#endif
1714817136
CASE(OP_push_atom_value):
1714917137
*sp++ = JS_AtomToValue(ctx, get_u32(pc));
@@ -18317,16 +18305,20 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1831718305
}
1831818306
BREAK;
1831918307

18320-
#define GET_FIELD_INLINE(name, keep) \
18308+
#define GET_FIELD_INLINE(name, keep, is_length) \
1832118309
{ \
1832218310
JSValue val, obj; \
1832318311
JSAtom atom; \
1832418312
JSObject *p; \
1832518313
JSProperty *pr; \
1832618314
JSShapeProperty *prs; \
1832718315
\
18328-
atom = get_u32(pc); \
18329-
pc += 4; \
18316+
if (is_length) { \
18317+
atom = JS_ATOM_length; \
18318+
} else { \
18319+
atom = get_u32(pc); \
18320+
pc += 4; \
18321+
} \
1833018322
\
1833118323
obj = sp[-1]; \
1833218324
if (likely(JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT)) { \
@@ -18370,13 +18362,19 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1837018362

1837118363

1837218364
CASE(OP_get_field):
18373-
GET_FIELD_INLINE(get_field, 0);
18365+
GET_FIELD_INLINE(get_field, 0, 0);
1837418366
BREAK;
1837518367

1837618368
CASE(OP_get_field2):
18377-
GET_FIELD_INLINE(get_field2, 1);
18369+
GET_FIELD_INLINE(get_field2, 1, 0);
1837818370
BREAK;
1837918371

18372+
#if SHORT_OPCODES
18373+
CASE(OP_get_length):
18374+
GET_FIELD_INLINE(get_length, 0, 1);
18375+
BREAK;
18376+
#endif
18377+
1838018378
CASE(OP_put_field):
1838118379
{
1838218380
int ret;

tests/microbench.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,21 @@ function array_slice(n)
425425
return len * n;
426426
}
427427

428+
function array_length_read(n)
429+
{
430+
var tab, sum, j;
431+
tab = [1, 2, 3];
432+
sum = 0;
433+
for(j = 0; j < n; j++) {
434+
sum += tab.length;
435+
sum += tab.length;
436+
sum += tab.length;
437+
sum += tab.length;
438+
}
439+
global_res = sum;
440+
return n * 4;
441+
}
442+
428443
function array_length_decr(n)
429444
{
430445
var tab, ref, i, j, len;
@@ -929,6 +944,21 @@ function regexp_utf16(n)
929944
return n * 1000;
930945
}
931946

947+
function string_length(n)
948+
{
949+
var str, sum, j;
950+
str = "abcde";
951+
sum = 0;
952+
for(j = 0; j < n; j++) {
953+
sum += str.length;
954+
sum += str.length;
955+
sum += str.length;
956+
sum += str.length;
957+
}
958+
global_res = sum;
959+
return n * 4;
960+
}
961+
932962
/* incremental string contruction as local var */
933963
function string_build1(n)
934964
{
@@ -1382,6 +1412,7 @@ function main(argc, argv, g)
13821412
array_update,
13831413
array_prop_create,
13841414
array_slice,
1415+
array_length_read,
13851416
array_length_decr,
13861417
array_hole_length_decr,
13871418
array_push,
@@ -1411,6 +1442,7 @@ function main(argc, argv, g)
14111442
math_min,
14121443
regexp_ascii,
14131444
regexp_utf16,
1445+
string_length,
14141446
string_build1,
14151447
string_build1x,
14161448
string_build2c,

0 commit comments

Comments
 (0)