Skip to content

index: preserve Unicode fold classes in substring verification - #1230

Merged
keegancsmith merged 10 commits into
sourcegraph:mainfrom
perfloop-oss:perfloop-pr-open-8k5yx2g7jj
Nov 13, 2026
Merged

keegancsmith merged 10 commits into
sourcegraph:mainfrom
perfloop-oss:perfloop-pr-open-8k5yx2g7jj

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Substring filters now use Unicode simple folding of the original pattern and report consumed subject bytes. This preserves sss/ssſ and σσσ/σσς matches with equivalent extraction or retained regexps. ASCII uses a 256-byte lowercase table and byte positions. Cached lowercase buffers retain cheap extent checks after matched prefixes; BM25 grouping is unchanged.

Paired Linux amd64/Go 1.27.3 medians:

Helper workload Base ns/op Changed ns/op Pairs
Mixed ASCII 59.17 49.66 9
Uppercase ASCII 57.1 49.79 11
Mixed Unicode 138.7 133.9 12

Remaining complete substring-verification costs, in ns/op (base → changed; pairs):

  • Padded Greek rejection: 20.98 → 26.74; 27. Emoji rejection: 21.845 → 26.43; 24.
  • Nine-rune Greek matching: 48.54 → 63.515; 14. Sigma matching: 54.4 → 73.97; 15.
  • Three-rune Greek matching: 38 → 53.71; 17. Sigma matching: 42.49 → 61.97; 17.
  • Rejection after 128 matched Greek/sigma runes before a 256-Kelvin suffix: 1524.5 → 2225; 16 / 1707 → 2405; 17.

These costs are accepted to prevent lost matches while preserving corrupt-span detection and cheap mismatch bounds. Net cost depends on unknown query mix; these checks do not measure whole-Search latency.

The native mixed-subject benchmark is in index/case_folding_bench_test.go. Reproduction recipe: go test ./index -run '^$' -bench '^BenchmarkCaseFoldingEqualsRunes$' -benchtime=2000000x -count=1.

Focused Go tests covering folding, substring/regexp search, byte spans, symbols, BM25 and corrupt-candidate bounds passed. Native tests cover two Search cases, the fold-table sweep and two cached-width bounds rows.

The separate regexp-engine fixed-width-prefix issue can still affect anchored long-s queries such as case:no content:sss$.

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

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

The fix looks right. Three things before merge:

  1. The ASCII fast path got slower, because the pattern byte is now case-folded on every iteration. Lowercase both bytes through a 256-byte lookup table (or only lowercase when pb != mb). In my benchmark either one matches main's speed. Lowercasing ASCII never leaves a fold class, so k/K and s/ſ still go to the slow path correctly.
  2. Drop the substrBytes == nil fallback in matchContent. Only test fixtures need it, so update them instead.
  3. Trim the integration test: put the ſ/ς/uppercase rows into TestCaseFoldingEqualsRunes, and keep one content case and one filename case. Keep the exhaustive fold-table test.

@perfloop
perfloop Bot force-pushed the perfloop-pr-open-8k5yx2g7jj branch from e7c5d0b to 648e7b7 Compare November 13, 2026 01:51
@perfloop-agent

Copy link
Copy Markdown
Contributor Author

ASCII comparison uses a 256-byte lookup table, and the description contains paired ASCII and Unicode results. Unicode folding avoids redundant Latin-1 work and stops when the fold cycle wraps; the description also states its remaining costs. The original-pattern fixtures and two-case integration test remain.

@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!

@keegancsmith
keegancsmith merged commit 9ac3459 into sourcegraph:main Nov 13, 2026
7 checks passed
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