[PR #970] [MERGED] Server thread: ahead of the workers on macOS and Windows, as vanilla's spin #2952

Closed
opened 2026-11-02 16:00:22 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/970
Author: @ludovic111
Created: 10/19/2026
Status: ✅ Merged
Merged: 10/19/2026
Merged by: @ludovic111

Base: main ← Head: perf/server-thread-first


📝 Commits (1)

  • 0e025df Server thread: ahead of the workers on macOS and Windows, as vanilla's spin

📊 Changes

7 files changed (+98 additions, -1 deletions)

View changed files

📝 Cargo.lock (+9 -0)
📝 Cargo.toml (+1 -1)
📝 crates/horizon-client/Cargo.toml (+2 -0)
📝 crates/horizon-client/src/game/dedicated.rs (+2 -0)
📝 crates/horizon-client/src/game/integrated.rs (+2 -0)
➕ crates/horizon-threads/Cargo.toml (+16 -0)
➕ crates/horizon-threads/src/lib.rs (+66 -0)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

What

The thread that ticks the server (the dedicated server's, and single player's Server thread) now asks the OS to run ahead of the process's other threads, as vanilla does:

  • MinecraftServer.spin calls thread.setPriority(8) on its server thread when the machine has more than four processors. The new horizon-threads crate makes the same request: THREAD_PRIORITY_ABOVE_NORMAL on Windows (what HotSpot maps priority 8 to), user-interactive QoS on macOS (pthread_set_qos_class_self_np, macOS's own way to put a thread ahead; HotSpot raises the thread's pthread priority there). On Linux it does nothing: an unprivileged thread can lower its priority but not raise it, and the JVM leaves Java priorities alone there too.
  • horizon-threads exists because horizon-client forbids unsafe code. It holds these two calls and nothing else (libc and windows-sys 0.61 were already in the lock file; no new crate from crates.io).
  • Called once at the top of run_dedicated and at the start of the integrated server's thread. Nothing else changes: same tick order, same work.

Why

Brief perf-server-11, task 1: in the worst ticks the server thread had far more wall time than CPU time. On the Mac mini with 40 explore bots, the tick's wall time was 4x its CPU time: 12 generation workers and 13 chunk-encode helpers at the same priority, plus the other sessions on the machine, kept it waiting for a core. I tried the other way round first (generation workers and helpers at utility QoS): ticks got short too, but the workers then lost the cores to the other processes and the bots got 60 to 90 % fewer chunks a second. Raising the server thread alone keeps the chunk rate exactly the same.

Numbers

Mac mini (M4 Pro, 14 threads, 80–95 % busy with other sessions), tools/server_bench.py --servers horizon --bots 40, HORIZON_SPIKES=0 so every tick is logged with its wall and server-thread CPU time; first 400 ticks skipped. Explore bots at --explore-speed 5, so both builds do the same work (same chunks/s). base = main with #915, #934, #914 and #927 (2403eca8); new = base + this PR. Runs alternated base/new/base/new.

40 bots tick wall p50 / p99 / max (ms) server CPU p99 ticks > 50 ms MSPT avg / p95 / p99 chunks/s
explore 1, base 18.5 / 101.5 / 762 33.7 166 26.4 / 59.6 / 102.3 242.7
explore 1, new 14.4 / 32.8 / 607 32.7 1 17.7 / 24.6 / 31.8 242.7
explore 2, base 17.3 / 77.7 / 1320 38.8 146 25.0 / 46.5 / 77.5 243.0
explore 2, new 15.0 / 32.7 / 599 32.6 2 18.2 / 25.8 / 38.2 242.8
walk, base 3.0 / 17.3 / 46.2 8.4 0 5.2 / 12.0 / 19.7 –
walk, new 2.6 / 3.7 / 11.6 3.7 0 3.0 / 3.8 / 24.2 –

With the server thread first, a tick's wall time equals its CPU time (p99 32.8 vs 32.7 ms): it no longer waits for a core. An earlier set of runs with an experiment build (same machine, same bench) agreed: tick p99 117 and 154 ms without, 31.7 and 33.3 ms with. The walk MSPT p99 of 24.2 is one /tick query sample (they cover 100-tick windows every 5 s); the per-tick log of that run has p99 3.7 and max 11.6 ms.

The ~600 ms max that is left is one tick, the last one, when the bots leave and their chunks unload: the sculk index dropped them one chunk at a time. #972 fixes it (max about 40 ms with it); #971 moves the heightmaps off this thread.

ThinkPad (Linux, 4 threads): no change, by design (nothing to call on Linux, and vanilla only raises with more than four processors).

Checks

  • cargo fmt --all --check, cargo clippy --workspace --all-targets -- -D warnings: clean. cargo clippy -p horizon-threads --all-targets --target x86_64-pc-windows-msvc and --target x86_64-unknown-linux-gnu -- -D warnings: clean.
  • cargo test -p horizon-client -p horizon-threads: 896 lib + 77 bin tests and the rest pass. The new favouring_a_thread_takes_where_the_os_allows_it checks that the call succeeds on macOS/Windows, does nothing elsewhere, and reads the QoS class back on macOS.
  • Integration commit ae52589e (origin/main c569bbe2 + this PR + the heightmaps, sculk and lan_e2e PRs): parity chunk_stage, dedicated_server, heightmap, lan, sculk, server_entities, server_world_gen, world_reopen: 8 passed, 0 failed. lan_e2e: with and without --server-thread, nothing fails that doesn't fail on main on the same machine (main fails swings every time, self and crafting with --server-thread, and now and then sign, effects, hurt or projectiles while the other sessions keep the Mac mini busy). With this PR, --server-thread's pvp, hurt, respawn and sleep checks first failed in most runs: the server thread now ticks on time, and the guest died in the hazards check's void before its cleanup commands. #968 fixes that harness race; with it, --check hazards pvp hurt respawn sleep passed 3 times out of 3.

Doesn't touch game.rs tick order (server-ticks-2): only the thread's priority.

Needs Ludovic

Nothing. Windows builds only at release time: the Windows code is two windows-sys calls, checked with cargo clippy --target x86_64-pc-windows-msvc.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/970 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/19/2026 **Status:** ✅ Merged **Merged:** 10/19/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/server-thread-first` --- ### 📝 Commits (1) - [`0e025df`](https://github.com/horizon-rust/horizon/commit/0e025df8e1869ea604842799b4d89b0065e4b10f) Server thread: ahead of the workers on macOS and Windows, as vanilla's spin ### 📊 Changes **7 files changed** (+98 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `Cargo.lock` (+9 -0) 📝 `Cargo.toml` (+1 -1) 📝 `crates/horizon-client/Cargo.toml` (+2 -0) 📝 `crates/horizon-client/src/game/dedicated.rs` (+2 -0) 📝 `crates/horizon-client/src/game/integrated.rs` (+2 -0) ➕ `crates/horizon-threads/Cargo.toml` (+16 -0) ➕ `crates/horizon-threads/src/lib.rs` (+66 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What The thread that ticks the server (the dedicated server's, and single player's `Server thread`) now asks the OS to run ahead of the process's other threads, as vanilla does: - `MinecraftServer.spin` calls `thread.setPriority(8)` on its server thread when the machine has more than four processors. The new `horizon-threads` crate makes the same request: `THREAD_PRIORITY_ABOVE_NORMAL` on Windows (what HotSpot maps priority 8 to), user-interactive QoS on macOS (`pthread_set_qos_class_self_np`, macOS's own way to put a thread ahead; HotSpot raises the thread's pthread priority there). On Linux it does nothing: an unprivileged thread can lower its priority but not raise it, and the JVM leaves Java priorities alone there too. - `horizon-threads` exists because `horizon-client` forbids unsafe code. It holds these two calls and nothing else (libc and windows-sys 0.61 were already in the lock file; no new crate from crates.io). - Called once at the top of `run_dedicated` and at the start of the integrated server's thread. Nothing else changes: same tick order, same work. ## Why Brief perf-server-11, task 1: in the worst ticks the server thread had far more wall time than CPU time. On the Mac mini with 40 explore bots, the tick's wall time was 4x its CPU time: 12 generation workers and 13 chunk-encode helpers at the same priority, plus the other sessions on the machine, kept it waiting for a core. I tried the other way round first (generation workers and helpers at utility QoS): ticks got short too, but the workers then lost the cores to the other processes and the bots got 60 to 90 % fewer chunks a second. Raising the server thread alone keeps the chunk rate exactly the same. ## Numbers Mac mini (M4 Pro, 14 threads, 80–95 % busy with other sessions), `tools/server_bench.py --servers horizon --bots 40`, `HORIZON_SPIKES=0` so every tick is logged with its wall and server-thread CPU time; first 400 ticks skipped. Explore bots at `--explore-speed 5`, so both builds do the same work (same chunks/s). base = main with #915, #934, #914 and #927 (2403eca8); new = base + this PR. Runs alternated base/new/base/new. | 40 bots | tick wall p50 / p99 / max (ms) | server CPU p99 | ticks > 50 ms | MSPT avg / p95 / p99 | chunks/s | |---|---|---|---|---|---| | explore 1, base | 18.5 / 101.5 / 762 | 33.7 | 166 | 26.4 / 59.6 / 102.3 | 242.7 | | explore 1, new | 14.4 / **32.8** / 607 | 32.7 | **1** | **17.7 / 24.6 / 31.8** | 242.7 | | explore 2, base | 17.3 / 77.7 / 1320 | 38.8 | 146 | 25.0 / 46.5 / 77.5 | 243.0 | | explore 2, new | 15.0 / **32.7** / 599 | 32.6 | **2** | **18.2 / 25.8 / 38.2** | 242.8 | | walk, base | 3.0 / 17.3 / 46.2 | 8.4 | 0 | 5.2 / 12.0 / 19.7 | – | | walk, new | 2.6 / **3.7** / 11.6 | 3.7 | 0 | 3.0 / 3.8 / 24.2 | – | With the server thread first, a tick's wall time equals its CPU time (p99 32.8 vs 32.7 ms): it no longer waits for a core. An earlier set of runs with an experiment build (same machine, same bench) agreed: tick p99 117 and 154 ms without, 31.7 and 33.3 ms with. The walk MSPT p99 of 24.2 is one `/tick query` sample (they cover 100-tick windows every 5 s); the per-tick log of that run has p99 3.7 and max 11.6 ms. The ~600 ms max that is left is one tick, the last one, when the bots leave and their chunks unload: the sculk index dropped them one chunk at a time. #972 fixes it (max about 40 ms with it); #971 moves the heightmaps off this thread. ThinkPad (Linux, 4 threads): no change, by design (nothing to call on Linux, and vanilla only raises with more than four processors). ## Checks - `cargo fmt --all --check`, `cargo clippy --workspace --all-targets -- -D warnings`: clean. `cargo clippy -p horizon-threads --all-targets --target x86_64-pc-windows-msvc` and `--target x86_64-unknown-linux-gnu -- -D warnings`: clean. - `cargo test -p horizon-client -p horizon-threads`: 896 lib + 77 bin tests and the rest pass. The new `favouring_a_thread_takes_where_the_os_allows_it` checks that the call succeeds on macOS/Windows, does nothing elsewhere, and reads the QoS class back on macOS. - Integration commit ae52589e (origin/main c569bbe2 + this PR + the heightmaps, sculk and lan_e2e PRs): parity `chunk_stage`, `dedicated_server`, `heightmap`, `lan`, `sculk`, `server_entities`, `server_world_gen`, `world_reopen`: 8 passed, 0 failed. lan_e2e: with and without `--server-thread`, nothing fails that doesn't fail on main on the same machine (main fails `swings` every time, `self` and `crafting` with `--server-thread`, and now and then `sign`, `effects`, `hurt` or `projectiles` while the other sessions keep the Mac mini busy). With this PR, `--server-thread`'s pvp, hurt, respawn and sleep checks first failed in most runs: the server thread now ticks on time, and the guest died in the hazards check's void before its cleanup commands. #968 fixes that harness race; with it, `--check hazards pvp hurt respawn sleep` passed 3 times out of 3. Doesn't touch `game.rs` tick order (server-ticks-2): only the thread's priority. ## Needs Ludovic Nothing. Windows builds only at release time: the Windows code is two windows-sys calls, checked with `cargo clippy --target x86_64-pc-windows-msvc`. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 16:00:22 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#2952
No description provided.