Don't do excessive busy-waiting.

This commit is contained in:
IQuant 2024-07-31 15:11:46 +03:00
parent 2ed8ce58e1
commit ae50e35f63
2 changed files with 13 additions and 1 deletions

View file

@ -16,6 +16,9 @@ build:
run-rel: add_dylib_release run-rel: add_dylib_release
cd noita-proxy && NP_APPID=480 NP_SKIP_MOD_CHECK=1 cargo run --release cd noita-proxy && NP_APPID=480 NP_SKIP_MOD_CHECK=1 cargo run --release
flamegraph: add_dylib_debug
cd noita-proxy && NP_APPID=480 NP_SKIP_MOD_CHECK=1 cargo flamegraph
run: add_dylib_debug run: add_dylib_debug
cd noita-proxy && NP_APPID=480 NP_SKIP_MOD_CHECK=1 cargo run cd noita-proxy && NP_APPID=480 NP_SKIP_MOD_CHECK=1 cargo run

View file

@ -9,7 +9,7 @@ use std::{
net::{SocketAddr, TcpListener, TcpStream}, net::{SocketAddr, TcpListener, TcpStream},
sync::{atomic::AtomicBool, Arc, Mutex}, sync::{atomic::AtomicBool, Arc, Mutex},
thread, thread,
time::Duration, time::{Duration, Instant},
}; };
use tracing::debug; use tracing::debug;
use world::{world_info::WorldInfo, NoitaWorldUpdate, WorldManager}; use world::{world_info::WorldInfo, NoitaWorldUpdate, WorldManager};
@ -160,6 +160,8 @@ impl NetManager {
world: WorldManager::new(self.is_host(), self.peer.my_id().unwrap()), world: WorldManager::new(self.is_host(), self.peer.my_id().unwrap()),
}; };
let mut last_iter = Instant::now();
while self while self
.continue_running .continue_running
.load(std::sync::atomic::Ordering::Relaxed) .load(std::sync::atomic::Ordering::Relaxed)
@ -271,6 +273,13 @@ impl NetManager {
for update in updates { for update in updates {
state.try_ws_write(ws_encode_proxy_bin(0, &update)); state.try_ws_write(ws_encode_proxy_bin(0, &update));
} }
// Don't do excessive busy-waiting;
let min_update_time = Duration::from_millis(1);
let elapsed = last_iter.elapsed();
if elapsed < min_update_time {
thread::sleep(min_update_time - elapsed);
}
last_iter = Instant::now();
} }
Ok(()) Ok(())
} }