From 65396a910ff955ee011a979e266e5835cc566701 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Tue, 18 Oct 2022 01:09:35 +0100 Subject: [PATCH] Optimize `Lua::create_table` to use reference thread if possible --- src/lua.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index b0d2ad3..65c1278 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -1457,11 +1457,15 @@ impl Lua { /// Lua may use these hints to preallocate memory for the new table. pub fn create_table_with_capacity(&self, narr: c_int, nrec: c_int) -> Result { unsafe { + if self.unlikely_memory_error() { + push_table(self.ref_thread(), narr, nrec, false)?; + return Ok(Table(self.pop_ref_thread())); + } + let _sg = StackGuard::new(self.state); check_stack(self.state, 3)?; - let protect = !self.unlikely_memory_error(); - push_table(self.state, narr, nrec, protect)?; + push_table(self.state, narr, nrec, true)?; Ok(Table(self.pop_ref())) } }