From 37d9894dde0e7b2a8b71d9a3bd3bd4a446350d85 Mon Sep 17 00:00:00 2001 From: Meo597 <197331664+Meo597@users.noreply.github.com> Date: Mon, 5 Oct 2026 04:11:03 +0800 Subject: [PATCH] geodata: reduce Lua matcher build allocations --- common/geodata/lua.go | 25 +++++++++++++++---------- common/lua/luar.go | 4 ++-- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/common/geodata/lua.go b/common/geodata/lua.go index 21adf8980..f0b863d73 100644 --- a/common/geodata/lua.go +++ b/common/geodata/lua.go @@ -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) diff --git a/common/lua/luar.go b/common/lua/luar.go index eef71c514..6494187f6 100644 --- a/common/lua/luar.go +++ b/common/lua/luar.go @@ -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) }