Skip to content

Commit f4951ef

Browse files
author
Fabrice Bellard
committed
optimize the create of arrays - optimized the rest and array_from opcodes
1 parent f1253f2 commit f4951ef

File tree

1 file changed

+60
-59
lines changed

1 file changed

+60
-59
lines changed

quickjs.c

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8914,6 +8914,57 @@ static JSValue js_allocate_fast_array(JSContext *ctx, int64_t len)
89148914
return arr;
89158915
}
89168916

8917+
static JSValue js_create_array(JSContext *ctx, int len, JSValueConst *tab)
8918+
{
8919+
JSValue obj;
8920+
JSObject *p;
8921+
int i;
8922+
8923+
obj = JS_NewArray(ctx);
8924+
if (JS_IsException(obj))
8925+
return JS_EXCEPTION;
8926+
if (len > 0) {
8927+
p = JS_VALUE_GET_OBJ(obj);
8928+
if (expand_fast_array(ctx, p, len) < 0) {
8929+
JS_FreeValue(ctx, obj);
8930+
return JS_EXCEPTION;
8931+
}
8932+
p->u.array.count = len;
8933+
for(i = 0; i < len; i++)
8934+
p->u.array.u.values[i] = JS_DupValue(ctx, tab[i]);
8935+
/* update the 'length' field */
8936+
set_value(ctx, &p->prop[0].u.value, JS_NewInt32(ctx, len));
8937+
}
8938+
return obj;
8939+
}
8940+
8941+
static JSValue js_create_array_free(JSContext *ctx, int len, JSValue *tab)
8942+
{
8943+
JSValue obj;
8944+
JSObject *p;
8945+
int i;
8946+
8947+
obj = JS_NewArray(ctx);
8948+
if (JS_IsException(obj))
8949+
goto fail;
8950+
if (len > 0) {
8951+
p = JS_VALUE_GET_OBJ(obj);
8952+
if (expand_fast_array(ctx, p, len) < 0) {
8953+
JS_FreeValue(ctx, obj);
8954+
fail:
8955+
for(i = 0; i < len; i++)
8956+
JS_FreeValue(ctx, tab[i]);
8957+
return JS_EXCEPTION;
8958+
}
8959+
p->u.array.count = len;
8960+
for(i = 0; i < len; i++)
8961+
p->u.array.u.values[i] = tab[i];
8962+
/* update the 'length' field */
8963+
set_value(ctx, &p->prop[0].u.value, JS_NewInt32(ctx, len));
8964+
}
8965+
return obj;
8966+
}
8967+
89178968
static void js_free_desc(JSContext *ctx, JSPropertyDescriptor *desc)
89188969
{
89198970
JS_FreeValue(ctx, desc->getter);
@@ -15620,26 +15671,6 @@ static JSValue js_build_mapped_arguments(JSContext *ctx, int argc,
1562015671
return JS_EXCEPTION;
1562115672
}
1562215673

15623-
static JSValue js_build_rest(JSContext *ctx, int first, int argc, JSValueConst *argv)
15624-
{
15625-
JSValue val;
15626-
int i, ret;
15627-
15628-
val = JS_NewArray(ctx);
15629-
if (JS_IsException(val))
15630-
return val;
15631-
for (i = first; i < argc; i++) {
15632-
ret = JS_DefinePropertyValueUint32(ctx, val, i - first,
15633-
JS_DupValue(ctx, argv[i]),
15634-
JS_PROP_C_W_E);
15635-
if (ret < 0) {
15636-
JS_FreeValue(ctx, val);
15637-
return JS_EXCEPTION;
15638-
}
15639-
}
15640-
return val;
15641-
}
15642-
1564315674
static JSValue build_for_in_iterator(JSContext *ctx, JSValue obj)
1564415675
{
1564515676
JSObject *p, *p1;
@@ -17122,7 +17153,8 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1712217153
{
1712317154
int first = get_u16(pc);
1712417155
pc += 2;
17125-
*sp++ = js_build_rest(ctx, first, argc, (JSValueConst *)argv);
17156+
first = min_int(first, argc);
17157+
*sp++ = js_create_array(ctx, argc - first, (JSValueConst *)(argv + first));
1712617158
if (unlikely(JS_IsException(sp[-1])))
1712717159
goto exception;
1712817160
}
@@ -17345,27 +17377,13 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1734517377
}
1734617378
BREAK;
1734717379
CASE(OP_array_from):
17348-
{
17349-
int i, ret;
17350-
17351-
call_argc = get_u16(pc);
17352-
pc += 2;
17353-
ret_val = JS_NewArray(ctx);
17354-
if (unlikely(JS_IsException(ret_val)))
17355-
goto exception;
17356-
call_argv = sp - call_argc;
17357-
for(i = 0; i < call_argc; i++) {
17358-
ret = JS_DefinePropertyValue(ctx, ret_val, __JS_AtomFromUInt32(i), call_argv[i],
17359-
JS_PROP_C_W_E | JS_PROP_THROW);
17360-
call_argv[i] = JS_UNDEFINED;
17361-
if (ret < 0) {
17362-
JS_FreeValue(ctx, ret_val);
17363-
goto exception;
17364-
}
17365-
}
17366-
sp -= call_argc;
17367-
*sp++ = ret_val;
17368-
}
17380+
call_argc = get_u16(pc);
17381+
pc += 2;
17382+
ret_val = js_create_array_free(ctx, call_argc, sp - call_argc);
17383+
sp -= call_argc;
17384+
if (unlikely(JS_IsException(ret_val)))
17385+
goto exception;
17386+
*sp++ = ret_val;
1736917387
BREAK;
1737017388

1737117389
CASE(OP_apply):
@@ -42104,23 +42122,6 @@ static void js_array_iterator_mark(JSRuntime *rt, JSValueConst val,
4210442122
}
4210542123
}
4210642124

42107-
static JSValue js_create_array(JSContext *ctx, int len, JSValueConst *tab)
42108-
{
42109-
JSValue obj;
42110-
int i;
42111-
42112-
obj = JS_NewArray(ctx);
42113-
if (JS_IsException(obj))
42114-
return JS_EXCEPTION;
42115-
for(i = 0; i < len; i++) {
42116-
if (JS_CreateDataPropertyUint32(ctx, obj, i, JS_DupValue(ctx, tab[i]), 0) < 0) {
42117-
JS_FreeValue(ctx, obj);
42118-
return JS_EXCEPTION;
42119-
}
42120-
}
42121-
return obj;
42122-
}
42123-
4212442125
static JSValue js_create_array_iterator(JSContext *ctx, JSValueConst this_val,
4212542126
int argc, JSValueConst *argv, int magic)
4212642127
{

0 commit comments

Comments
 (0)