Skip to content

index: fix four-byte UTF-8 rune offset lookups - #1294

Open
perfloop-agent wants to merge 5 commits into
sourcegraph:mainfrom
perfloop-oss:perfloop-pr-open-rc0tkhsjyg
Open

perfloop-agent wants to merge 5 commits into
sourcegraph:mainfrom
perfloop-oss:perfloop-pr-open-rc0tkhsjyg

Conversation

@perfloop-agent

Copy link
Copy Markdown
Contributor

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.UTFMax for 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:

Input Before median After median Change
One preceding file 17.41 13.9 −18.8% (−23.1% to −11.1%, n=9)
Empty following file 15.09 9.18 −39.0% (−44.8% to −35.0%, n=9)
4,096 intervening empty files 17.72 13.98 −22.6% (−39.9% to −10.8%, n=9)

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:

Content lookup Before median After median Change
ASCII prefix 12.095 13.605 +12.2% (+7.3% to +24.7%, n=28)
Two-byte runes 16.075 17.62 +9.0% (+2.9% to +23.6%, n=28)
Three-byte runes 15.785 17.895 +11.9% (+5.3% to +17.4%, n=28)
Four-byte runes 16.355 18.545 +12.0% (+4.9% to +25.7%, n=28)
Mixed-width text 15.45 17.39 +9.7% (+7.2% to +17.9%, n=28)
Minimum document, two-byte 14.78 15.92 +8.4% (+2.6% to +13.3%, n=28)
Minimum document, three-byte 14.53 15.92 +8.6% (+4.5% to +16.5%, n=28)
Minimum document, four-byte 14.835 16.42 +9.9% (+2.7% to +20.8%, n=28)
Minimum document, mixed-width 12.1 13.445 +9.9% (+3.3% to +16.4%, n=28)

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.go and index/index_test.go cover 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.

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 keegancsmith left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

@keegancsmith Dropped TestSearchUTF8FileBoundary and TestFindOffsetReadBounds as you suggested; the other four tests cover the fix. Could you approve the CI run on the new head (7ef3af5)?

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