From 04c076314664e73f1cfc84c0412bd784ffcadaae Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Thu, 12 Oct 2023 09:52:34 +0100 Subject: [PATCH] Optimize iterating over array part of table --- src/table.rs | 45 ++++++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/src/table.rs b/src/table.rs index c59affe..b69ff1a 100644 --- a/src/table.rs +++ b/src/table.rs @@ -720,7 +720,7 @@ impl<'lua> Table<'lua> { pub fn sequence_values>(self) -> TableSequence<'lua, V> { TableSequence { table: self.0, - index: Some(1), + index: 1, len: None, _phantom: PhantomData, } @@ -740,7 +740,7 @@ impl<'lua> Table<'lua> { let len = len.unwrap_or_else(|| self.raw_len()) as Integer; TableSequence { table: self.0, - index: Some(1), + index: 1, len: Some(len), _phantom: PhantomData, } @@ -1193,7 +1193,7 @@ where /// [`Table::sequence_values`]: crate::Table::sequence_values pub struct TableSequence<'lua, V> { table: LuaRef<'lua>, - index: Option, + index: Integer, len: Option, _phantom: PhantomData, } @@ -1205,31 +1205,22 @@ where type Item = Result; fn next(&mut self) -> Option { - if let Some(index) = self.index.take() { - let lua = self.table.lua; - let state = lua.state(); - - let res = (|| unsafe { - let _sg = StackGuard::new(state); - check_stack(state, 1)?; - - lua.push_ref(&self.table); - match ffi::lua_rawgeti(state, -1, index) { - ffi::LUA_TNIL if index > self.len.unwrap_or(0) => Ok(None), - _ => Ok(Some((index, lua.pop_value()))), - } - })(); - - match res { - Ok(Some((index, r))) => { - self.index = Some(index + 1); - Some(V::from_lua(r, lua)) - } - Ok(None) => None, - Err(err) => Some(Err(err)), + let lua = self.table.lua; + let state = lua.state(); + unsafe { + let _sg = StackGuard::new(state); + if let Err(err) = check_stack(state, 1) { + return Some(Err(err)); + } + + lua.push_ref(&self.table); + match ffi::lua_rawgeti(state, -1, self.index) { + ffi::LUA_TNIL if self.index > self.len.unwrap_or(0) => None, + _ => { + self.index += 1; + Some(V::from_stack(-1, lua)) + } } - } else { - None } } }