From 0cb70584765433241df2a01a23fbe04816d3753e Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Fri, 30 Dec 2022 14:50:39 +0000 Subject: [PATCH] Rename MultiValue cache to pool --- src/lua.rs | 50 ++++++++++++++++++++++++-------------------------- src/multi.rs | 14 +++++++------- src/value.rs | 10 ++++++++-- 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/lua.rs b/src/lua.rs index 2b0cdf4..659878e 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -105,8 +105,8 @@ pub(crate) struct ExtraData { // Cache of `WrappedFailure` enums on the ref thread (as userdata) wrapped_failures_cache: Vec, - // Cache of recycled `MultiValue` containers - multivalue_cache: Vec>, + // Pool of `MultiValue` containers + multivalue_pool: Vec>, // Cache of recycled `Thread`s (coroutines) #[cfg(feature = "async")] recycled_thread_cache: Vec, @@ -227,7 +227,7 @@ pub(crate) static ASYNC_POLL_PENDING: u8 = 0; pub(crate) static EXTRA_REGISTRY_KEY: u8 = 0; const WRAPPED_FAILURES_CACHE_SIZE: usize = 32; -const MULTIVALUE_CACHE_SIZE: usize = 32; +const MULTIVALUE_POOL_SIZE: usize = 64; /// Requires `feature = "send"` #[cfg(feature = "send")] @@ -583,7 +583,7 @@ impl Lua { ref_stack_top, ref_free: Vec::new(), wrapped_failures_cache: Vec::with_capacity(WRAPPED_FAILURES_CACHE_SIZE), - multivalue_cache: Vec::with_capacity(MULTIVALUE_CACHE_SIZE), + multivalue_pool: Vec::with_capacity(MULTIVALUE_POOL_SIZE), #[cfg(feature = "async")] recycled_thread_cache: Vec::new(), wrapped_failure_mt_ptr, @@ -2670,7 +2670,7 @@ impl Lua { let lua: &Lua = mem::transmute((*extra).inner.as_ref().unwrap()); let _guard = StateGuard::new(&lua.0, state); - let mut args = MultiValue::new_or_cached(lua); + let mut args = MultiValue::new_or_pooled(lua); args.reserve(nargs as usize); for _ in 0..nargs { args.push_front(lua.pop_value()); @@ -2684,7 +2684,7 @@ impl Lua { for r in results.drain_all() { lua.push_value(r)?; } - lua.cache_multivalue(results); + MultiValue::return_to_pool(results, lua); Ok(nresults) }) @@ -2752,7 +2752,7 @@ impl Lua { let lua: &Lua = mem::transmute((*extra).inner.as_ref().unwrap()); let _guard = StateGuard::new(&lua.0, state); - let mut args = MultiValue::new_or_cached(lua); + let mut args = MultiValue::new_or_pooled(lua); args.reserve(nargs as usize); for _ in 0..nargs { args.push_front(lua.pop_value()); @@ -2971,25 +2971,6 @@ impl Lua { (*extra).inner.as_ref().map(|lua| Lua(Arc::clone(lua))) } - #[inline] - pub(crate) fn new_or_cached_multivalue(&self) -> MultiValue { - unsafe { - let extra = &mut *self.extra.get(); - extra.multivalue_cache.pop().unwrap_or_default() - } - } - - #[inline] - pub(crate) fn cache_multivalue(&self, mut multivalue: MultiValue) { - unsafe { - let extra = &mut *self.extra.get(); - if extra.multivalue_cache.len() < MULTIVALUE_CACHE_SIZE { - multivalue.clear(); - extra.multivalue_cache.push(mem::transmute(multivalue)); - } - } - } - #[inline] pub(crate) unsafe fn unlikely_memory_error(&self) -> bool { // MemoryInfo is empty in module mode so we cannot predict memory limits @@ -3010,6 +2991,23 @@ impl LuaInner { pub(crate) fn ref_thread(&self) -> *mut ffi::lua_State { unsafe { (*self.extra.get()).ref_thread } } + + #[inline] + pub(crate) fn new_multivalue_from_pool(&self) -> MultiValue { + let extra = unsafe { &mut *self.extra.get() }; + extra.multivalue_pool.pop().unwrap_or_default() + } + + #[inline] + pub(crate) fn return_multivalue_to_pool(&self, mut multivalue: MultiValue) { + let extra = unsafe { &mut *self.extra.get() }; + if extra.multivalue_pool.len() < MULTIVALUE_POOL_SIZE { + multivalue.clear(); + extra + .multivalue_pool + .push(unsafe { mem::transmute(multivalue) }); + } + } } struct StateGuard<'a>(&'a LuaInner, *mut ffi::lua_State); diff --git a/src/multi.rs b/src/multi.rs index b091561..507aac0 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -11,7 +11,7 @@ use crate::value::{FromLua, FromLuaMulti, IntoLua, IntoLuaMulti, MultiValue, Nil impl<'lua, T: IntoLua<'lua>, E: IntoLua<'lua>> IntoLuaMulti<'lua> for StdResult { #[inline] fn into_lua_multi(self, lua: &'lua Lua) -> Result> { - let mut result = MultiValue::new_or_cached(lua); + let mut result = MultiValue::new_or_pooled(lua); match self { Ok(v) => result.push_front(v.into_lua(lua)?), Err(e) => { @@ -26,7 +26,7 @@ impl<'lua, T: IntoLua<'lua>, E: IntoLua<'lua>> IntoLuaMulti<'lua> for StdResult< impl<'lua, T: IntoLua<'lua>> IntoLuaMulti<'lua> for T { #[inline] fn into_lua_multi(self, lua: &'lua Lua) -> Result> { - let mut v = MultiValue::new_or_cached(lua); + let mut v = MultiValue::new_or_pooled(lua); v.push_front(self.into_lua(lua)?); Ok(v) } @@ -36,7 +36,7 @@ impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for T { #[inline] fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result { let res = T::from_lua(values.pop_front().unwrap_or(Nil), lua); - lua.cache_multivalue(values); + MultiValue::return_to_pool(values, lua); res } } @@ -129,7 +129,7 @@ impl DerefMut for Variadic { impl<'lua, T: IntoLua<'lua>> IntoLuaMulti<'lua> for Variadic { #[inline] fn into_lua_multi(self, lua: &'lua Lua) -> Result> { - let mut values = MultiValue::new_or_cached(lua); + let mut values = MultiValue::new_or_pooled(lua); values.refill(self.0.into_iter().map(|e| e.into_lua(lua)))?; Ok(values) } @@ -143,7 +143,7 @@ impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for Variadic { .map(|e| T::from_lua(e, lua)) .collect::>>() .map(Variadic); - lua.cache_multivalue(values); + MultiValue::return_to_pool(values, lua); res } } @@ -153,14 +153,14 @@ macro_rules! impl_tuple { impl<'lua> IntoLuaMulti<'lua> for () { #[inline] fn into_lua_multi(self, lua: &'lua Lua) -> Result> { - Ok(MultiValue::new_or_cached(lua)) + Ok(MultiValue::new_or_pooled(lua)) } } impl<'lua> FromLuaMulti<'lua> for () { #[inline] fn from_lua_multi(values: MultiValue<'lua>, lua: &'lua Lua) -> Result { - lua.cache_multivalue(values); + MultiValue::return_to_pool(values, lua); Ok(()) } } diff --git a/src/value.rs b/src/value.rs index 204e5f3..bfd8dcf 100644 --- a/src/value.rs +++ b/src/value.rs @@ -203,8 +203,14 @@ impl<'lua> MultiValue<'lua> { /// Similar to `new` but can return previously used container with allocated capacity. #[inline] - pub(crate) fn new_or_cached(lua: &'lua Lua) -> MultiValue<'lua> { - lua.new_or_cached_multivalue() + pub(crate) fn new_or_pooled(lua: &'lua Lua) -> MultiValue<'lua> { + lua.new_multivalue_from_pool() + } + + /// Clears and returns previously allocated multivalue container to the pool. + #[inline] + pub(crate) fn return_to_pool(multivalue: Self, lua: &Lua) { + lua.return_multivalue_to_pool(multivalue); } }