Repository navigation
Geodata: Reduce MPH matcher runtime memory usage - #6821
Conversation
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.
|
还有个好处是拉平后没那么多小对象了,因此每次 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.
|
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 |
|
13% is the percentage of small rule sets. Now, |
事实上对于索引 Geodata 这种常驻内存的静态数据集,本来就应该这么设计,在“decompress 程度”和“索引速度”间找个平衡点
|
|
算法方面再压内存,就只能换数据结构了,比如压缩树,隔壁就是这样 压缩树的代价就是会慢几倍,虽然只是数百纳秒 宏观方面,Xray 的非移动端也没有尽可能地复用 DomainMatcher 而是追求速度优先 |
|
|
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