From 770809908dc627c1a5409eb14252d4069e588c8b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 07:57:47 +0200 Subject: [PATCH 1/2] Render thread ahead of the workers on macOS and Windows The client asks for its render thread what it already asks for the server thread (vanilla's MinecraftServer.spin): user-interactive QoS on macOS, above-normal priority on Windows, on machines with more than four processors. While terrain loads at 32 chunks, the worker pools (two with single player's server thread), the chunk encoders and other programs outnumber the cores. A frame that gave its core up in queue.submit then waited 10 to 200 ms to get one back: the long frames of #678. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 7 +++++++ crates/horizon-threads/src/lib.rs | 9 +++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2df6aa9b0..2ff15febc 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -12756,6 +12756,13 @@ pub fn run() -> Result<()> { if options.dedicated.is_some() { // Before any worker starts: on Linux they go behind the server thread. horizon_threads::put_helpers_behind(); + } else { + // The render thread, like the server thread, ahead of the workers on + // macOS and Windows. While terrain loads, the worker pools (two with + // single player's server thread) and other programs outnumber the + // cores, and a frame that gave its core up (in a GPU submit) waited + // 10 to 200 ms to get one back. + horizon_threads::favour_current(); } let started = Instant::now(); horizon_client::spans::enable(options.spikes.is_some()); diff --git a/crates/horizon-threads/src/lib.rs b/crates/horizon-threads/src/lib.rs index d6cd135e0..6af6818cc 100644 --- a/crates/horizon-threads/src/lib.rs +++ b/crates/horizon-threads/src/lib.rs @@ -7,10 +7,11 @@ use std::sync::atomic::{AtomicBool, Ordering}; /// threads, as vanilla's `MinecraftServer.spin` does for its server thread /// (`Thread.setPriority(8)` when there are more than four processors), so /// world generation and chunk encoding on every other core don't hold a tick -/// back: user-interactive quality of service on macOS, above-normal priority -/// on Windows. Nothing elsewhere: Linux lets an unprivileged thread lower its -/// priority but not raise it (and the JVM leaves it there too). Whether the -/// OS took it. +/// back (the client asks the same for its render thread, so they don't hold +/// a frame back either): user-interactive quality of service on macOS, +/// above-normal priority on Windows. Nothing elsewhere: Linux lets an +/// unprivileged thread lower its priority but not raise it (and the JVM +/// leaves it there too). Whether the OS took it. pub fn favour_current() -> bool { if std::thread::available_parallelism().map_or(1, |n| n.get()) <= 4 { return false; From 41a4ce4edb15aa2abdaa902d847e7322d15e0e44 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 11:05:19 +0200 Subject: [PATCH 2/2] Linux: the game's workers go behind its render and server threads On Linux an unprivileged thread can't raise itself, so #1179's render thread hint does nothing there. The dedicated server already lowered its helper threads instead (world generation, lighting, connection writers: 10 nice levels, or the idle class). The client now does the same, so the world-generation and meshing workers (two pools with single player's server thread) no longer hold the render thread back while terrain loads. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/game.rs | 8 ++++---- crates/horizon-client/src/lan/conn.rs | 2 +- crates/horizon-client/src/streaming.rs | 2 +- crates/horizon-threads/src/lib.rs | 28 +++++++++++++------------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2ff15febc..c3b0a346b 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -12753,10 +12753,10 @@ pub fn run() -> Result<()> { if options.dedicated.is_some() && !dedicated::preflight(&mut options)? { return Ok(()); } - if options.dedicated.is_some() { - // Before any worker starts: on Linux they go behind the server thread. - horizon_threads::put_helpers_behind(); - } else { + // Before any worker starts: on Linux they go behind the server thread + // and the render thread. + horizon_threads::put_helpers_behind(); + if options.dedicated.is_none() { // The render thread, like the server thread, ahead of the workers on // macOS and Windows. While terrain loads, the worker pools (two with // single player's server thread) and other programs outnumber the diff --git a/crates/horizon-client/src/lan/conn.rs b/crates/horizon-client/src/lan/conn.rs index 7a92040f2..a8ec46dad 100644 --- a/crates/horizon-client/src/lan/conn.rs +++ b/crates/horizon-client/src/lan/conn.rs @@ -778,7 +778,7 @@ fn play(c: Connection, shared: &Arc, parts: Parts, address: std::net::So std::thread::Builder::new() .name(format!("horizon-lan-out-{conn_id}")) .spawn(move || { - // A dedicated server's tick comes first (horizon_threads). + // Ticks and frames come first (horizon_threads). horizon_threads::behind_server(); write_loop(writer, rx, alive, latency, pending) }) diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 858d05d30..5d5839539 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -436,7 +436,7 @@ impl Streaming { thread::Builder::new() .name(format!("horizon-worker-{i}")) .spawn(move || { - // A dedicated server's tick comes first (horizon_threads). + // Ticks and frames come first (horizon_threads). horizon_threads::behind_server(); let mut worker = generator.worker(); // The face visibility table and its occlusion cache stay with diff --git a/crates/horizon-threads/src/lib.rs b/crates/horizon-threads/src/lib.rs index 6af6818cc..ae4cc753f 100644 --- a/crates/horizon-threads/src/lib.rs +++ b/crates/horizon-threads/src/lib.rs @@ -41,30 +41,30 @@ fn favour() -> bool { false } -/// Set once by a dedicated server, before its helper threads start: they -/// then go behind its server thread (`behind_server`). +/// Set once as the game starts, before its helper threads do: they then go +/// behind the server thread and the render thread (`behind_server`). static HELPERS_BEHIND: AtomicBool = AtomicBool::new(false); /// How many nice levels a helper thread goes below the process on Linux. #[cfg(target_os = "linux")] const BEHIND: i32 = 10; -/// A dedicated server: from now on, the helper threads that call -/// `behind_server` as they start go behind the server thread. Call it -/// before they start. +/// From now on, the helper threads that call `behind_server` as they start +/// go behind the server thread (a dedicated server's or single player's) +/// and the render thread. Call it before they start. pub fn put_helpers_behind() { HELPERS_BEHIND.store(true, Ordering::Relaxed); } -/// Called by a helper thread as it starts (world generation and lighting, -/// a connection's packet writer): work the tick never waits for. In a -/// dedicated server (`put_helpers_behind`) on Linux, where the server thread -/// can't be raised (`favour_current`), the helper lowers itself instead, -/// which an unprivileged thread may: 10 nice levels below the process, or, -/// where nice runs out (a server started under nice 10 or more), the idle -/// class, which runs the thread only when no ordinary thread wants its core. -/// The server thread then gets a core when its tick starts instead of -/// waiting behind them. Nothing elsewhere. Whether the OS took it. +/// Called by a helper thread as it starts (world generation, lighting and +/// meshing, a connection's packet writer): work no tick or frame waits for. +/// Once `put_helpers_behind` was called, on Linux, where the server and +/// render threads can't be raised (`favour_current`), the helper lowers +/// itself instead, which an unprivileged thread may: 10 nice levels below +/// the process, or, where nice runs out (a game started under nice 10 or +/// more), the idle class, which runs the thread only when no ordinary thread +/// wants its core. A tick or a frame then gets a core when it starts instead +/// of waiting behind them. Nothing elsewhere. Whether the OS took it. pub fn behind_server() -> bool { HELPERS_BEHIND.load(Ordering::Relaxed) && behind() }