mirror of
https://github.com/ccxvii/mujs.git
synced 2026-02-05 17:29:43 +08:00
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:
13
jscompile.c
13
jscompile.c
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> */
|
||||
|
||||
4
jsrun.c
4
jsrun.c
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user