optimize functions

This commit is contained in:
Sky Johnson 2025-03-27 21:45:39 -05:00
parent 29679349ef
commit b83f77d7a6

View File

@ -22,13 +22,17 @@ import (
// GoFunction defines the signature for Go functions callable from Lua // GoFunction defines the signature for Go functions callable from Lua
type GoFunction func(*State) int type GoFunction func(*State) int
// Static registry size reduces resizing operations
const initialRegistrySize = 64
var ( var (
// functionRegistry stores all registered Go functions // functionRegistry stores all registered Go functions
functionRegistry = struct { functionRegistry = struct {
sync.RWMutex 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 return -1
} }
// Use read-lock for better concurrency
functionRegistry.RLock() functionRegistry.RLock()
fn, ok := functionRegistry.funcs[ptr] fn, ok := functionRegistry.funcs[ptr]
functionRegistry.RUnlock() functionRegistry.RUnlock()