mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
Reduce overhead of lua result tracking
This commit is contained in:
parent
ac8c809a20
commit
3e80a0a1c4
2 changed files with 19 additions and 17 deletions
|
@ -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");
|
let listener = {
|
||||||
if !self.inner.borrow().results.contains_key(&id) {
|
let inner = self.inner.borrow();
|
||||||
let listener = {
|
let event = inner.events.get(&id);
|
||||||
let mut inner = self.inner.borrow_mut();
|
event.map(Event::listen)
|
||||||
let event = inner
|
};
|
||||||
.events
|
listener.expect("Thread must be tracked").await;
|
||||||
.entry(id)
|
|
||||||
.or_insert_with(|| Rc::new(Event::new()));
|
|
||||||
event.listen()
|
|
||||||
};
|
|
||||||
listener.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)?;
|
||||||
|
|
|
@ -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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue