geodata: reduce Lua matcher build allocations

This commit is contained in:
Meo597
2026-10-05 04:11:03 +08:00
parent e73fb0d500
commit 37d9894dde
2 changed files with 17 additions and 12 deletions
+15 -10
View File
@@ -7,6 +7,19 @@ import (
luar "layeh.com/gopher-luar"
)
var (
luaDomainDirectMethods = map[string]xlua.DirectMethod{
"Match": luaDomainMatch,
"MatchAny": luaDomainMatchAny,
}
luaIPDirectMethods = map[string]xlua.DirectMethod{
"Match": luaIPMatch,
"AnyMatch": luaIPAnyMatch,
"Matches": luaIPMatches,
"FilterIPs": luaIPFilterIPs,
}
)
// RegisterLua makes xray.geodata available to require in an LState.
func RegisterLua(L *lua.LState) {
L.PreloadModule("xray.geodata", func(L *lua.LState) int {
@@ -23,10 +36,7 @@ func RegisterLua(L *lua.LState) {
L.RaiseError("%v", err)
return 0
}
xlua.PushWithDirectMethods(L, matcher, map[string]xlua.DirectMethod{
"Match": luaDomainMatch,
"MatchAny": luaDomainMatchAny,
})
xlua.PushWithDirectMethods(L, matcher, luaDomainDirectMethods)
return 1
}))
@@ -41,12 +51,7 @@ func RegisterLua(L *lua.LState) {
L.RaiseError("%v", err)
return 0
}
xlua.PushWithDirectMethods(L, matcher, map[string]xlua.DirectMethod{
"Match": luaIPMatch,
"AnyMatch": luaIPAnyMatch,
"Matches": luaIPMatches,
"FilterIPs": luaIPFilterIPs,
})
xlua.PushWithDirectMethods(L, matcher, luaIPDirectMethods)
return 1
}))
L.Push(module)
+2 -2
View File
@@ -29,11 +29,11 @@ func PushWithDirectMethods(L *glua.LState, value any, directMethods map[string]D
return callLuarMethod(L, original)
})
// Keep luar's method aliases on the same direct binding.
methods.ForEach(func(key, method glua.LValue) {
for key, method := methods.Next(glua.LNil); key != glua.LNil; key, method = methods.Next(key) {
if method == original {
methods.RawSet(key, fn)
}
})
}
}
metatable.RawSetString("__index", methods)
}