Skip to content

Geodata: skip regexes whose match cannot end like the input - #6868

Closed
4RH1T3CT0R7 wants to merge 1 commit into
XTLS:mainfrom
4RH1T3CT0R7:geodata-regex-tail-guard
Closed

4RH1T3CT0R7 wants to merge 1 commit into
XTLS:mainfrom
4RH1T3CT0R7:geodata-regex-tail-guard

Conversation

@4RH1T3CT0R7

@4RH1T3CT0R7 4RH1T3CT0R7 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

on real traffic lookups mostly wait on regexes: GEOLOCATION-!CN's 151 regexes take about 95% of a 2-3 µs miss, and PRIVATE's literal-free ^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$ costs 300-400 ns on every lookup that reaches it, since #6818 cannot skip it

most of them end with a fixed tail like \.com$, so at build time each pattern is walked back from the end and the bytes a match can have in its last 8 positions are recorded. Match checks the input against those sets first and runs the regexp only when they allow a match. the guard is conservative (case folding, non-ASCII, (?m), branches without $), only nested repeats can drop it, and 161 of the 163 Loyalsoldier regexes get one

lookups reaching PRIVATE get about 80% faster, regex-bound misses and Match on dns2/all3 about 40%, and hits are unchanged

lookups, paired A/B, median ns/op

Loyalsoldier geosite.dat, Ryzen 9 5900X, go1.27.0, GOMAXPROCS=4. main vs main gives about ±2.5% on hit rows

MatchAny, desktop     main     PR      delta
private/hit-exact     50.4    50.1     -0.6%
private/miss         469.1   100.5    -78.6%
private/crux-global  449.2    87.5    -80.5%
!cn/miss            2817.5  1696.5    -39.8%
all3/hit-exact        95.3    97.0     +1.8%
all3/crux-global    2630.0  1547.0    -41.2%
dns2/miss           2875.0  1724.0    -40.0%

MatchAny, compact
private/miss         427.5    66.7    -84.4%
!cn/miss            2753.5  1696.0    -38.4%

Match
dns2/crux-global    2655.0  1598.5    -39.8%
all3/crux-global    2712.0  1626.0    -40.0%
soundness
  • all 163 distinct regexes in the file were run with and without the guard on 1.27M inputs each (benchmark workloads, edge cases, 1M generated domain-like strings): 207M comparisons, 0 differences
  • 62 crafted patterns (anchors, (?m), \z, (?i), Kelvin and long-s folding, non-ASCII classes, nested repeats) plus a 60 s go test -fuzz run against regexp: no wrong rejection
  • MatchAny/Match results recorded on main for 10 lists x 6 workloads are unchanged on both factories

most geosite regexes end with a fixed tail such as \.com$, so the
pattern is walked back from the end to record the bytes a match can have
at each of its last 8 positions, plus one set for every earlier byte
when it also starts with ^. Match checks these sets before the literal
prefilter and the regexp

only nested repeats can blow the walk up, so their steps are capped at
100000 and the guard is dropped past that. 161 of the 163 distinct
Loyalsoldier regexes get a guard. it rejects only inputs that cannot
match; case folding, non-ASCII, invalid UTF-8 and branches without $ are
handled conservatively
@Fangliding

Copy link
Copy Markdown
Member

请尽量把此类pr放在一起 理论上应该和 #6818 在一起的。。

@4RH1T3CT0R7

Copy link
Copy Markdown
Contributor Author

ok, moved it into #6867 together with the rest of the geodata changes

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