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()