mirror of
https://github.com/lune-org/mlua-luau-scheduler.git
synced 2025-04-10 13:30:54 +01:00
More speed
This commit is contained in:
parent
6a2c2f588e
commit
134996bee6
1 changed files with 10 additions and 17 deletions
27
src/main.rs
27
src/main.rs
|
@ -8,8 +8,8 @@ use tokio::{
|
||||||
runtime::Runtime as TokioRuntime,
|
runtime::Runtime as TokioRuntime,
|
||||||
select, spawn,
|
select, spawn,
|
||||||
sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
|
sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
|
||||||
task::LocalSet,
|
task::{spawn_blocking, LocalSet},
|
||||||
time::{interval, sleep, Instant, MissedTickBehavior},
|
time::{sleep, Instant},
|
||||||
};
|
};
|
||||||
|
|
||||||
const NUM_TEST_BATCHES: usize = 20;
|
const NUM_TEST_BATCHES: usize = 20;
|
||||||
|
@ -43,14 +43,15 @@ fn main() {
|
||||||
let (lua_tx, msg_rx) = unbounded_channel::<RuntimeMessage>();
|
let (lua_tx, msg_rx) = unbounded_channel::<RuntimeMessage>();
|
||||||
|
|
||||||
set.block_on(&rt, async {
|
set.block_on(&rt, async {
|
||||||
select! {
|
// TODO: Handle result
|
||||||
_ = set.spawn_local(lua_main(lua_rx, lua_tx)) => {},
|
let _ = select! {
|
||||||
_ = spawn(sched_main(msg_rx, msg_tx)) => {},
|
r = spawn_blocking(|| lua_main(lua_rx, lua_tx)) => r,
|
||||||
}
|
r = spawn(sched_main(msg_rx, msg_tx)) => r,
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn lua_main(mut rx: RuntimeReceiver, tx: RuntimeSender) -> LuaResult<()> {
|
fn lua_main(mut rx: RuntimeReceiver, tx: RuntimeSender) -> LuaResult<()> {
|
||||||
let lua = Lua::new();
|
let lua = Lua::new();
|
||||||
let g = lua.globals();
|
let g = lua.globals();
|
||||||
|
|
||||||
|
@ -90,9 +91,6 @@ async fn lua_main(mut rx: RuntimeReceiver, tx: RuntimeSender) -> LuaResult<()> {
|
||||||
|
|
||||||
let before = Instant::now();
|
let before = Instant::now();
|
||||||
|
|
||||||
let mut throttle = interval(Duration::from_millis(5));
|
|
||||||
throttle.set_missed_tick_behavior(MissedTickBehavior::Delay);
|
|
||||||
|
|
||||||
for n in 1..=NUM_TEST_BATCHES {
|
for n in 1..=NUM_TEST_BATCHES {
|
||||||
println!("Running batch {n} of {NUM_TEST_BATCHES}");
|
println!("Running batch {n} of {NUM_TEST_BATCHES}");
|
||||||
|
|
||||||
|
@ -108,12 +106,6 @@ async fn lua_main(mut rx: RuntimeReceiver, tx: RuntimeSender) -> LuaResult<()> {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Limit this loop to a maximum of 200hz, this lets us improve performance
|
|
||||||
// by batching more work and not switching between running threads and waiting
|
|
||||||
// for the next message as often. It may however add another 5 milliseconds of
|
|
||||||
// latency to something like a web server, but the tradeoff is worth it.
|
|
||||||
throttle.tick().await;
|
|
||||||
|
|
||||||
// Resume as many threads as possible
|
// Resume as many threads as possible
|
||||||
for (thread_id, thread) in runnable_threads.drain() {
|
for (thread_id, thread) in runnable_threads.drain() {
|
||||||
thread.resume(())?;
|
thread.resume(())?;
|
||||||
|
@ -139,7 +131,7 @@ async fn lua_main(mut rx: RuntimeReceiver, tx: RuntimeSender) -> LuaResult<()> {
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
if let Some(message) = rx.recv().await {
|
if let Some(message) = rx.blocking_recv() {
|
||||||
process_message(message);
|
process_message(message);
|
||||||
while let Ok(message) = rx.try_recv() {
|
while let Ok(message) = rx.try_recv() {
|
||||||
process_message(message);
|
process_message(message);
|
||||||
|
@ -151,6 +143,7 @@ async fn lua_main(mut rx: RuntimeReceiver, tx: RuntimeSender) -> LuaResult<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let after = Instant::now();
|
let after = Instant::now();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"Ran {} threads in {:?}",
|
"Ran {} threads in {:?}",
|
||||||
NUM_TEST_BATCHES * NUM_TEST_THREADS,
|
NUM_TEST_BATCHES * NUM_TEST_THREADS,
|
||||||
|
|
Loading…
Add table
Reference in a new issue