ref: 06a6f9fb110fe02eb5c306a9c6c143a6cd8bd3bb
parent: 4c7f6be4333250cbc544dd758e587342e317e2aa
author: Tor Andersson <[email protected]>
date: Fri Mar 26 08:05:35 EDT 2021
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.
--- a/jscompile.c
+++ b/jscompile.c
@@ -302,17 +302,10 @@
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;
}
}
--- a/jscompile.h
+++ b/jscompile.h
@@ -38,6 +38,7 @@
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> */
--- a/jsrun.c
+++ b/jsrun.c
@@ -1438,6 +1438,10 @@
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);
--- a/opnames.h
+++ b/opnames.h
@@ -25,6 +25,7 @@
"setvar",
"delvar",
"in",
+"initarray",
"initprop",
"initgetter",
"initsetter",