Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inefficient bytecode for constructor-less ES6 child classes #752

Closed
bnoordhuis opened this issue Dec 12, 2024 · 4 comments · Fixed by #753
Closed

Inefficient bytecode for constructor-less ES6 child classes #752

bnoordhuis opened this issue Dec 12, 2024 · 4 comments · Fixed by #753
Labels
enhancement New feature or request

Comments

@bnoordhuis
Copy link
Contributor

Refs: #750

Ex.

class Foo extends Bar { /* no construct() */ }

Emits:

        /* super */
        emit_op(s, OP_scope_get_var);
        emit_atom(s, JS_ATOM_this_active_func);
        emit_u16(s, 0);

        emit_op(s, OP_get_super);

        emit_op(s, OP_scope_get_var);
        emit_atom(s, JS_ATOM_new_target);
        emit_u16(s, 0);

        emit_op(s, OP_array_from);
        emit_u16(s, 0);
        emit_op(s, OP_push_i32);
        emit_u32(s, 0);

        /* arguments */
        emit_op(s, OP_scope_get_var);
        emit_atom(s, JS_ATOM_arguments);
        emit_u16(s, 0);

        emit_op(s, OP_append);
        /* drop the index */
        emit_op(s, OP_drop);

        emit_op(s, OP_apply);
        emit_u16(s, 1);
        /* set the 'this' value */
        emit_op(s, OP_dup);
        emit_op(s, OP_scope_put_var_init);
        emit_atom(s, JS_ATOM_this);
        emit_u16(s, 0);
        emit_class_field_init(s);

https://github.com/quickjs-ng/quickjs/blob/4b9d0ebdba3ace6b7f6de21c3e3806ab4055bd7e/quickjs.c#L21898-L21938

Pretty bulky and inefficient (splatting arguments, doing js_function_apply, etc.) Probably better handled through a dedicated opcode.

Affects WBT benchmarks like babylon that create tons of such objects.

@bnoordhuis bnoordhuis added the enhancement New feature or request label Dec 12, 2024
@saghul
Copy link
Contributor

saghul commented Dec 13, 2024

Hum, how come has_super doesn't work there? Then it would skip a bunch of that, wouldn't it?

Also, looks like this was changed in a8a5ecb any chance it was made somehow worse?

@bnoordhuis
Copy link
Contributor Author

how come has_super doesn't work there?

It does - ⬆️ is the bytecode for when has_super == true.

looks like this was changed in a8a5ecb any chance it was made somehow worse?

With 20/20 hindsight it would've probably been better to add and emit a specialized opcode but 🤷, Divy's commit at least improved spec conformance.

@saghul
Copy link
Contributor

saghul commented Dec 13, 2024

Ah hold on. You mean all of that happens even when there is no constructor which calls super?!

@bnoordhuis
Copy link
Contributor Author

Yep, that's right. It essentially synthesizes a constructor because of this:

class A { constructor(x) { print(x) } }
class B extends A { /* no constructor */ }
new B(42) // prints 42

bnoordhuis added a commit to bnoordhuis/quickjs that referenced this issue Dec 13, 2024
We were emitting a gob of inefficient bytecode that created an arguments
array on the stack, then applied it to the parent constructor.

Add a new opcode for initializing a derived class. Speeds up construction
by 500%, although sadly that is not visible in the web-tooling-benchmark,
only in microbenchmarks.

Fixes: quickjs-ng#752
bnoordhuis added a commit to bnoordhuis/quickjs that referenced this issue Jan 5, 2025
We were emitting a gob of inefficient bytecode that created an arguments
array on the stack, then applied it to the parent constructor.

Add a new opcode for initializing a derived class. Speeds up construction
by 500%, although sadly that is not visible in the web-tooling-benchmark,
only in microbenchmarks.

Fixes: quickjs-ng#752
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants