optimize functions
This commit is contained in:
parent
29679349ef
commit
b83f77d7a6
|
@ -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()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user