Skip to content

perf(ast): read local variables in place when evaluating selects - #3

Merged
vbtcl merged 1 commit into
boson-basefrom
perf/const-eval-select-by-ref
Oct 19, 2026
Merged

vbtcl merged 1 commit into
boson-basefrom
perf/const-eval-select-by-ref

Conversation

@vbtcl

@vbtcl vbtcl commented Oct 16, 2026 •

Copy link
Copy Markdown

ElementSelectExpression, RangeSelectExpression and MemberAccessExpression evaluate their value operand with value().eval(). For a local variable, that returns a copy of the entire variable. Each read of one element of a large local array or packed vector in a constant function copies the whole local. Hsiao ECC matrix generation, for example, reads single bits of a 20,880-bit packed local and single elements of a 22,800-element unpacked local in its inner loops.

When the value operand names a local of the current frame, the select now reads the variable in place, through the new NamedValueExpression::evalLocalRef. This only happens when every selector is side-effect free: literals, names, unary operators other than ++/--, binary operators, conversions, and nested selects of those. Any other selector keeps the copy, so the value is still read before the selector's side effects (a[a[0]++] reads the old a). checkConstant diagnostics are issued exactly as before.

Measured with the boson native-elab driver setup, getAllDiagnostics, 1 thread:

design before this PR with #2 (memo) both
astral 112.3 s 7.8 s 8.6 s 2.2 s
carfield 27.5 s 3.2 s 4.4 s 2.0 s

The rendered diagnostics are byte-identical in every column. The new EvalTests.cpp case covers unpacked, packed and struct selects of locals, a side-effecting selector, and an out-of-bounds read. The full unittests pass: 22082 assertions in 2204 test cases.

Ships to boson as scripts/patches/slang/0011-read-locals-in-place-in-const-eval-selects.patch (partcleda/boson#13187).

🤖 Generated with Claude Code


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

Element, range and member selects evaluated their value operand by copying
it, so every read of one element of a large local array or packed vector
in a constant function copied the whole variable. Hsiao ECC matrix
generation, for example, reads single bits of a 20,880-bit packed local
and single elements of a 22,800-element unpacked local in its inner loops.

When the value operand names a local variable of the current frame and
the selectors can't write to any variable, the select now reads the
variable in place. Selectors with side effects keep the copy, so the
value is still read before the selector's side effects.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vbtcl
vbtcl merged commit 3797b5f into boson-base Oct 19, 2026
12 of 15 checks passed
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