A few small performance improvements
When 'debug_assertions' is not enabled, don't bother doing asserts in stack_guard / stack_err_guard. Also, add an optional feature not enabled by default to disable LUA_USE_APICHECK in release mode. Once the bugs in rlua that allow you to trigger LUA_USE_APICHECK are fixed, this feature will be the default behavior.
This commit is contained in:
parent
de4d21f8ea
commit
84b009da03
|
@ -22,6 +22,12 @@ default = ["builtin-lua"]
|
||||||
# * LUA_NUMBER as double
|
# * LUA_NUMBER as double
|
||||||
# * LUA_EXTRASPACE is sizeof(void*)
|
# * LUA_EXTRASPACE is sizeof(void*)
|
||||||
builtin-lua = ["gcc"]
|
builtin-lua = ["gcc"]
|
||||||
|
# Don't define LUA_USE_APICHECK if we are in release mode. When
|
||||||
|
# debug_assertions are enabled, LUA_USE_APICHECK is enabled regardless. There
|
||||||
|
# are still a few known ways to trigger LUA_USE_APICHECK checks with rlua, but
|
||||||
|
# when these bugs are fixed, this option will go away and this behavior will be
|
||||||
|
# the default.
|
||||||
|
disable-lua-apicheck = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
libc = { version = "0.2" }
|
libc = { version = "0.2" }
|
||||||
|
|
5
build.rs
5
build.rs
|
@ -21,10 +21,9 @@ fn main() {
|
||||||
config.define("LUA_USE_WINDOWS", None);
|
config.define("LUA_USE_WINDOWS", None);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enables lua api checking, which has a slight performance penalty. We
|
if cfg!(debug_assertions) || !cfg!(feature = "disable-lua-apicheck") {
|
||||||
// could allow disabling this via cfg one day when there is much more
|
|
||||||
// confidence in the soundness of the API.
|
|
||||||
config.define("LUA_USE_APICHECK", None);
|
config.define("LUA_USE_APICHECK", None);
|
||||||
|
}
|
||||||
|
|
||||||
config
|
config
|
||||||
.include("lua")
|
.include("lua")
|
||||||
|
|
14
src/util.rs
14
src/util.rs
|
@ -24,6 +24,7 @@ pub unsafe fn stack_guard<F, R>(state: *mut ffi::lua_State, change: c_int, op: F
|
||||||
where
|
where
|
||||||
F: FnOnce() -> R,
|
F: FnOnce() -> R,
|
||||||
{
|
{
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
let expected = ffi::lua_gettop(state) + change;
|
let expected = ffi::lua_gettop(state) + change;
|
||||||
lua_internal_assert!(
|
lua_internal_assert!(
|
||||||
state,
|
state,
|
||||||
|
@ -43,6 +44,9 @@ where
|
||||||
);
|
);
|
||||||
|
|
||||||
res
|
res
|
||||||
|
} else {
|
||||||
|
op()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run an operation on a lua_State and automatically clean up the stack before
|
// Run an operation on a lua_State and automatically clean up the stack before
|
||||||
|
@ -58,6 +62,7 @@ pub unsafe fn stack_err_guard<F, R>(state: *mut ffi::lua_State, change: c_int, o
|
||||||
where
|
where
|
||||||
F: FnOnce() -> Result<R>,
|
F: FnOnce() -> Result<R>,
|
||||||
{
|
{
|
||||||
|
if cfg!(debug_assertions) {
|
||||||
let expected = ffi::lua_gettop(state) + change;
|
let expected = ffi::lua_gettop(state) + change;
|
||||||
lua_internal_assert!(
|
lua_internal_assert!(
|
||||||
state,
|
state,
|
||||||
|
@ -87,8 +92,15 @@ where
|
||||||
ffi::lua_settop(state, expected);
|
ffi::lua_settop(state, expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res
|
res
|
||||||
|
} else {
|
||||||
|
let prev = ffi::lua_gettop(state) + change;
|
||||||
|
let res = op();
|
||||||
|
if res.is_err() {
|
||||||
|
ffi::lua_settop(state, prev);
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call a function that calls into the Lua API and may trigger a Lua error (longjmp) in a safe way.
|
// Call a function that calls into the Lua API and may trigger a Lua error (longjmp) in a safe way.
|
||||||
|
|
Loading…
Reference in New Issue