Skip to content

Geodata: Prefilter regexp rules by their required literals - #6818

Merged
RPRX merged 1 commit into
XTLS:mainfrom
4RH1T3CT0R7:perf/geodata-regex-literal-prefilter
Sep 25, 2026
Merged

RPRX merged 1 commit into
XTLS:mainfrom
4RH1T3CT0R7:perf/geodata-regex-literal-prefilter

Conversation

@4RH1T3CT0R7

@4RH1T3CT0R7 4RH1T3CT0R7 commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

regexp rules in geosite are matched one by one, so any domain the other rule types miss runs every regexp in the list (151 in geolocation-!cn). 162 of the 163 regexps contain a fixed piece like 91porn or .googlevideo.com. this pulls those pieces out of the regexp/syntax tree when the matcher is built and checks them with strings.Contains before running the regexp. only literals every match has to contain are used, so results don't change

MatchAny per domain on Loyalsoldier's geosite.dat: geolocation-!cn went from 5.9 µs to 0.24 µs, category-porn from 126 µs to 3.0 µs. I checked all 163 regexps against ~170M inputs plus fuzzing, no differences. building the regexp matchers takes about 0.35 ms longer

Collect the plain substrings every match of a regexp rule has to contain
from its parse tree, and skip the regexp when the input lacks any of them.
@Meo597

Meo597 commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

A pre-check for strings is a great idea.
I also noticed that regexp performance was poor, but I didn't delve into the library's implementation to find the cause. I thought they had already done so.
Therefore, I just simply set the priority of regexmatcher to the lowest.

If the strings extracted by requiredLiterals lack sufficient specificity, it could lead to negative returns. Can you continue to optimize this?
slices.SortStableFunc(m.literals, func(a, b string) int { return len(b) - len(a) })
I just saw it, no problem anymore.

@RPRX
RPRX merged commit a642a19 into XTLS:main Sep 25, 2026
40 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.

3 participants