Reduce overhead of lua result tracking

This commit is contained in:
Filip Tibell 2025-04-29 23:31:10 +02:00
parent ac8c809a20
commit 3e80a0a1c4
No known key found for this signature in database
2 changed files with 19 additions and 17 deletions

View file

@ -13,7 +13,7 @@ use crate::thread_id::ThreadId;
struct ThreadResultMapInner { struct ThreadResultMapInner {
tracked: FxHashSet<ThreadId>, tracked: FxHashSet<ThreadId>,
results: FxHashMap<ThreadId, LuaResult<LuaMultiValue>>, results: FxHashMap<ThreadId, LuaResult<LuaMultiValue>>,
events: FxHashMap<ThreadId, Rc<Event>>, events: FxHashMap<ThreadId, Event>,
} }
impl ThreadResultMapInner { impl ThreadResultMapInner {
@ -39,7 +39,9 @@ impl ThreadResultMap {
#[inline(always)] #[inline(always)]
pub fn track(&self, id: ThreadId) { pub fn track(&self, id: ThreadId) {
self.inner.borrow_mut().tracked.insert(id); let mut inner = self.inner.borrow_mut();
inner.tracked.insert(id);
inner.events.insert(id, Event::new());
} }
#[inline(always)] #[inline(always)]
@ -47,6 +49,7 @@ impl ThreadResultMap {
self.inner.borrow().tracked.contains(&id) self.inner.borrow().tracked.contains(&id)
} }
#[inline(always)]
pub fn insert(&self, id: ThreadId, result: LuaResult<LuaMultiValue>) { pub fn insert(&self, id: ThreadId, result: LuaResult<LuaMultiValue>) {
debug_assert!(self.is_tracked(id), "Thread must be tracked"); debug_assert!(self.is_tracked(id), "Thread must be tracked");
let mut inner = self.inner.borrow_mut(); let mut inner = self.inner.borrow_mut();
@ -56,21 +59,17 @@ impl ThreadResultMap {
} }
} }
#[inline(always)]
pub async fn listen(&self, id: ThreadId) { pub async fn listen(&self, id: ThreadId) {
debug_assert!(self.is_tracked(id), "Thread must be tracked");
if !self.inner.borrow().results.contains_key(&id) {
let listener = { let listener = {
let mut inner = self.inner.borrow_mut(); let inner = self.inner.borrow();
let event = inner let event = inner.events.get(&id);
.events event.map(Event::listen)
.entry(id)
.or_insert_with(|| Rc::new(Event::new()));
event.listen()
}; };
listener.await; listener.expect("Thread must be tracked").await;
}
} }
#[inline(always)]
pub fn remove(&self, id: ThreadId) -> Option<LuaResult<LuaMultiValue>> { pub fn remove(&self, id: ThreadId) -> Option<LuaResult<LuaMultiValue>> {
let mut inner = self.inner.borrow_mut(); let mut inner = self.inner.borrow_mut();
let res = inner.results.remove(&id)?; let res = inner.results.remove(&id)?;

View file

@ -1,4 +1,7 @@
use std::hash::{Hash, Hasher}; use std::{
ffi::c_void,
hash::{Hash, Hasher},
};
use mlua::prelude::*; use mlua::prelude::*;
@ -12,13 +15,13 @@ use mlua::prelude::*;
*/ */
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ThreadId { pub struct ThreadId {
inner: usize, inner: *const c_void,
} }
impl From<&LuaThread> for ThreadId { impl From<&LuaThread> for ThreadId {
fn from(thread: &LuaThread) -> Self { fn from(thread: &LuaThread) -> Self {
Self { Self {
inner: thread.to_pointer() as usize, inner: thread.to_pointer(),
} }
} }
} }