From b83f77d7a6cd3b6f7a7a28e3f3835334d0e66cf1 Mon Sep 17 00:00:00 2001 From: Sky Johnson Date: Thu, 27 Mar 2025 21:45:39 -0500 Subject: [PATCH] optimize functions --- functions.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/functions.go b/functions.go index db5f2e1..f5d336b 100644 --- a/functions.go +++ b/functions.go @@ -22,13 +22,17 @@ import ( // GoFunction defines the signature for Go functions callable from Lua type GoFunction func(*State) int +// Static registry size reduces resizing operations +const initialRegistrySize = 64 + var ( // functionRegistry stores all registered Go functions functionRegistry = struct { sync.RWMutex - funcs map[unsafe.Pointer]GoFunction + funcs map[unsafe.Pointer]GoFunction + initOnce sync.Once }{ - funcs: make(map[unsafe.Pointer]GoFunction), + funcs: make(map[unsafe.Pointer]GoFunction, initialRegistrySize), } ) @@ -43,6 +47,7 @@ func goFunctionWrapper(L *C.lua_State) C.int { return -1 } + // Use read-lock for better concurrency functionRegistry.RLock() fn, ok := functionRegistry.funcs[ptr] functionRegistry.RUnlock()