Skip to content

sparse-ngrams: report each query gram's index-folded bytes - #142

Open
aneubeck wants to merge 1 commit into
mainfrom
aneubeck/query-gram-bytes
Open

sparse-ngrams: report each query gram's index-folded bytes#142
aneubeck wants to merge 1 commit into
mainfrom
aneubeck/query-gram-bytes

Conversation

@aneubeck

Copy link
Copy Markdown
Collaborator

Problem

QueryGrams emits 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).

Blackbird hit exactly this after switching its query-time extraction over to this crate: its iterator-tree snapshots went from

$[OR2[AND[*0=c:boring :20, c:art is g:40, 1=c:ing mi:20, ...

to

$[OR2[AND[*c:NGram('t w', len=3):40, 0=c:NGram('0x20baba', len=4):2, ...

Change

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. Consumers become FnMut(NGram, u32, Option<u8>, &[u8]):

arg meaning
gram the compact NGram key
end position of the character just after the gram
follow that following byte, when already fed
bytes the gram's index-folded bytes, in reading order

bytes borrows 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 inside from_window_masked).

NGram::from_window_masked is removed: aligning in extract_gram makes its masking redundant, since the left shift already drops the high bytes.

Compatibility

Breaking for the four QueryGrams emitters (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_char is extended (and renamed to emitted_follow_and_bytes_match_the_input) to assert, for every emitted gram, that the reported slice equals the corresponding input slice and that NGram::from_bytes(bytes) reproduces the emitted key. cargo test -p sparse-ngrams, cargo clippy --all-targets and cargo fmt --check are clean.

`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
@aneubeck
aneubeck requested a review from a team as a code owner July 30, 2026 15:46
Copilot AI review requested due to automatic review settings July 30, 2026 15:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 NGram construction.
  • 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

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