Skip to content

Geodata: Reduce matcher memory on mobile and desktop, skip regexes that cannot match - #6867

Merged
RPRX merged 6 commits into
XTLS:mainfrom
4RH1T3CT0R7:geodata-mph-one-record
Sep 29, 2026
Merged

RPRX merged 6 commits into
XTLS:mainfrom
4RH1T3CT0R7:geodata-mph-one-record

Conversation

@4RH1T3CT0R7

@4RH1T3CT0R7 4RH1T3CT0R7 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

follow-up to #6821, with the rest of the geodata work kept together as Fangliding asked on #6868. the commits read on their own. the first stores one record per distinct pattern in MphMatcherGroup, in a byte arena behind a 0.99-load hash/displace/compress table with 4-byte slots and a 1-byte fingerprint, hashed right to left by a deterministic polynomial so one pass gives every parent domain. the second decodes geosite categories with protowire straight from the read buffer instead of going through []*Domain. the third makes ios and android build each category as an MphValueMatcher instead of the map-based trie, with MphValueMatcherCombiner hashing the input once per list. the fourth gives regexes a guard of the bytes a match can end with, so most misses never run the regexp

a typical desktop config goes from 33.6 to 11.4 MB, and on an iPhone ads + cn + !cn from +62 to +10 MiB of phys_footprint. hits get 1.5-1.7x faster on the phone, misses on regex-heavy lists about 40% and lookups reaching PRIVATE about 80%. the in-repo group-level Match benchmarks are 7-15% slower because Match now decodes values into its own slice, which MphValueMatcher hands on without the extra copy. on mobile the answers now equal desktop on rule shapes that Loyalsoldier and dlc don't contain (leading-dot or empty domain rules, keywords)

desktop memory, main -> this PR

Loyalsoldier geosite.dat, Ryzen 9 5900X, go1.27.0, GOMAXPROCS=4, fresh process per cell, median of 3, desktop factory with the first three commits (the regex commit does not change memory)

set        heap MB          B/rule         build peak MB
ads       15.03 ->  5.10   78.4 -> 26.6    95.4 -> 16.5
cn         6.75 ->  2.12   60.6 -> 19.0    54.6 ->  7.6
all3      24.76 ->  8.74   75.0 -> 26.4   152.1 -> 28.8
typical   33.62 -> 11.39   71.7 -> 24.3   102.8 -> 21.1
whole     27.06 -> 10.06   51.9 -> 19.3   164.3 -> 40.1   (build 38 s -> 2.4 s)

all3 = ads + cn + !cn in one list, typical = router [ads], [cn], [!cn] + DNS [cn, !cn]

iPhone, ads + cn + !cn, main -> this PR

ios/arm64, go1.27.1, a plain gomobile app (same phys_footprint ledger as an extension), one run per launch, 3-4 runs each

phys_footprint after load     +62.1..62.7  ->  +9.8..10.1 MiB
phys_footprint peak           +71.7..72.1  ->  +19.3..21.5 MiB
heap objects                  983k         ->  2.3k
forced GC, 1 core             16.9..17.7   ->  0.04..0.24 ms
build, GOGC=10 + 30 MiB       210..213     ->  105..106 ms
hit-exact                     64..67       ->  37..38 ns
lookups, desktop, paired A/B vs main, median ns/op
first commit (MatchAny)     main     PR      delta
ads/hit-exact              114.1    70.9    -37.8%
ads/crux-global            105.5    50.6    -52.1%
cn/crux-cn                 152.4   108.8    -28.6%

regex commit (MatchAny)
private/miss               469.1   100.5    -78.6%
!cn/miss                  2817.5  1696.5    -39.8%
all3/crux-global          2630.0  1547.0    -41.2%
all3/hit-exact              95.3    97.0     +1.8%

regex commit (Match)
dns2/crux-global          2655.0  1598.5    -39.8%

each block measures that commit alone against main; answers identical on every sample

correctness
  • MatchAny and Match results recorded on main for 10 lists x 6 workloads (about 1.2M lookups, with an edge-case list) are identical after every commit on both paths, apart from the mobile shapes above
  • all 163 regexes checked with and without the guard on 1.27M inputs each, plus a fuzz run: 0 differences
  • the loader accepts and rejects what proto.Unmarshal does on every category and attribute filter
  • tests pass for common/geodata, app/router and app/dns, also under GOARCH=386 and -race

@Meo597

Meo597 commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

完了,这下核武器真的从地里自己冒出来了。我 review 不了这个 PR 了
唯一能给的建议就是:如果返回值是新的切片,那么中间的调用环节可以省去一次拷贝
之前我记得在某个 PR 特意加的

@4RH1T3CT0R7 4RH1T3CT0R7 changed the title Geodata: store one record per pattern in MphMatcherGroup Geodata: reduce matcher memory on desktop and mobile, skip regexes that cannot match Sep 29, 2026
@4RH1T3CT0R7

Copy link
Copy Markdown
Contributor Author

thanks. that copy is already gone here: the group's Match returns a fresh slice and MphValueMatcher and MphIndexMatcher hand it on as is instead of going through CompositeMatches. TestMphDomainMatcher_MatchReturnsDetachedSlice still passes, and there is a new test that clears a returned slice and checks the next result

the loader, mobile and regex commits are in here now too, as Fangliding asked, so it can be read one commit at a time. every commit was checked against MatchAny/Match results recorded on main for 10 lists x 6 workloads, about 1.2M lookups

@Meo597

Meo597 commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

我看到了你把 CompositeMatches 拿掉了
但不只是 mph
还得确认一下 ac 自动机等是否需要 copy
根源是 dns 模块每次匹配需要排序,要防 race

@4RH1T3CT0R7

Copy link
Copy Markdown
Contributor Author

checked every path. the mph part is a new slice per call, and the ac and regex parts are appended into it rather than returned, so even when only the keyword part matches (CompositeMatchesReverse hands out ac's own slice there) append copies it into a fresh one. LinearValueMatcher keeps CompositeMatches' clone, and on mobile MphSetGroup builds its result per call and appends it to that clone

I added TestDomainMatcher_MatchResultsCanBeSortedConcurrently: full, domain, keyword, regex and geosite rules on both factories, each result overwritten up to its capacity before matching again, and 8 goroutines sorting results in place like dns does. it passes under -race and fails if the keyword slice or a single custom part escapes without a copy

@4RH1T3CT0R7
4RH1T3CT0R7 force-pushed the geodata-mph-one-record branch from 3c103da to 4b14911 Compare September 29, 2026 13:53
@Meo597

Meo597 commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

I’ve finished reviewing everything except the perfect hashing implementation. Reviewing the abstractions was honestly a bit painful, because I’d pretty much forgotten my own original design.

Could we rename CompactDomainMatcherFactory to CompactMphDomainMatcherFactory, and CompactDomainMatcher to CompactMphDomainMatcher? Compact was originally a generic way to combine shared category matchers, but this implementation is now tied to MPH. Including Mph would make that clear.

For MphSetGroup, I’d suggest MphValueMatcherCombiner. It combines already-built MphValueMatchers, binds each one to an external value, and shares the input hashing. “Combiner” describes that role more clearly, since Set and Group already mean lower-level components in this package.

We can keep the current file layout and implementation—just clarify the names.

@4RH1T3CT0R7

Copy link
Copy Markdown
Contributor Author

renamed as you suggested: CompactMphDomainMatcherFactory, CompactMphDomainMatcher and MphValueMatcherCombiner, and the field holding it is combiner now

@Meo597

Meo597 commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

@RPRX 建议尽早发版测试,iOS 重大利好

@RPRX

RPRX commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

这个 PR 也很猛啊,测了准确性没问题就行,等 @Fangliding 完成 #6866 就可以发 pre 了,正式版要等 Go 1.27.2

@RPRX RPRX changed the title Geodata: reduce matcher memory on desktop and mobile, skip regexes that cannot match Geodata: Reduce matcher memory on mobile and desktop, skip regexes that cannot match Sep 29, 2026
@RPRX
RPRX merged commit 6243d2a into XTLS:main Sep 29, 2026
40 checks passed
@PhoenixNil

Copy link
Copy Markdown
Contributor

这个 PR 也很猛啊,测了准确性没问题就行,等 @Fangliding 完成 #6866 就可以发 pre 了,正式版要等 Go 1.27.2

国庆能发正式版吗

@Meo597

Meo597 commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

够呛
https://go-review.googlesource.com/c/go/+/840665 都没合并呢

net/http 包现在是练习时长不到两年半的 junior 接手
世界是个草台班子

@KobeArthurScofield

Copy link
Copy Markdown
Collaborator

够呛
Go 又有一些私密安全报告在更新时才知道动了什么,不知道会不会拖住 win7 然后额外加时

@PhoenixNil

Copy link
Copy Markdown
Contributor

这个PR优化的挺好的,内存降低了挺多的

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.

5 participants