Repository navigation
index: fix four-byte UTF-8 rune offset lookups - #1294
Open
perfloop-agent wants to merge 5 commits into
Open
perfloop-agent wants to merge 5 commits into
perfloop-agent wants to merge 5 commits into
Conversation
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
keegancsmith
approved these changes
Nov 26, 2026
keegancsmith
left a comment
Member
There was a problem hiding this comment.
Thanks!. This is quite test heavy. Could you drop TestSearchUTF8FileBoundary (it passes without the fix) and consider dropping TestFindOffsetReadBounds, which hand-builds an mmapedIndexFile? The other four cover the fix.
keegancsmith
added a commit
that referenced
this pull request
Nov 26, 2026
…#1299) 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.
Contributor
Author
|
@keegancsmith Dropped |
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.
Searching files with four-byte UTF-8 characters can silently miss valid matches or panic as though the shard were corrupt. A prefix of 76 emoji before
needle, for example, exhausts the old 300-byte decoding window. The lookup now reads enough bytes to reach the match's actual byte offset.cc @keegancsmith @burmudar
The bounded read uses
utf8.UTFMaxfor the remaining rune count, stops at the content corpus's end, and is skipped at exact samples. When a sample precedes the current file, decoding starts at that file's known byte boundary instead. Both content and filename decoding stop at the current file's end, so invalid bytes in separate files cannot combine into a character the indexer never saw.Local paired offset-lookup measurements with Go 1.27.3, in ns/op:
Small same-file content lookups remain slower; their measured costs are below. The additional comparisons bound decoding to the correct file and content section. Whole-Search timing remains inconclusive.
The index package's Go tests passed with the race detector.
Production prevalence of the failing input is unmeasured.
Small-lookup costs and coverage
Content-lookup medians with Go 1.27.3 in non-ASCII shards, in ns/op, from 28 paired runs:
The first five rows translate rune offset one in longer documents. The remaining rows use minimum-length documents for those inputs. These are lookup timings, not whole-Search timings. Longer content-span comparisons remain inconclusive. For Search over repository source files, 16 longer local paired runs bounded the paired change to −6,713 to +8,143 ns.
The tests in
index/contentprovider_test.goandindex/index_test.gocover exact offsets across single- and mixed-width text, both four-byte failure modes, substring case modes, regexp and symbol queries, and line and chunk results. File-boundary cases include invalid sequences, empty files, ASCII prefixes, and split UTF-8 bytes in filenames. The ASCII-shard shortcut is unchanged, and detected mapping corruption still raises a contextual shard panic.Case and evidence
Made by Perfloop. Human sponsor: Tomás Senart.