Skip to content

index: expand short classes and alternations in a regexp into substrings - #1280

Merged
keegancsmith merged 3 commits into
sourcegraph:mainfrom
shubhamkapoor01:feat/regexp-charclass-plan
Nov 26, 2026
Merged

keegancsmith merged 3 commits into
sourcegraph:mainfrom
shubhamkapoor01:feat/regexp-charclass-plan

Conversation

@shubhamkapoor01

@shubhamkapoor01 shubhamkapoor01 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Regexps made of plain text, small character classes and alternations, like f[ao]o, [Ee]rr or (foo|bar)baz, now become a short list of plain strings (at most 16) when that gives exactly the same matches. The search can then use the trigram index instead of running the regexp on every file. Case-insensitive searches get the same for patterns like (get|set)id.

On a Go stdlib shard (8,087 files), median of 10 runs, with the same files found:

  • [Ee]rr: 583 ms → 72.5 ms
  • case:no [Ee]rr: 508 ms → 73 ms
  • f[ao]o: 419 ms → 2.9 ms
  • (foo|bar)baz: 105 ms → 0.09 ms
  • case:no (get|set)id: 2.28 s → 0.72 ms

A new test checks random patterns against the regexp, including tricky letters like the Kelvin sign and the long s. Case-sensitive (?i:k)ey now also matches the Kelvin sign, like (?i)key already does.

Two things to know:

  • Highlights can differ a little when a string overlaps itself, same as the existing alternation path.
  • A case-sensitive pattern with more than 16 case variants still goes through the regexp. case:auto counts (?i)(get|set)id as case-sensitive, so by default it works the same as today (with case:no it's fast).

Fixes #1279

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

@shubhamkapoor01 Thanks, this is a nice win, and the differential test is great. Before landing, could we trim the planner a bit so it's easier to maintain?

  1. Drop the case-sensitive retry (if !ok && !ignoreCase { ... ok = ok && closed }) and the closed bookkeeping it needs. It only helps case:yes patterns with more than 16 case variants, and it's the subtlest part to reason about.
  2. Related: when the search is case-sensitive, have expandLiterals give up on FoldCase literals (an inline (?i:...) under case:yes) instead of expanding their case orbits. Those keep today's regexp path, and together with (1) it removes the orbit loop and every closed computation. expandLiterals would then handle only literals, classes, captures, concats and alternations, plus minFold for case-insensitive search.
  3. The prefix check only applies inside one expanded concat, so an outer alternation like (?:[ab]cd|bcdef) can highlight a different span than the regexp, though the matching files are the same. Could you add a short comment saying that's intentional?

I think that takes out quite a few lines without losing any of the common cases ([Ee]rr, f[ao]o, (foo|bar)baz).

Drop the case-sensitive retry and the closed bookkeeping it needed.
Under case-sensitive search, leave FoldCase literals to the regexp
instead of expanding their case orbits. Go parses a class like [Ee]
into such a literal too, so case-sensitive [Ee]rr keeps the regexp
path, while case-insensitive [Ee]rr, f[ao]o and (foo|bar)baz still
expand.

Also note that the prefix check covers one concat only: an outer
alternation can highlight a different span than the regexp.
@shubhamkapoor01

Copy link
Copy Markdown
Contributor Author

@keegancsmith Done, eval.go is 23 lines shorter, and f[ao]o and (foo|bar)baz are still fast. One catch: Go parses [Ee] the same way as (?i:e), and case:auto treats both as case-sensitive, so by default [Ee]rr and (?i)(get|set)id are back to main's speed (with case:no they're still fast). If you want [Ee]rr fast by default too, keeping the orbit loop would add back 9 lines.

Go parses a class like [Ee] into the same FoldCase literal as (?i:e),
and case:auto counts [Ee]rr as case sensitive. Leaving FoldCase
literals to the regexp put the default [Ee]rr back on the regexp path,
so expand each rune of such a literal into its case orbit again. This
also keeps case-sensitive (?i:k)ey matching the Kelvin sign, like Go's
regexp does.

FoldCase literals of more than 16 runes keep the regexp path, since
expanding them rune by rune copies every string for each rune.
@shubhamkapoor01

Copy link
Copy Markdown
Contributor Author

@keegancsmith I went ahead and kept the orbit loop (+14 lines, including a cap so a long (?i:...) literal can't make planning quadratic), so [Ee]rr stays fast by default: 583 ms → 72.5 ms on the stdlib shard. Happy to drop it again if you'd rather keep it shorter.

Copy link
Copy Markdown
Member

@shubhamkapoor01 Thanks, much nicer. Yes please, add the orbit loop back for case-sensitive FoldCase literals so [Ee]rr stays fast by default; just without the closed retry.

@shubhamkapoor01

Copy link
Copy Markdown
Contributor Author

@keegancsmith Our comments crossed: 6379599 already does that. The orbit loop is back for case-sensitive FoldCase literals, with no closed retry.

@keegancsmith
keegancsmith merged commit 8e450e8 into sourcegraph:main Nov 26, 2026
8 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.

index: short classes and alternations in a regexp skip the trigram index

2 participants