Issue #120: Optimize array construction bytecode.

Use a specialized array initializer that pushes values to the end
of the array instead of using a lot of setprop. This avoids the need
to create a lot of number constants for the array indices.
This commit is contained in:
Tor Andersson
2021-03-26 12:05:35 +01:00
parent 4c7f6be433
commit 06a6f9fb11
4 changed files with 9 additions and 10 deletions

View File

@@ -302,17 +302,10 @@ static void cbinary(JF, js_Ast *exp, int opcode)
static void carray(JF, js_Ast *list)
{
int i = 0;
while (list) {
if (list->a->type != EXP_UNDEF) {
emitline(J, F, list->a);
emitnumber(J, F, i++);
cexp(J, F, list->a);
emitline(J, F, list->a);
emit(J, F, OP_INITPROP);
} else {
++i;
}
emitline(J, F, list->a);
cexp(J, F, list->a);
emit(J, F, OP_INITARRAY);
list = list->b;
}
}

View File

@@ -38,6 +38,7 @@ enum js_OpCode
OP_IN, /* <name> <obj> -- <exists?> */
OP_INITARRAY, /* <obj> <val> -- <obj> */
OP_INITPROP, /* <obj> <key> <val> -- <obj> */
OP_INITGETTER, /* <obj> <key> <closure> -- <obj> */
OP_INITSETTER, /* <obj> <key> <closure> -- <obj> */

View File

@@ -1438,6 +1438,10 @@ static void jsR_run(js_State *J, js_Function *F)
js_pushboolean(J, b);
break;
case OP_INITARRAY:
js_setindex(J, -2, js_getlength(J, -2));
break;
case OP_INITPROP:
obj = js_toobject(J, -3);
str = js_tostring(J, -2);

View File

@@ -25,6 +25,7 @@
"setvar",
"delvar",
"in",
"initarray",
"initprop",
"initgetter",
"initsetter",