Skip to content

perf(encoding): keep natural-number suffixes on the stack - #400

Draft
tvolk131 wants to merge 1 commit into
BlockstreamResearch:masterfrom
tvolk131:perf/stack-natural-suffix
Draft

tvolk131 wants to merge 1 commit into
BlockstreamResearch:masterfrom
tvolk131:perf/stack-natural-suffix

Conversation

@tvolk131

@tvolk131 tvolk131 commented Oct 25, 2026 •

Copy link
Copy Markdown

encode_natural allocates a temporary vector for every encoded integer greater than one. Store its bounded suffix in a four-entry stack array and emit the populated entries in reverse order. On a 64-bit target the largest successive suffix lengths are 63, 5, 2, and 1; smaller pointer widths need no more entries. This retains the existing encoder assumption that usize is at most 64 bits.

In warmed release-mode serialization benchmarks on macOS ARM64 with Rust 1.99, this independently reduced CPU time by 8.1% on the small fixture and 7.4% on the 2,048-leaf fixture (median of 33 paired samples). Allocations for the latter fell from 8,219 to 2,075. It does not change peak requested heap bytes in these fixtures. These are synthetic operation measurements, not application speedups.

Add exact-bit checks against a recursive specification around every representable power of two and at usize::MAX, at all eight starting bit offsets. Keep the existing decoder round-trip test; the decoder's u32 limit cannot validate the encoder's full usize range.

Validation:

  • cargo test --locked --workspace --all-features (121 library tests, 14 sys tests, 2 doctests)
  • cargo test --locked -p simplicity-lang --lib --no-default-features (65 tests)
  • cargo +1.74.0 test --offline --locked -p simplicity-lang --lib --all-features (121 tests)
  • cargo fmt --all -- --check
  • cargo clippy --locked --workspace --all-targets --all-features -- --deny warnings
  • 17,896 differential natural-number encodings against unchanged master, including usize::MAX

Written by OpenAI Codex.

Written with GPT 6.1 Sol on Extra High.

Replace the temporary vector with four stack entries, the maximum needed by the existing encoder on targets up to 64 bits. Check exact encodings around every power of two and at usize::MAX at all starting bit offsets.

Written by OpenAI Codex.
@tvolk131

Copy link
Copy Markdown
Author

Draft until I finish iterating on it and hand-read & hand-test the code

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