Skip to content

Conversation

@hossinasaadi
Copy link
Contributor

@hossinasaadi hossinasaadi commented Jan 8, 2026

I've implemented serialization for MphMatcherGroup and the memory usage decreased.
Same rules, This PR : 22 MB, Serialized : 14-15 MB

So far, I've done the following:
• Added a function to prebuild matchers from the config, keyed by ruleTag
• Serialized all rule matchers and saved them to a file
• Loaded the serialized data via mmap and applied it directly to routing conditions
• Add xray.cached.matcher flag for apply matcher form file
• Add function for build the matcher.cache from config json string (BuildDomainMatcherCache to RouterConfig)

#5488 (comment)
@RPRX @iambabyninja @mangustyura

Details :

1- required rules to have ruleTag :

{
   "domain" : [
     "domain:alidns.com",
     "domain:doh.pub",
     "domain:dot.pub",
     "domain:360.cn",
     "domain:onedns.net",

     "geosite:cn"

   ],
   "outboundTag" : "direct",
   "ruleTag" : "rule-1"
 },
 {
   "ip" : [
     "223.5.5.5",
     "223.6.6.6",
     "2400:3200::1",
     "2400:3200:baba::1",

     "geoip:cn"
   ],
   "outboundTag" : "direct",
   "ruleTag" : "rule-2"
 }

2- Cached file should be alongside the geofiles and file name should be matcher.cache

3- Sample code for pre-built domain matcher :

func makeDomainMatcherCache(configPath string, assetsDir string) error {
	os.Setenv("XRAY_LOCATION_ASSET", assetsDir)

	xrayBytes, err := os.ReadFile(configPath)
	if err != nil {
		return err
	}
	var xray conf.Config
	err := json.Unmarshal(xrayBytes, &xray)
	if err != nil {
		return nil
	}

	err = xray.RouterConfig.BuildDomainMatcherCache()
	if err != nil {
		return err
	}

	return nil
}

@hossinasaadi
Copy link
Contributor Author

ready to test.

@RPRX
Copy link
Member

RPRX commented Jan 8, 2026

2- Cached file should be alongside the geofiles and file name should be matcher.cache

配置文件内 Routing 加个配置项来指定名称吧,同时也是开启该功能的标志,不然开多个 Xray 会炸,DNS 的你也看一下

@RPRX
Copy link
Member

RPRX commented Jan 8, 2026

@yiguodev libXray 或许需要更新 BuildDomainMatcherCache()

@RPRX
Copy link
Member

RPRX commented Jan 8, 2026

本来以为 mmap 是把磁盘空间当运行内存用结果它确实是懒加载,不知道会不会像 swap,不过仍然 https://t.me/projectXtls/1384

当然 Xray 启动时也不再需要 read、unmarshal geosite、build 出完整的 DomainMatcher 从而降低了 peak memory usage

有了这项优化,前面的 123 全被解决了,重新拾起 pre-map 也没必要了

@iambabyninja
Copy link
Member

@RPRX, could you please advise which of the three PRs (
#5499
#5488
#5505
) should be tested on iOS?

We are ready to make a test build now.

@RPRX
Copy link
Member

RPRX commented Jan 13, 2026

@iambabyninja 要测试的是这个 PR,不过由于该伊朗贡献者目前失联了所以新版本暂时不会包括这个 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.

3 participants