Skip to content

index: derive rune offset sample at a corpus end on a sample boundary - #1299

Merged
keegancsmith merged 1 commit into
mainfrom
k/rune-offset-corpus-end
Nov 26, 2026
Merged

keegancsmith merged 1 commit into
mainfrom
k/rune-offset-corpus-end

Conversation

@keegancsmith

Copy link
Copy Markdown
Member

The builder writes a rune offset sample only when it reaches the start of a rune, so when a shard's content (or filename) corpus ends exactly on a multiple of 100 runes, nothing is written for the end. runeOffsetMap.lookup then extrapolates from the previous correction as if the trailing runes were ASCII, so findOffset at the end of the last file returns a wrong byte offset in non-ASCII shards (e.g. 294 instead of 394 for a 197-rune é file).

The fix is in the reader: makeRuneOffsetMap now takes the corpus end in runes and bytes, which read.go already has, and adds the missing correction. That fixes existing shards without a format change and costs at most one extra correction per map. A sample written by some future builder at the end is used as is. Search results weren't affected in practice, because findOffset is only called for match starts, which come before EOF. A search hit ending at EOF already returns correct offsets on main.

Found while reviewing #1294. This is independent of it and merges cleanly with it.

The builder writes a rune offset sample only when it reaches the start of a
rune, so when a shard's content or filename corpus ends exactly on a multiple
of runeOffsetFrequency runes there is no sample for the end. The lookup then
extrapolated from the previous correction as if the trailing runes were ASCII,
so findOffset at the end of the last file returned a wrong byte offset in
non-ASCII shards.

Derive the missing sample when loading the shard, since the corpus end in runes
and bytes is already known there. This fixes existing shards without changing
the on-disk format, and costs at most one extra correction per map.

Found while reviewing #1294.

Amp-Thread-ID: https://ampcode.com/threads/T-78959086-d0cc-476e-b607-38862deb5e09
Co-authored-by: Amp <amp@ampcode.com>
@keegancsmith
keegancsmith marked this pull request as ready for review November 26, 2026 15:06
@keegancsmith
keegancsmith merged commit 9518682 into main Nov 26, 2026
8 checks passed
@keegancsmith
keegancsmith deleted the k/rune-offset-corpus-end branch November 26, 2026 15:06
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