Merge pull request #1778 from ludovic111/perf/worker-count

🤖 Agent: verify and parity pass on 1437537cb; spawn load −15% and teleports −12% on a 4-thread laptop (measurements in the body). Queued by perf-next-2.
This commit is contained in:
Ludovic Marie
2026-10-26 00:29:11 +01:00
committed by GitHub
+12 -8
View File
@@ -385,14 +385,18 @@ impl Graphics {
if self.workers > 0 {
return self.workers as usize;
}
// The pool never takes every core. The render thread and the
// integrated server thread (single player runs on its own server
// thread by default) each need one, and a pool that fills every core
// starves them: the frame rate collapses to single digits while the
// workers saturate the CPU and its memory bandwidth.
// Vanilla's count (Util.maxAllowedExecutorThreads: every core but
// one). The pool never takes every core: a pool that fills them all
// starves the render and server threads, and the frame rate collapses
// while the workers saturate the CPU and its memory bandwidth. One
// core left is enough with the workers behind those two threads
// (horizon_threads: niced on Linux; on macOS and Windows the server
// and render threads are raised above four cores, where vanilla
// raises its server thread). Leaving two idled a core of a 4-thread
// laptop while a world loaded: a teleport there took ~15% longer.
let auto = std::thread::available_parallelism()
.map_or(2, |n| n.get())
.saturating_sub(2)
.saturating_sub(1)
.max(1);
if self.battery { auto.div_ceil(2) } else { auto }
}
@@ -555,11 +559,11 @@ mod tests {
}
#[test]
fn automatic_workers_leave_cores_for_the_render_and_server_threads() {
fn automatic_workers_leave_a_core_as_vanilla_does() {
let g = Graphics::default();
assert_eq!(g.workers, 0);
let cores = std::thread::available_parallelism().map_or(2, |n| n.get());
assert_eq!(g.worker_threads(), cores.saturating_sub(2).max(1));
assert_eq!(g.worker_threads(), cores.saturating_sub(1).max(1));
// A manual count is the player's own choice, kept as asked.
let manual = Graphics { workers: 3, ..g };
assert_eq!(manual.worker_threads(), 3);