Skip to content

perf(compiler): elided the TDZ checks of let/const after their declaration - #12

Closed
H4vC wants to merge 1 commit into
stencil-hq:masterfrom
H4vC:perf/lexical-tdz
Closed

H4vC wants to merge 1 commit into
stencil-hq:masterfrom
H4vC:perf/lexical-tdz

Conversation

@H4vC

@H4vC H4vC commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Every reference of a function to its own let, const or class variable was
a get_loc_check/put_loc_check, even long after the declaration. Besides
the test at each access, the peephole optimizations only match get_loc and
put_loc (add_loc, inc_loc, set_loc...), so 'c += 1' on a let ran 48%
slower than on a var (30M iterations: 348 ms, now 172 ms; the 'i++' of a
'for (let i ...)' loop now becomes inc_loc too, so the var loop also went
from 236 to 170 ms).

resolve_variables() reads the code in the order it was emitted. When it
resolves the OP_scope_put_var_init of a declaration in the scope of its
variable, it marks the variable as initialized, and the following
references of the function to it in that scope get get_loc/put_loc. They
can only run after the declaration in the current entry of the scope:
the entry of a scope makes its variables uninitialized again and a loop
jumps back before it, a break or continue leaves the scope, and a
declaration is always a direct statement of its block. Not marked:

  • the body of a switch (new JSVarScope.is_switch): a case label jumps past
    the declarations of the previous cases;
  • 'this' of a derived class constructor, initialized by super() anywhere;
  • the references of closures (get_var_ref_check), which can run before
    the declaration.
    Python functions emit their final opcodes directly and are unchanged.

The two properties of the code generator this relies on are stated at
lexical_var_needs_check() and at the switch statement. CONFIG_CHECK_TDZ=y
builds a qjs whose reads of a local let/const/class variable without
check abort if it is uninitialized (private names, read without check by
design, are left out): test262 runs with it without abort (44/52778
errors, as master), and clearing is_switch makes a case label reading a
declaration of a previous case abort.

test262: same results as master (44/52778 errors, the same 3 host time
zone failures).

Testing

Based on 68be18f (the tip of feat/py), so the same commit applies to master and to feat/py without conflicts, alone or with the other PRs of this series.

  • make test, tests/py/interop/run.sh and a short Python script (string indexing/iteration/concatenation, marshal, list pop/insert/clear/extend) pass with the Python build, on this PR alone and with the whole series, on master and on feat/py (plus perf(arrays): O(1) shift() on fast arrays for feat/py #10 for the array PRs).
  • test262 on master + the series: 44/52778 errors, as on master (the same 3 host time zone failures); the same with CONFIG_CHECK_TDZ=y, without abort.
  • AddressSanitizer build of master + the series: test_language, test_builtin, test_loop, test_closure, test_bigint, test_decorators, test_using and test262 language/ (0/23162 errors).
  • Linux (WSL, gcc 13.3). feat/py itself needs 665e816 (gcc 15 / glibc fixes from master) to build here; it was added for testing only.

Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

…ation

Every reference of a function to its own let, const or class variable was
a get_loc_check/put_loc_check, even long after the declaration. Besides
the test at each access, the peephole optimizations only match get_loc and
put_loc (add_loc, inc_loc, set_loc...), so 'c += 1' on a let ran 48%
slower than on a var (30M iterations: 348 ms, now 172 ms; the 'i++' of a
'for (let i ...)' loop now becomes inc_loc too, so the var loop also went
from 236 to 170 ms).

resolve_variables() reads the code in the order it was emitted. When it
resolves the OP_scope_put_var_init of a declaration in the scope of its
variable, it marks the variable as initialized, and the following
references of the function to it in that scope get get_loc/put_loc. They
can only run after the declaration in the current entry of the scope:
the entry of a scope makes its variables uninitialized again and a loop
jumps back before it, a break or continue leaves the scope, and a
declaration is always a direct statement of its block. Not marked:
- the body of a switch (new JSVarScope.is_switch): a case label jumps past
  the declarations of the previous cases;
- 'this' of a derived class constructor, initialized by super() anywhere;
- the references of closures (get_var_ref_check), which can run before
  the declaration.
Python functions emit their final opcodes directly and are unchanged.

The two properties of the code generator this relies on are stated at
lexical_var_needs_check() and at the switch statement. CONFIG_CHECK_TDZ=y
builds a qjs whose reads of a local let/const/class variable without
check abort if it is uninitialized (private names, read without check by
design, are left out): test262 runs with it without abort (44/52778
errors, as master), and clearing is_switch makes a case label reading a
declaration of a previous case abort.

test262: same results as master (44/52778 errors, the same 3 host time
zone failures).
@H4vC H4vC closed this Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant