Repository navigation
index: expand short classes and alternations in a regexp into substrings - #1280
Conversation
keegancsmith
left a comment
There was a problem hiding this comment.
@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?
- 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.
- 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.
- 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.
|
@keegancsmith Done, eval.go is 23 lines shorter, and |
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.
|
@keegancsmith I went ahead and kept the orbit loop (+14 lines, including a cap so a long |
|
@shubhamkapoor01 Thanks, much nicer. Yes please, add the orbit loop back for case-sensitive |
|
@keegancsmith Our comments crossed: 6379599 already does that. The orbit loop is back for case-sensitive |
Regexps made of plain text, small character classes and alternations, like
f[ao]o,[Ee]rror(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 mscase:no [Ee]rr: 508 ms → 73 msf[ao]o: 419 ms → 2.9 ms(foo|bar)baz: 105 ms → 0.09 mscase:no (get|set)id: 2.28 s → 0.72 msA new test checks random patterns against the regexp, including tricky letters like the Kelvin sign and the long s. Case-sensitive
(?i:k)eynow also matches the Kelvin sign, like(?i)keyalready does.Two things to know:
(?i)(get|set)idas case-sensitive, so by default it works the same as today (with case:no it's fast).Fixes #1279