Skip to content

Commit 335f433

Browse files
committed
Optimize callbacks
1 parent 2aed548 commit 335f433

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

src/lua.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ use crate::types::{
2222
};
2323
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataMethods, UserDataWrapped};
2424
use crate::util::{
25-
assert_stack, callback_error, check_stack, get_gc_userdata, get_main_state,
26-
get_meta_gc_userdata, get_wrapped_error, init_error_registry, init_gc_metatable_for,
27-
init_userdata_metatable, pop_error, protect_lua, protect_lua_closure, push_gc_userdata,
28-
push_meta_gc_userdata, push_string, push_userdata, push_wrapped_error, StackGuard,
25+
assert_stack, callback_error, check_stack, get_gc_userdata, get_main_state, get_userdata,
26+
get_wrapped_error, init_error_registry, init_gc_metatable_for, init_userdata_metatable,
27+
pop_error, protect_lua, protect_lua_closure, push_gc_userdata, push_meta_gc_userdata,
28+
push_string, push_userdata, push_wrapped_error, StackGuard,
2929
};
3030
use crate::value::{FromLua, FromLuaMulti, MultiValue, Nil, ToLua, ToLuaMulti, Value};
3131

@@ -1504,9 +1504,10 @@ impl Lua {
15041504
}
15051505

15061506
pub(crate) unsafe fn userdata_metatable<T: 'static + UserData>(&self) -> Result<c_int> {
1507+
let type_id = TypeId::of::<T>();
15071508
if let Some(table_id) = mlua_expect!(self.extra.lock(), "extra is poisoned")
15081509
.registered_userdata
1509-
.get(&TypeId::of::<T>())
1510+
.get(&type_id)
15101511
{
15111512
return Ok(*table_id);
15121513
}
@@ -1567,7 +1568,7 @@ impl Lua {
15671568
})?;
15681569

15691570
let mut extra = mlua_expect!(self.extra.lock(), "extra is poisoned");
1570-
extra.registered_userdata.insert(TypeId::of::<T>(), id);
1571+
extra.registered_userdata.insert(type_id, id);
15711572
extra.registered_userdata_mt.insert(ptr);
15721573

15731574
Ok(id)
@@ -1616,12 +1617,13 @@ impl Lua {
16161617
{
16171618
unsafe extern "C" fn call_callback(state: *mut ffi::lua_State) -> c_int {
16181619
callback_error(state, |nargs| {
1619-
let func =
1620-
get_meta_gc_userdata::<Callback, Callback>(state, ffi::lua_upvalueindex(1));
1621-
let lua = get_gc_userdata::<Lua>(state, ffi::lua_upvalueindex(2));
1622-
if func.is_null() || lua.is_null() {
1620+
if ffi::lua_type(state, ffi::lua_upvalueindex(1)) == ffi::LUA_TNIL
1621+
|| ffi::lua_type(state, ffi::lua_upvalueindex(2)) == ffi::LUA_TNIL
1622+
{
16231623
return Err(Error::CallbackDestructed);
16241624
}
1625+
let func = get_userdata::<Callback>(state, ffi::lua_upvalueindex(1));
1626+
let lua = get_userdata::<Lua>(state, ffi::lua_upvalueindex(2));
16251627

16261628
if nargs < ffi::LUA_MINSTACK {
16271629
check_stack(state, ffi::LUA_MINSTACK - nargs)?;
@@ -1681,14 +1683,13 @@ impl Lua {
16811683

16821684
unsafe extern "C" fn call_callback(state: *mut ffi::lua_State) -> c_int {
16831685
callback_error(state, |nargs| {
1684-
let func = get_meta_gc_userdata::<AsyncCallback, AsyncCallback>(
1685-
state,
1686-
ffi::lua_upvalueindex(1),
1687-
);
1688-
let lua = get_gc_userdata::<Lua>(state, ffi::lua_upvalueindex(2));
1689-
if func.is_null() || lua.is_null() {
1686+
if ffi::lua_type(state, ffi::lua_upvalueindex(1)) == ffi::LUA_TNIL
1687+
|| ffi::lua_type(state, ffi::lua_upvalueindex(2)) == ffi::LUA_TNIL
1688+
{
16901689
return Err(Error::CallbackDestructed);
16911690
}
1691+
let func = get_userdata::<AsyncCallback>(state, ffi::lua_upvalueindex(1));
1692+
let lua = get_userdata::<Lua>(state, ffi::lua_upvalueindex(2));
16921693

16931694
if nargs < ffi::LUA_MINSTACK {
16941695
check_stack(state, ffi::LUA_MINSTACK - nargs)?;
@@ -1715,14 +1716,16 @@ impl Lua {
17151716

17161717
unsafe extern "C" fn poll_future(state: *mut ffi::lua_State) -> c_int {
17171718
callback_error(state, |nargs| {
1718-
let fut = get_gc_userdata::<LocalBoxFuture<Result<MultiValue>>>(
1719+
if ffi::lua_type(state, ffi::lua_upvalueindex(1)) == ffi::LUA_TNIL
1720+
|| ffi::lua_type(state, ffi::lua_upvalueindex(2)) == ffi::LUA_TNIL
1721+
{
1722+
return Err(Error::CallbackDestructed);
1723+
}
1724+
let fut = get_userdata::<LocalBoxFuture<Result<MultiValue>>>(
17191725
state,
17201726
ffi::lua_upvalueindex(1),
17211727
);
1722-
let lua = get_gc_userdata::<Lua>(state, ffi::lua_upvalueindex(2));
1723-
if fut.is_null() || lua.is_null() {
1724-
return Err(Error::CallbackDestructed);
1725-
}
1728+
let lua = get_userdata::<Lua>(state, ffi::lua_upvalueindex(2));
17261729

17271730
if nargs < ffi::LUA_MINSTACK {
17281731
check_stack(state, ffi::LUA_MINSTACK - nargs)?;
@@ -1791,7 +1794,7 @@ impl Lua {
17911794
self.load(
17921795
r#"
17931796
poll = get_poll(...)
1794-
local poll = poll
1797+
local poll, yield, unpack = poll, yield, unpack
17951798
while true do
17961799
ready, res = poll()
17971800
if ready then

src/util.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,11 @@ pub unsafe fn push_meta_gc_userdata<MT: Any, T>(state: *mut ffi::lua_State, t: T
279279

280280
// Uses 2 stack spaces, does not call checkstack
281281
pub unsafe fn get_gc_userdata<T: Any>(state: *mut ffi::lua_State, index: c_int) -> *mut T {
282-
get_meta_gc_userdata::<T, T>(state, index)
283-
}
284-
285-
pub unsafe fn get_meta_gc_userdata<MT: Any, T>(state: *mut ffi::lua_State, index: c_int) -> *mut T {
286282
let ud = ffi::lua_touserdata(state, index) as *mut T;
287283
if ud.is_null() || ffi::lua_getmetatable(state, index) == 0 {
288284
return ptr::null_mut();
289285
}
290-
get_gc_metatable_for::<MT>(state);
286+
get_gc_metatable_for::<T>(state);
291287
let res = ffi::lua_rawequal(state, -1, -2) != 0;
292288
ffi::lua_pop(state, 2);
293289
if !res {

0 commit comments

Comments
 (0)