optimize the create of arrays - optimized the rest and array_from opcodes

This commit is contained in:
Fabrice Bellard
2025-10-03 12:25:05 +02:00
parent f1253f2ab5
commit f4951efd94

119
quickjs.c
View File

@@ -8914,6 +8914,57 @@ static JSValue js_allocate_fast_array(JSContext *ctx, int64_t len)
return arr; return arr;
} }
static JSValue js_create_array(JSContext *ctx, int len, JSValueConst *tab)
{
JSValue obj;
JSObject *p;
int i;
obj = JS_NewArray(ctx);
if (JS_IsException(obj))
return JS_EXCEPTION;
if (len > 0) {
p = JS_VALUE_GET_OBJ(obj);
if (expand_fast_array(ctx, p, len) < 0) {
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
p->u.array.count = len;
for(i = 0; i < len; i++)
p->u.array.u.values[i] = JS_DupValue(ctx, tab[i]);
/* update the 'length' field */
set_value(ctx, &p->prop[0].u.value, JS_NewInt32(ctx, len));
}
return obj;
}
static JSValue js_create_array_free(JSContext *ctx, int len, JSValue *tab)
{
JSValue obj;
JSObject *p;
int i;
obj = JS_NewArray(ctx);
if (JS_IsException(obj))
goto fail;
if (len > 0) {
p = JS_VALUE_GET_OBJ(obj);
if (expand_fast_array(ctx, p, len) < 0) {
JS_FreeValue(ctx, obj);
fail:
for(i = 0; i < len; i++)
JS_FreeValue(ctx, tab[i]);
return JS_EXCEPTION;
}
p->u.array.count = len;
for(i = 0; i < len; i++)
p->u.array.u.values[i] = tab[i];
/* update the 'length' field */
set_value(ctx, &p->prop[0].u.value, JS_NewInt32(ctx, len));
}
return obj;
}
static void js_free_desc(JSContext *ctx, JSPropertyDescriptor *desc) static void js_free_desc(JSContext *ctx, JSPropertyDescriptor *desc)
{ {
JS_FreeValue(ctx, desc->getter); JS_FreeValue(ctx, desc->getter);
@@ -15620,26 +15671,6 @@ static JSValue js_build_mapped_arguments(JSContext *ctx, int argc,
return JS_EXCEPTION; return JS_EXCEPTION;
} }
static JSValue js_build_rest(JSContext *ctx, int first, int argc, JSValueConst *argv)
{
JSValue val;
int i, ret;
val = JS_NewArray(ctx);
if (JS_IsException(val))
return val;
for (i = first; i < argc; i++) {
ret = JS_DefinePropertyValueUint32(ctx, val, i - first,
JS_DupValue(ctx, argv[i]),
JS_PROP_C_W_E);
if (ret < 0) {
JS_FreeValue(ctx, val);
return JS_EXCEPTION;
}
}
return val;
}
static JSValue build_for_in_iterator(JSContext *ctx, JSValue obj) static JSValue build_for_in_iterator(JSContext *ctx, JSValue obj)
{ {
JSObject *p, *p1; JSObject *p, *p1;
@@ -17122,7 +17153,8 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
{ {
int first = get_u16(pc); int first = get_u16(pc);
pc += 2; pc += 2;
*sp++ = js_build_rest(ctx, first, argc, (JSValueConst *)argv); first = min_int(first, argc);
*sp++ = js_create_array(ctx, argc - first, (JSValueConst *)(argv + first));
if (unlikely(JS_IsException(sp[-1]))) if (unlikely(JS_IsException(sp[-1])))
goto exception; goto exception;
} }
@@ -17345,27 +17377,13 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
} }
BREAK; BREAK;
CASE(OP_array_from): CASE(OP_array_from):
{ call_argc = get_u16(pc);
int i, ret; pc += 2;
ret_val = js_create_array_free(ctx, call_argc, sp - call_argc);
call_argc = get_u16(pc); sp -= call_argc;
pc += 2; if (unlikely(JS_IsException(ret_val)))
ret_val = JS_NewArray(ctx); goto exception;
if (unlikely(JS_IsException(ret_val))) *sp++ = ret_val;
goto exception;
call_argv = sp - call_argc;
for(i = 0; i < call_argc; i++) {
ret = JS_DefinePropertyValue(ctx, ret_val, __JS_AtomFromUInt32(i), call_argv[i],
JS_PROP_C_W_E | JS_PROP_THROW);
call_argv[i] = JS_UNDEFINED;
if (ret < 0) {
JS_FreeValue(ctx, ret_val);
goto exception;
}
}
sp -= call_argc;
*sp++ = ret_val;
}
BREAK; BREAK;
CASE(OP_apply): CASE(OP_apply):
@@ -42104,23 +42122,6 @@ static void js_array_iterator_mark(JSRuntime *rt, JSValueConst val,
} }
} }
static JSValue js_create_array(JSContext *ctx, int len, JSValueConst *tab)
{
JSValue obj;
int i;
obj = JS_NewArray(ctx);
if (JS_IsException(obj))
return JS_EXCEPTION;
for(i = 0; i < len; i++) {
if (JS_CreateDataPropertyUint32(ctx, obj, i, JS_DupValue(ctx, tab[i]), 0) < 0) {
JS_FreeValue(ctx, obj);
return JS_EXCEPTION;
}
}
return obj;
}
static JSValue js_create_array_iterator(JSContext *ctx, JSValueConst this_val, static JSValue js_create_array_iterator(JSContext *ctx, JSValueConst this_val,
int argc, JSValueConst *argv, int magic) int argc, JSValueConst *argv, int magic)
{ {