Skip to content

Commit 3e87d3e

Browse files
committed
Optimize Table::is_empty
1 parent 0f8bde4 commit 3e87d3e

2 files changed

Lines changed: 7 additions & 17 deletions

File tree

src/state/extra.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use super::{Lua, WeakLua};
2828
static EXTRA_REGISTRY_KEY: u8 = 0;
2929

3030
const WRAPPED_FAILURE_POOL_DEFAULT_CAPACITY: usize = 64;
31-
const REF_STACK_RESERVE: c_int = 1;
31+
const REF_STACK_RESERVE: c_int = 2;
3232

3333
/// Data associated with the Lua state.
3434
pub(crate) struct ExtraData {

src/table.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -468,26 +468,16 @@ impl Table {
468468
///
469469
/// It checks both the array part and the hash part.
470470
pub fn is_empty(&self) -> bool {
471-
// Check array part
472-
if self.raw_len() != 0 {
473-
return false;
474-
}
475-
476-
// Check hash part
477471
let lua = self.0.lua.lock();
478-
let state = lua.state();
472+
let ref_thread = lua.ref_thread();
479473
unsafe {
480-
let _sg = StackGuard::new(state);
481-
assert_stack(state, 4);
482-
483-
lua.push_ref(&self.0);
484-
ffi::lua_pushnil(state);
485-
if ffi::lua_next(state, -2) != 0 {
486-
return false;
474+
ffi::lua_pushnil(ref_thread);
475+
if ffi::lua_next(ref_thread, self.0.index) == 0 {
476+
return true;
487477
}
478+
ffi::lua_pop(ref_thread, 2);
488479
}
489-
490-
true
480+
false
491481
}
492482

493483
/// Returns a reference to the metatable of this table, or `None` if no metatable is set.

0 commit comments

Comments
 (0)