lua: preallocate tables with appropriate capacities

This commit is contained in:
Meo597
2026-10-05 04:29:01 +08:00
parent 37d9894dde
commit 54610a39a5
4 changed files with 7 additions and 7 deletions
+3 -3
View File
@@ -52,9 +52,9 @@ func luaServers(s *DNS) []luaDNSServer {
func registerLua(L *lua.LState, servers []luaDNSServer, client featureDNS.Client) {
L.PreloadModule("xray.dns", func(L *lua.LState) int {
serverList := L.NewTable()
serverList := L.CreateTable(len(servers), 0)
for i, client := range servers {
server := L.NewTable()
server := L.CreateTable(0, 2)
server.RawSetString("ID", lua.LString(client.id))
@@ -90,7 +90,7 @@ func registerLua(L *lua.LState, servers []luaDNSServer, client featureDNS.Client
serverList.RawSetInt(i+1, server)
}
module := L.NewTable()
module := L.CreateTable(0, 2)
if servers != nil {
module.RawSetString("Servers", serverList)
}
+2 -2
View File
@@ -21,7 +21,7 @@ func (r *Router) RegisterLua(L *lua.LState) {
registerLuaContext(L)
L.PreloadModule("xray.router", func(L *lua.LState) int {
module := L.NewTable()
module := L.CreateTable(0, 7)
module.RawSetString("NetworkUnknown", lua.LNumber(net.Network_Unknown))
module.RawSetString("NetworkTCP", lua.LNumber(net.Network_TCP))
@@ -73,7 +73,7 @@ func registerLuaContext(L *lua.LState) {
}
return 1
}))
methods := L.NewTable()
methods := L.CreateTable(0, 4)
L.SetFuncs(methods, map[string]lua.LGFunction{
"GetSourceIPs": func(L *lua.LState) int {
xlua.PushUserData(L, checkLuaContext(L).GetSourceIPs())
+1 -1
View File
@@ -23,7 +23,7 @@ var (
// RegisterLua makes xray.geodata available to require in an LState.
func RegisterLua(L *lua.LState) {
L.PreloadModule("xray.geodata", func(L *lua.LState) int {
module := L.NewTable()
module := L.CreateTable(0, 2)
module.RawSetString("BuildDomainMatcher", L.NewFunction(func(L *lua.LState) int {
parsed, err := ParseDomainRules(luaRules(L), Domain_Domain)
+1 -1
View File
@@ -10,7 +10,7 @@ import (
// RegisterLua makes xray.log available to require in an LState.
func RegisterLua(L *lua.LState) {
L.PreloadModule("xray.log", func(L *lua.LState) int {
module := L.NewTable()
module := L.CreateTable(0, 4)
var source, prefix string // cache
for name, severity := range map[string]Severity{
"Debug": Severity_Debug,