Skip to content

Leave windows on deep stacks to the table: the search never wins there - #8

Merged
codenlighten merged 1 commit into
mainfrom
search-depth-cap
Sep 18, 2026
Merged

codenlighten merged 1 commit into
mainfrom
search-depth-cap

Conversation

@codenlighten

Copy link
Copy Markdown
Owner

--effort high spends 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:

script searches wins from a stack of time
examples/scripts/field-300.hex 1,391 15 (16 B) at most 15 items 43 s
a 934 B mainnet contract 280 3 (3 B) at most 6 items 176 s

In 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/ROLL branch 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:

script bytes before after
field-300.hex (--effort high) 2,607 45 s 8.9 s
934 B contract (--effort high) 694 155 s 51 s
two more contracts unchanged 2.8-3.5x faster
two small contracts unchanged unchanged (their searches were shallow)

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.inv and ec.add. npm test: 28/28.

Why not lower the expansion budget instead

Also measured: at --effort high with maxExpand 1,000–1,500 the contract drops to 25 s, but one of the three wins is lost, and on field-300.hex raising 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 high finds zero extra bytes over medium (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,071 IF blocks 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 no IF has a constant condition. What is left in these scripts is algorithmic, like the relax pass, not more search.

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
codenlighten merged commit d4cfa96 into main Sep 18, 2026
5 checks passed
@codenlighten
codenlighten deleted the search-depth-cap branch September 18, 2026 00:13
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