sparse-ngrams: report each query gram's index-folded bytes - #142
sparse-ngrams: report each query gram's index-folded bytes#142aneubeck wants to merge 2 commits into
Conversation
`QueryGrams` emitted only the compact `NGram` key. Callers that want to
show a gram — tracing, snapshot tests, debugging output — cannot recover
the text from it: grams longer than 3 bytes are hashed into the 24-bit
payload, so `Debug` degrades to `NGram('0x20baba', len=4)`.
The state already holds the bytes: `extract_gram` builds the key from the
packed content window. Align that window once and hand the consumer a
`&[u8]` view of it, so consumers become
`FnMut(NGram, u32, Option<u8>, &[u8])`. The slice borrows a stack array
live only for the call, so nothing is allocated per gram.
`NGram::from_window_masked` goes away: aligning the window in
`extract_gram` (which the byte slice needs anyway) makes the masking
redundant, since the left shift already drops the high bytes.
Breaking change for the four `QueryGrams` emitters; the indexing
entrypoints (`collect_sparse_grams*`) are untouched, as their callers
already hold the content slice. Version bumped to 0.4.0.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 62aac6cc-77fd-42e4-9d10-27ab508f4062
There was a problem hiding this comment.
Pull request overview
Adds index-folded source bytes to QueryGrams callbacks for readable diagnostics without per-gram allocation.
Changes:
- Extends callbacks with
&[u8]and validates emitted bytes. - Simplifies aligned-window
NGramconstruction. - Updates documentation and bumps version to 0.4.0.
Show a summary per file
| File | Description |
|---|---|
crates/sparse-ngrams/src/query.rs |
Emits gram bytes and updates tests. |
crates/sparse-ngrams/src/ngram.rs |
Removes redundant masked-window helper. |
crates/sparse-ngrams/README.md |
Documents the callback API. |
crates/sparse-ngrams/Cargo.toml |
Bumps the breaking-release version. |
Review details
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 4/4 changed files
- Comments generated: 0
- Review effort level: Medium
Slicing the stack buffer with a runtime `len` leaves a bounds-check panic path in the generated code. That path is an observable side effect, so the optimizer cannot prove the buffer is dead even when the inlined consumer never touches it, and the byte-swap plus stack traffic survive. Measured on aarch64 (`--release`, an `append_byte` loop whose consumer only sums `gram.as_u32()`): 243 instructions with the plain slice against 229 without reporting bytes at all — a 14-instruction tax on every consumer, paid whether or not the bytes are wanted. Clamping the slice length to `MAX_SPARSE_GRAM_SIZE` makes the bound statically provable, so the panic path disappears and the whole buffer becomes dead code when unused: back to 229 instructions, byte-for-byte the pre-change codegen. Consumers that do read the bytes get cheaper code too (247 -> 240), since the eliminated check was on the hot path. The clamp never changes the slice: `len` is always in `2..=MAX_SPARSE_GRAM_SIZE`, which `from_window` already debug-asserts. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 62aac6cc-77fd-42e4-9d10-27ab508f4062
|
Follow-up pushed: the reported bytes were not free for consumers that ignore them, and now they are. What was wrongSlicing the stack buffer with a runtime Measured on aarch64 (
So every consumer paid a 14-instruction tax, wanted or not. The asm confirmed the cause — a live FixClamp the slice length to
Ignoring consumers are now byte-for-byte identical to the pre-change codegen, and consumers that do read the bytes got cheaper too (247 → 240), since the removed check was on the hot path. The clamp never changes the slice — |
Problem
QueryGramsemits only the compactNGramkey. Callers that want to show a gram — tracing, snapshot tests, debugging output — cannot recover the text from it: grams longer than 3 bytes are hashed into the 24-bit payload, soDebugdegrades toNGram('0x20baba', len=4).Blackbird hit exactly this after switching its query-time extraction over to this crate: its iterator-tree snapshots went from
to
Change
The state already holds the bytes:
extract_grambuilds the key from the packed content window. Align that window once and hand the consumer a&[u8]view of it. Consumers becomeFnMut(NGram, u32, Option<u8>, &[u8]):gramNGramkeyendfollowbytesbytesborrows a stack array that is live only for the duration of the call, so nothing is allocated per gram and the hot path is unchanged (the alignment shift was already being done insidefrom_window_masked).NGram::from_window_maskedis removed: aligning inextract_grammakes its masking redundant, since the left shift already drops the high bytes.Compatibility
Breaking for the four
QueryGramsemitters (append_char,append_byte,flush,consume_first). The indexing entrypoints (collect_sparse_grams*) are untouched — their callers already hold the content slice and can slice it themselves. Version bumped to 0.4.0.Testing
emitted_follow_is_the_actual_next_charis extended (and renamed toemitted_follow_and_bytes_match_the_input) to assert, for every emitted gram, that the reported slice equals the corresponding input slice and thatNGram::from_bytes(bytes)reproduces the emitted key.cargo test -p sparse-ngrams,cargo clippy --all-targetsandcargo fmt --checkare clean.