Skip to content

Skip repeated DP searches for unchanged stack runs - #15

Merged
RobinLinus merged 1 commit into
BitVM:mainfrom
adrienlacombe:codex/skip-redundant-stack-run-dp
Sep 10, 2026
Merged

RobinLinus merged 1 commit into
BitVM:mainfrom
adrienlacombe:codex/skip-redundant-stack-run-dp

Conversation

@adrienlacombe

@adrienlacombe adrienlacombe commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

When a contiguous fixed-stack-op run cannot shrink, apply_local_rules currently repeats the entire suffix dynamic program at every following instruction. For 512 consecutive OP_TOALTSTACK operations, that means 1,411,036 symbolic-window queries even though the first DP solve has already considered every suffix.

Remember the unchanged run's end within the current pass, reducing this case to 5,566 queries. stack_run_replacement now reports the consumed run length even when it finds no shorter replacement. Only redundant DP calls are skipped: all other local rules continue running at each index, and the marker is recreated for each fixpoint pass. Compiler options, window sizes, rewrite candidates, stack-peak checks, and underflow requirements are unchanged.

The reuse is valid because the source instructions and PrefixFacts are immutable throughout one apply_local_rules call. If a suffix had any saving, the DP's unchanged-prefix edges would propagate it to the run's first entry. Therefore an unchanged whole-run optimum proves every suffix unchanged under the exact same facts used by subsequent calls. The implementation keeps emitting instructions through the existing local-rule loop rather than skipping that loop.

Validation against base 124b561ed75ac3ec4c6ad99207d8dcdd3bc67180:

  • cargo test --locked --workspace --all-features -- --skip fields:: passes all 66 tests (22 library, 16 integration, 28 macro); the diagnostic benchmark is ignored by default and was run separately.
  • Three new exact-byte test groups pass both before and after the patch: irreducible/separated runs, profitable suffixes, and a hash-pipeline rewrite crossing the end of a cached run.
  • A deterministic query-count regression fails against the original implementation at length 64 (18,876 queries versus the required 638), and passes with the patch at lengths 64, 128, 256, and 512.
  • Independent integration in the Bitcoin Script lab produces byte-identical JSON for all 24 Core differential fixtures: SHA256 c9b59372f57934e7bff761f4a35a000e0016779fc6ad263c6f23a3916303b50c. All five active primitive metric tests also pass without changing the recorded metrics. With the lab's BLAKE3 resource-test repair (runtime witness inputs and observable digest output), the full non-field lab suite passes under this compiler override: 409 unit tests, 18 integration tests, and 6 documentation tests; 24 existing tests are ignored and 143 field tests filtered. The lab's dependency pin remains unchanged; this is an isolated candidate integration run.
  • Formatting and git diff --check pass.

The included bounded benchmark warms the rewrite tables and asserts identical output bytes. Run it with cargo test --locked --release --workspace --all-features benchmark_unchanged_stack_runs -- --ignored --nocapture.

Run operations Baseline queries Patched queries Baseline microseconds Patched microseconds
64 18,876 638 24,764 440
128 82,588 1,342 100,129 864
256 345,180 2,750 430,984 1,704
512 1,411,036 5,566 1,404,572 3,371

These are single warmed release samples on Apple M3 Max with rustc 1.98.0, while other validation was running. Timings are diagnostic, not a general speedup claim or a CI threshold; query counts are deterministic. This removes quadratic repetition of symbolic DP work on unchanged runs; other existing scans can still be quadratic.

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.

2 participants