mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Rename MultiValue cache to pool
This commit is contained in:
+24
-26
@@ -105,8 +105,8 @@ pub(crate) struct ExtraData {
|
||||
|
||||
// Cache of `WrappedFailure` enums on the ref thread (as userdata)
|
||||
wrapped_failures_cache: Vec<c_int>,
|
||||
// Cache of recycled `MultiValue` containers
|
||||
multivalue_cache: Vec<MultiValue<'static>>,
|
||||
// Pool of `MultiValue` containers
|
||||
multivalue_pool: Vec<MultiValue<'static>>,
|
||||
// Cache of recycled `Thread`s (coroutines)
|
||||
#[cfg(feature = "async")]
|
||||
recycled_thread_cache: Vec<c_int>,
|
||||
@@ -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);
|
||||
|
||||
+7
-7
@@ -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<T, E> {
|
||||
#[inline]
|
||||
fn into_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
|
||||
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<MultiValue<'lua>> {
|
||||
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<Self> {
|
||||
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<T> DerefMut for Variadic<T> {
|
||||
impl<'lua, T: IntoLua<'lua>> IntoLuaMulti<'lua> for Variadic<T> {
|
||||
#[inline]
|
||||
fn into_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
|
||||
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<T> {
|
||||
.map(|e| T::from_lua(e, lua))
|
||||
.collect::<Result<Vec<T>>>()
|
||||
.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<MultiValue<'lua>> {
|
||||
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<Self> {
|
||||
lua.cache_multivalue(values);
|
||||
MultiValue::return_to_pool(values, lua);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
+8
-2
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user