Worker threads: leave one core, as vanilla does, not two

Graphics::worker_threads sizes the worker pools (the server thread's, which
generates and lights, and the window's, which meshes). Its automatic count
left two cores to the render and server threads; vanilla's executor leaves
one (Util.maxAllowedExecutorThreads). The workers run behind those threads
(niced on Linux; above four cores, raised server and render threads on
macOS and Windows), so the second core mostly idled: on a 4-thread laptop
only 2 cores generated while a world loaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 08:39:29 +01:00
parent fbfec6435f
commit 1437537cbf
+12 -8
View File
@@ -376,14 +376,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 }
}
@@ -546,11 +550,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);