Skip to content

Commit c36808b

Browse files
committed
Faster Function::call() for lua51/jit/luau
1 parent 2022de2 commit c36808b

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/function.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ use std::slice;
66

77
use crate::error::{Error, Result};
88
use crate::lua::Lua;
9-
use crate::memory::MemoryState;
109
use crate::table::Table;
1110
use crate::types::{Callback, LuaRef, MaybeSend};
1211
use crate::util::{
13-
assert_stack, check_stack, error_traceback, linenumber_to_usize, pop_error, ptr_to_lossy_str,
14-
ptr_to_str, StackGuard,
12+
assert_stack, check_stack, linenumber_to_usize, pop_error, ptr_to_lossy_str, ptr_to_str,
13+
StackGuard,
1514
};
1615
use crate::value::{FromLuaMulti, IntoLua, IntoLuaMulti, Value};
1716

@@ -131,7 +130,7 @@ impl<'lua> Function<'lua> {
131130
check_stack(state, 2)?;
132131

133132
// Push error handler
134-
MemoryState::relax_limit_with(state, || ffi::lua_pushcfunction(state, error_traceback));
133+
lua.push_error_traceback();
135134
let stack_start = ffi::lua_gettop(state);
136135
// Push function and the arguments
137136
lua.push_ref(&self.0);

src/lua.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ use crate::types::{
3131
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataCell};
3232
use crate::userdata_impl::{UserDataProxy, UserDataRegistry};
3333
use crate::util::{
34-
self, assert_stack, check_stack, get_destructed_userdata_metatable, get_gc_metatable,
35-
get_gc_userdata, get_main_state, get_userdata, init_error_registry, init_gc_metatable,
36-
init_userdata_metatable, pop_error, push_gc_userdata, push_string, push_table, rawset_field,
37-
safe_pcall, safe_xpcall, short_type_name, StackGuard, WrappedFailure,
34+
self, assert_stack, check_stack, error_traceback, get_destructed_userdata_metatable,
35+
get_gc_metatable, get_gc_userdata, get_main_state, get_userdata, init_error_registry,
36+
init_gc_metatable, init_userdata_metatable, pop_error, push_gc_userdata, push_string,
37+
push_table, rawset_field, safe_pcall, safe_xpcall, short_type_name, StackGuard, WrappedFailure,
3838
};
3939
use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, MultiValue, Nil, Value};
4040

@@ -499,6 +499,13 @@ impl Lua {
499499
ptr
500500
};
501501

502+
// Store `error_traceback` function on the ref stack
503+
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
504+
{
505+
ffi::lua_pushcfunction(ref_thread, error_traceback);
506+
assert_eq!(ffi::lua_gettop(ref_thread), ExtraData::ERROR_TRACEBACK_IDX);
507+
}
508+
502509
// Create ExtraData
503510
let extra = Arc::new(UnsafeCell::new(ExtraData {
504511
inner: MaybeUninit::uninit(),
@@ -2601,6 +2608,16 @@ impl Lua {
26012608
LuaRef::new(self, index)
26022609
}
26032610

2611+
#[inline]
2612+
pub(crate) unsafe fn push_error_traceback(&self) {
2613+
let state = self.state();
2614+
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
2615+
ffi::lua_xpush(self.ref_thread(), state, ExtraData::ERROR_TRACEBACK_IDX);
2616+
// Lua 5.2+ support light C functions that does not require extra allocations
2617+
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))]
2618+
ffi::lua_pushcfunction(state, error_traceback);
2619+
}
2620+
26042621
unsafe fn register_userdata_metatable<'lua, T: 'static>(
26052622
&'lua self,
26062623
mut registry: UserDataRegistry<'lua, T>,
@@ -3211,6 +3228,12 @@ impl LuaInner {
32113228
}
32123229
}
32133230

3231+
impl ExtraData {
3232+
// Index of `error_traceback` function in auxiliary thread stack
3233+
#[cfg(any(feature = "lua51", feature = "luajit", feature = "luau"))]
3234+
const ERROR_TRACEBACK_IDX: c_int = 1;
3235+
}
3236+
32143237
struct StateGuard<'a>(&'a LuaInner, *mut ffi::lua_State);
32153238

32163239
impl<'a> StateGuard<'a> {

0 commit comments

Comments
 (0)