Leave windows on deep stacks to the table: the search never wins there - #8
Merged
Merged
Conversation
At --effort high the A* search runs on every window. Its cost grows with the
stack the window starts from, but its wins do not. Counting what it found on
real scripts:
field-300.hex 1,391 searches, 15 wins (16 B). Every win started from a
stack of at most 15 items; the 446 searches on stacks of 20+
found nothing and took 40 s of the 43 s spent searching.
a 934 B mainnet contract 280 searches, 3 wins (3 B), all from stacks of at
most 6; the 90 searches on stacks of 20+ took 113 s of 176 s.
So a window whose stack is deeper than searchMaxDepth (16) is now left to the
table, as a window wider than the table already is. Same output, less time:
field-300.hex 2,607 B 45 s -> 8.9 s
934 B contract 694 B 155 s -> 51 s (3 more contracts: 1.0-3.5x)
The search also builds each state's key once, when the state is created,
instead of rebuilding it at every pop, and tests the goal by comparing stacks
rather than strings.
Output is byte for byte what it was, checked by SHA-256 on the Miller loop,
the final exponentiation, field-300.hex, sha256.block, g1.inSubgroup, fp12.mul,
fp12.inv and ec.add.
codenlighten
force-pushed
the
search-depth-cap
branch
from
September 18, 2026 00:12
0e9fbfe to
2915d3e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
--effort highspends nearly all its time in the superoptimizer's A* search, and on real scripts that search almost never pays. Instrumenting where its wins come from:examples/scripts/field-300.hexIn both, the searches starting from a stack of 20 or more items found nothing, while taking 40 s of 43 s and 113 s of 176 s. The search's cost grows with the stack it starts from (every expansion copies it, and
PICK/ROLLbranch over its depth) but its wins do not.Change
A window whose stack is deeper than
searchMaxDepth(default 16) is left to the table, the way a window wider than the table already is. Nothing else about which windows are searched changes, and the limit is an option.The search also builds each state's key once, when the state is created, instead of rebuilding it at each pop, and tests the goal by comparing stacks instead of strings.
Effect
Same bytes, less time:
field-300.hex(--effort high)--effort high)Output is byte for byte what it was, compared by SHA-256: the Miller loop (333,031 → 199,147), the final exponentiation (473,562 → 284,713),
field-300.hex,sha256.block,g1.inSubgroup,fp12.mul,fp12.invandec.add.npm test: 28/28.Why not lower the expansion budget instead
Also measured: at
--effort highwithmaxExpand1,000–1,500 the contract drops to 25 s, but one of the three wins is lost, and onfield-300.hexraising the budget from 1,000 to 5,000 buys nothing at all (2,607 B either way, 26 s more). The depth limit removes the waste without giving up a win, so the budget is left alone.What this came out of
Measuring whether the optimizer leaves bytes on the table inside regions. On mainnet contracts
--effort highfinds zero extra bytes overmedium(the 3 B it found were recovered by other passes anyway) while taking up to 100x as long; on field code it finds 0.5%. Branch-level work looks similarly thin: across 4,071IFblocks in the mainnet contract corpus, code shared between both branches that could be moved out of the block comes to 0.24% of their bytes, and noIFhas a constant condition. What is left in these scripts is algorithmic, like therelaxpass, not more search.