sparse-ngrams: report each query gram's index-folded bytes - #142
Open
aneubeck wants to merge 1 commit into
Open
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
Contributor
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.