Skip to content

feat(match): zsync-inspired sequential-match lookahead (ZSO-2) - #4624

Merged
oferchen merged 1 commit into
masterfrom
feat/matching-zsync-seq-match-lookahead-zso2
May 21, 2026
Merged

oferchen merged 1 commit into
masterfrom
feat/matching-zsync-seq-match-lookahead-zso2

Conversation

@oferchen

Copy link
Copy Markdown
Owner

Summary

Adds a zsync-style sequential-match lookahead (ZSO-2 / #2510) on DeltaSignatureIndex. After a confirmed match at basis block K, the matcher probes a recorded successor link first and only falls back to the rolling-rsum / bithash / compact-lookup chain on miss. The optimization is purely in-memory: no wire bytes change.

  • next_match: Vec<u32> (with u32::MAX sentinel) on each DeltaSignatureIndex, populated in a single pass during populate_index and reset by rebuild for the per-segment INC_RECURSE lifecycle (ZSO-7).
  • try_next_match_bytes / try_next_match_slices probe APIs that follow the recorded successor, do a cheap rolling-sum compare, then a strong-checksum verify. Skip the tag-table / bithash / compact-lookup work on hit.
  • SeqMatchCounters (probes / hits / misses) wired behind the existing bench-internal cfg gate so release builds carry zero overhead. Test-only accessor seq_match_counters() lets the regression suite assert the lookahead path is taken.
  • Generator wiring: the in-chain probe (the one taken after every confirmed match) now goes through try_next_match_slices; the start-of-file Some(0) seed continues to use the existing check_block_match_slices shortcut. The match_idx + 1 arithmetic is replaced by index.next_match(match_idx) so any future basis re-ordering is honoured automatically.

Wire-byte parity

Wire format is unchanged by construction: nothing serialized changes, only the in-memory probe ordering. Verified by running the protocol golden suite both before and after:

cargo nextest run -p protocol --all-features -E 'test(golden)'
  Summary [   0.454s] 407 tests run: 407 passed, 5186 skipped

Measured behaviour (counter-based)

The regression suite pins concrete probe / hit / miss counts straight from the new counters:

  • seq_match_hits_consecutive_blocks (6 distinct blocks, source = basis):
    • probes = 5, hits = 5, misses = 0
    • every in-chain transition lands through the lookahead, the rolling-rsum lookup is bypassed on every adjacent boundary.
  • seq_match_falls_back_on_mismatch (3 basis blocks followed by block_len + 7 literal bytes):
    • hits >= 2 (both in-run transitions take the lookahead),
    • misses >= 1 (the divergence after block 2 forces a fall-through to find_match_slices_filtered).
  • next_match_is_consistent_with_basis_order (proptest, 2..=8 blocks): every interior block resolves its successor through try_next_match_bytes with no misses.

Throughput

Existing seq_match_redundant bench harness (cargo bench -p matching --features bench-internal --bench seq_match_redundant -- --quick) on the post-change tree:

Corpus Throughput
log_like (16 MiB, append-only log fixture) ~275 MiB/s
tar_like (16 MiB, near-duplicate records) ~68 MiB/s
random_control (16 MiB, uncorrelated) ~375 MiB/s

A wire-trapped before/after speedup number would need a long-running, controlled benchmark pass on a quiet host; the harness above is the bench infra the design note (docs/design/zsync-seq-match.md) calls for and is preserved unchanged.

Test plan

  • cargo fmt --all -- --check clean
  • cargo clippy --workspace --all-targets --all-features --no-deps -- -D warnings clean
  • cargo nextest run -p matching --all-features - 330 passed, 3 skipped
  • cargo nextest run -p protocol --all-features -E 'test(golden)' - 407 passed (wire-byte parity)
  • cargo bench -p matching --features bench-internal --bench seq_match_redundant -- --quick runs cleanly

Record the source-order successor of every indexed basis block in a new
`next_match: Vec<u32>` parallel array on `DeltaSignatureIndex`, populated
in a single pass during `populate_index` and cleared by `rebuild` so the
per-segment INC_RECURSE lifecycle stays leak-free (ZSO-7).

Expose two new probe entry points - `try_next_match_bytes` and
`try_next_match_slices` - that consult the recorded successor, verify the
strong checksum, and skip the tag-table / bithash / compact-lookup probe
chain on the common "next block in source order matches the next window"
path. Test-only `SeqMatchCounters` (probes / hits / misses) hang off the
index behind the existing `bench-internal` cfg gate, carrying zero
overhead in release builds.

Wire the generator's in-chain probe (the one taken after every confirmed
match) onto the new `try_next_match_slices` so the lookahead is driven
through the index's link table rather than the prior `match_idx + 1`
arithmetic. The start-of-file `Some(0)` seed continues to use the
existing `check_block_match_slices` shortcut.

Tests in `crates/matching/src/index/seq_match_tests.rs` cover:
- the successor chain and out-of-range contract for `next_match`,
- hit / miss / tail-block behaviour for the new probes (with counter
  assertions),
- generator wiring (6 sequential blocks => 5 probes / 5 hits / 0 misses;
  divergent source => >= 1 miss with the full lookup taking over),
- per-segment isolation across `rebuild` mirroring the bithash leak test,
- a proptest over basis sizes 2..=8 pinning the successor link.

Wire-byte parity preserved: protocol golden tests (407 cases) pass.
@github-actions github-actions Bot added the enhancement New feature or request label May 20, 2026
@oferchen
oferchen merged commit 08897c6 into master May 21, 2026
50 of 55 checks passed
oferchen added a commit that referenced this pull request May 21, 2026
…SO-5)

Adds the ZSO-5 (task #2513) regression suite at
crates/matching/tests/zsync_wire_parity.rs. For each ZSO-1..4
optimization, constructs a basis + source pair designed to exercise
that optimization's code path, runs generate_delta, serialises the
DeltaScript through the production wire-format encoder
(protocol::wire::write_token_stream), and pins the resulting bytes
against (a) determinism across repeated runs and (b) round-trip
reconstruction of the source via apply_delta.

Active tests:
- bithash_optimization_preserves_wire_bytes (ZSO-1, task #2510, shipped
  in PR #3737) - 16 KiB random basis + disjoint random source so every
  rolling probe walks the bithash reject path. Asserts all-literal
  script and deterministic wire bytes.
- seq_match_optimization_preserves_wire_bytes (ZSO-2, task #2510,
  landing on PR #4624) - 84-block basis used as its own source so
  every adjacent-block hint fires. Asserts single fat Copy token and
  deterministic wire bytes. Test was authored to remain green on
  master (extend_run + script-level coalescing already produce the
  pinned shape) and stays green when PR #4624 swaps the probe path.
- all_active_zso_fixtures_are_deterministic - cross-cutting check that
  signals "matching pipeline became non-deterministic" as a single
  failure.

Stub tests (#[ignore]):
- hash_chain_prune_preserves_wire_bytes (ZSO-3, task #2511)
- compact_rolling_key_preserves_wire_bytes (ZSO-4, task #2512)

The stubs are structured so the only step required when those
optimizations land is to drop #[ignore] and swap the placeholder
basis/source for the duplicate-heavy / small-basis corpora documented
in the test rustdoc.

Complements the protocol-layer golden tests at
crates/protocol/tests/golden_protocol_v28_mplex_delta_stats.rs which
pin the token frame format but do not synthesize ZSO-specific basis
fixtures.

No production code changes. No new dependencies (uses a small in-test
LCG to keep the corpus deterministic). All 321 matching tests pass;
407 protocol golden tests pass.
oferchen added a commit that referenced this pull request May 21, 2026
…SO-5) (#4627)

Adds the ZSO-5 (task #2513) regression suite at
crates/matching/tests/zsync_wire_parity.rs. For each ZSO-1..4
optimization, constructs a basis + source pair designed to exercise
that optimization's code path, runs generate_delta, serialises the
DeltaScript through the production wire-format encoder
(protocol::wire::write_token_stream), and pins the resulting bytes
against (a) determinism across repeated runs and (b) round-trip
reconstruction of the source via apply_delta.

Active tests:
- bithash_optimization_preserves_wire_bytes (ZSO-1, task #2510, shipped
  in PR #3737) - 16 KiB random basis + disjoint random source so every
  rolling probe walks the bithash reject path. Asserts all-literal
  script and deterministic wire bytes.
- seq_match_optimization_preserves_wire_bytes (ZSO-2, task #2510,
  landing on PR #4624) - 84-block basis used as its own source so
  every adjacent-block hint fires. Asserts single fat Copy token and
  deterministic wire bytes. Test was authored to remain green on
  master (extend_run + script-level coalescing already produce the
  pinned shape) and stays green when PR #4624 swaps the probe path.
- all_active_zso_fixtures_are_deterministic - cross-cutting check that
  signals "matching pipeline became non-deterministic" as a single
  failure.

Stub tests (#[ignore]):
- hash_chain_prune_preserves_wire_bytes (ZSO-3, task #2511)
- compact_rolling_key_preserves_wire_bytes (ZSO-4, task #2512)

The stubs are structured so the only step required when those
optimizations land is to drop #[ignore] and swap the placeholder
basis/source for the duplicate-heavy / small-basis corpora documented
in the test rustdoc.

Complements the protocol-layer golden tests at
crates/protocol/tests/golden_protocol_v28_mplex_delta_stats.rs which
pin the token frame format but do not synthesize ZSO-specific basis
fixtures.

No production code changes. No new dependencies (uses a small in-test
LCG to keep the corpus deterministic). All 321 matching tests pass;
407 protocol golden tests pass.
@oferchen
oferchen deleted the feat/matching-zsync-seq-match-lookahead-zso2 branch May 21, 2026 14:08
oferchen added a commit that referenced this pull request Aug 24, 2026
Record the source-order successor of every indexed basis block in a new
`next_match: Vec<u32>` parallel array on `DeltaSignatureIndex`, populated
in a single pass during `populate_index` and cleared by `rebuild` so the
per-segment INC_RECURSE lifecycle stays leak-free (ZSO-7).

Expose two new probe entry points - `try_next_match_bytes` and
`try_next_match_slices` - that consult the recorded successor, verify the
strong checksum, and skip the tag-table / bithash / compact-lookup probe
chain on the common "next block in source order matches the next window"
path. Test-only `SeqMatchCounters` (probes / hits / misses) hang off the
index behind the existing `bench-internal` cfg gate, carrying zero
overhead in release builds.

Wire the generator's in-chain probe (the one taken after every confirmed
match) onto the new `try_next_match_slices` so the lookahead is driven
through the index's link table rather than the prior `match_idx + 1`
arithmetic. The start-of-file `Some(0)` seed continues to use the
existing `check_block_match_slices` shortcut.

Tests in `crates/matching/src/index/seq_match_tests.rs` cover:
- the successor chain and out-of-range contract for `next_match`,
- hit / miss / tail-block behaviour for the new probes (with counter
  assertions),
- generator wiring (6 sequential blocks => 5 probes / 5 hits / 0 misses;
  divergent source => >= 1 miss with the full lookup taking over),
- per-segment isolation across `rebuild` mirroring the bithash leak test,
- a proptest over basis sizes 2..=8 pinning the successor link.

Wire-byte parity preserved: protocol golden tests (407 cases) pass.
oferchen added a commit that referenced this pull request Aug 24, 2026
…SO-5) (#4627)

Adds the ZSO-5 (task #2513) regression suite at
crates/matching/tests/zsync_wire_parity.rs. For each ZSO-1..4
optimization, constructs a basis + source pair designed to exercise
that optimization's code path, runs generate_delta, serialises the
DeltaScript through the production wire-format encoder
(protocol::wire::write_token_stream), and pins the resulting bytes
against (a) determinism across repeated runs and (b) round-trip
reconstruction of the source via apply_delta.

Active tests:
- bithash_optimization_preserves_wire_bytes (ZSO-1, task #2510, shipped
  in PR #3737) - 16 KiB random basis + disjoint random source so every
  rolling probe walks the bithash reject path. Asserts all-literal
  script and deterministic wire bytes.
- seq_match_optimization_preserves_wire_bytes (ZSO-2, task #2510,
  landing on PR #4624) - 84-block basis used as its own source so
  every adjacent-block hint fires. Asserts single fat Copy token and
  deterministic wire bytes. Test was authored to remain green on
  master (extend_run + script-level coalescing already produce the
  pinned shape) and stays green when PR #4624 swaps the probe path.
- all_active_zso_fixtures_are_deterministic - cross-cutting check that
  signals "matching pipeline became non-deterministic" as a single
  failure.

Stub tests (#[ignore]):
- hash_chain_prune_preserves_wire_bytes (ZSO-3, task #2511)
- compact_rolling_key_preserves_wire_bytes (ZSO-4, task #2512)

The stubs are structured so the only step required when those
optimizations land is to drop #[ignore] and swap the placeholder
basis/source for the duplicate-heavy / small-basis corpora documented
in the test rustdoc.

Complements the protocol-layer golden tests at
crates/protocol/tests/golden_protocol_v28_mplex_delta_stats.rs which
pin the token frame format but do not synthesize ZSO-specific basis
fixtures.

No production code changes. No new dependencies (uses a small in-test
LCG to keep the corpus deterministic). All 321 matching tests pass;
407 protocol golden tests pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant