Skip to content

Geodata: Reduce MPH matcher runtime memory usage - #6821

Merged
RPRX merged 3 commits into
XTLS:mainfrom
4RH1T3CT0R7:perf/geodata-mph-compact
Sep 27, 2026
Merged

RPRX merged 3 commits into
XTLS:mainfrom
4RH1T3CT0R7:perf/geodata-mph-compact

Conversation

@4RH1T3CT0R7

Copy link
Copy Markdown
Contributor

after Build, MphMatcherGroup kept a []string of patterns and a [][]uint32 of values, so every rule stayed a separate heap object for the GC to scan. now the patterns are one string and the values one []uint32, both indexed by offsets. the hash tables and the runtime.GC() calls are unchanged

on Loyalsoldier's geosite.dat category-ads-all now retains 14.4 MiB in under 300 objects instead of 32.2 MiB in 636k, and with ads-all, cn and geolocation-!cn loaded a forced GC takes 0.3 ms instead of 9-10 ms at GOMAXPROCS=2. build time and lookups on these lists are the same, the new differential test compares against a map over all short inputs

Keep the patterns and values of a built MphMatcherGroup in one string
and one slice indexed by offsets, instead of one heap object per rule.
@Meo597

Meo597 commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

full:xxxxx 越多,降低常驻内存的收益越大
去广告规则集大量的 full 因此会降低一倍常驻内存

还有个好处是拉平后没那么多小对象了,因此每次 GC 都会变快

但总的代价是匹配慢了 13% (~14ns)

Reading the span from patternOffs and slicing patterns cost three bounds
checks per probe where the old []string index had one, which showed up
as a few ns per MatchAny on small hot tables. level1 only holds rule
indices written by Build, so read the span without checks.
@4RH1T3CT0R7

Copy link
Copy Markdown
Contributor Author

thanks for measuring. I could reproduce it on the small in-repo benchmarks (64-1024 rules, all in cache): reading the span from patternOffs and slicing patterns added three bounds checks per probe where rules[n] had one, and MatchAny does 2-3 probes per domain. level1 only holds indices written by Build, so the new commit reads the span with unsafe and no checks. if you'd rather not have unsafe there, the checked version costs about 3% on those small tables

MatchAny, 5900X, 12 interleaved runs, main / before / now: Full/Succ 29.1 / 31.8 / 31.0 ns, Domain/Succ 23.9 / 25.1 / 24.0 ns, geomean of the 8 cases 32.7 / 33.6 / 32.8 ns. retained heap for category-ads-all stays at 14.3 MiB in 23 objects (27.9 MiB, 446k objects on main)

with real geosite.dat lists (ApplyDomain over geosite:cn and category-ads-all) I don't see a difference outside noise for any of the three, the misses dominate there. which benchmark gave you the 13%? I'd like to check that one too

@Meo597

Meo597 commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

13% is the percentage of small rule sets.
Indeed, the background noise is significant, so I previously tested ten rounds, with both small and large rule sets.
Stable overhead increased by approximately 5-14ns.
This is negligible given that large rule sets often take around 230ns.

Now, unsafe() can recover ~5ns.
I think this PR can be accepted; its benefits are substantial.

@RPRX

RPRX commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

on Loyalsoldier's geosite.dat category-ads-all now retains 14.4 MiB in under 300 objects instead of 32.2 MiB in 636k, and with ads-all, cn and geolocation-!cn loaded a forced GC takes 0.3 ms instead of 9-10 ms at GOMAXPROCS=2. build time and lookups on these lists are the same, the new differential test compares against a map over all short inputs

这个 PR 很猛啊

事实上对于索引 Geodata 这种常驻内存的静态数据集,本来就应该这么设计,在“decompress 程度”和“索引速度”间找个平衡点

所以其实我感觉还能进一步降低内存占用

@RPRX
RPRX merged commit 2953d44 into XTLS:main Sep 27, 2026
40 checks passed
@Meo597

Meo597 commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator

算法方面再压内存,就只能换数据结构了,比如压缩树,隔壁就是这样
但其实隔壁没做到极致,抠一下字符编码还能省 20% 内存

压缩树的代价就是会慢几倍,虽然只是数百纳秒
还需要推一个新的 geosite 格式,主要是我没 iOS 需求没啥动力做

宏观方面,Xray 的非移动端也没有尽可能地复用 DomainMatcher 而是追求速度优先
虽然很不理智,几百纳秒而已,但权衡的时候我无脑站在性能那一边

@RPRX

RPRX commented Sep 28, 2026

Copy link
Copy Markdown
Member

还是站在内存那边吧,这匹配相比于后面的“copy 大量流量”来说过于没存在感了,匹配性能不是很重要

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