[PR #737] [MERGED] LAN: wake the writer threads from one post thread, once per tick #2769

Closed
opened 2026-11-02 15:58:54 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: main ← Head: perf/lan-post-thread


📝 Commits (1)

  • d99cf24 LAN: wake the writer threads from one post thread, once per tick

📊 Changes

3 files changed (+126 additions, -15 deletions)

View changed files

📝 crates/horizon-client/src/game/lan.rs (+1 -3)
📝 crates/horizon-client/src/lan/conn.rs (+124 -11)
📝 crates/horizon-client/src/lan/mod.rs (+1 -1)

📄 Description

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

What: at the end of each tick the game thread handed every player's held packets to that player's writer thread (PlayerSender::flush per guest), waking each one. Each wake is a system call (semaphore_signal), and on a busy machine the woken writer often takes the rest of the game thread's time slice. Now lan::flush_all gives every batch to one post thread (horizon-lan-post) in a single channel send, and that thread wakes the writers. With 50 players, that is one wake a tick from the game thread instead of 50.

Order is kept. Until the post thread has handed a player's batch over, a packet sent straight to that player (outside a tick) goes through the post thread too. Each player's packets keep their order, also against what is sent to it next. New test: packets_sent_after_a_posted_tick_follow_it (50 rounds of tick, flush, then immediate send and disconnect, two players). If the post thread can't be started, batches are handed over on the calling thread, as before.

Measured (dedicated server, 50 walking bots, seed 12345, view distance 10, Mac mini M4 Pro):

The flush step on the server thread, from a local probe (Instant around the step, every tick, the 900 ticks of the measured window). Base and this PR ran at the same time, sides swapped in round 2:

per tick base r1 this PR r1 base r2 this PR r2
flush step, mean 1624 µs 601 µs 2271 µs 601 µs
flush step, median 587 µs 10 µs 143 µs 9 µs
flush step, 90th percentile 4682 µs 2132 µs 7277 µs 22 µs

Share of the server thread (macOS sample, 20 s in the measured window, two runs each):

base base this PR this PR
PlayerSender::flush 19.3 % 12.6 % 0.0 % 0.0 %

The post thread itself was asleep 97 % of the time (its 50 wakes a tick).

MSPT (/tick query average), base and this PR at the same time:

base this PR
round 1 17.35 ms 16.76 ms
round 2 13.61 ms 13.87 ms
round 3 6.60 ms 6.57 ms
round 4 (probe builds) 7.82 ms 8.44 ms
round 5 (probe builds) 10.86 ms 11.32 ms

MSPT doesn't move outside the noise on this machine. The server thread used only about 3 ms of CPU a tick (ps -M); with a load average of 60 to 170 on 14 threads, most of a tick's wall time went to waiting for a core, and the 1 to 1.7 ms a tick this removes is below that spread. The probe and the sampled share are the load-independent numbers.

How it was measured

  • Mac mini (M4 Pro, 14 threads, 64 GB), shared with other agents' builds and parity runs: ~/fleet/load showed the CPU 72 to 92 % busy with a load average of 56 to 170 through the runs.
  • tools/server_bench.py --servers horizon --behaviour walk --bots 50 --warmup 30 --seconds 45 --gamerule locator_bar=false: seed 12345, view and simulation distance 10, bots on the same machine, fast profile, base = main 7bbbf6e9. Every run sent the bots the same chunks (490 to 505 a second, none sent twice).
  • Paired runs: the base and the new build ran at the same time (two servers, 50 bots each), so both saw the same load; they swapped worktrees between rounds.
  • Probe: a local patch, not part of this PR, timed the step with Instant on the server thread and logged one line per event. The numbers cover the 900 ticks of the measured window. The steps are much shorter than a scheduler time slice, so their medians are close to CPU time even on a loaded machine.
  • Share of the server thread: macOS sample of the engine for 20 s inside the measured window of a separate run per build. It counts wall time, so time spent waiting for a core counts too.
  • MSPT (/tick query average) didn't separate the builds on this machine. The server thread used only about 3 ms of CPU a tick (ps -M); with load averages up to 170 on 14 threads, most of a tick's wall time went to waiting for a core, and that spread is larger than these changes.

Checks

  • cargo fmt --all --check: ok
  • cargo clippy --workspace --all-targets -- -D warnings: ok
  • cargo test -p horizon-client --lib: 824 passed, 0 failed, 11 ignored
  • Local parity (tools/parity.py --changed origin/main, one run on #737, #738, #739 and #748 stacked together, plus the chunk-occupancy commit on perf/chunk-occupied-columns): 87 passed, 0 failed (every harness that depends on the 9 changed files).

Nothing here needs Ludovic.

🤖 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/737 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/17/2026 **Status:** ✅ Merged **Merged:** 10/17/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/lan-post-thread` --- ### 📝 Commits (1) - [`d99cf24`](https://github.com/horizon-rust/horizon/commit/d99cf24400828dda16abeb141740b157c1059345) LAN: wake the writer threads from one post thread, once per tick ### 📊 Changes **3 files changed** (+126 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game/lan.rs` (+1 -3) 📝 `crates/horizon-client/src/lan/conn.rs` (+124 -11) 📝 `crates/horizon-client/src/lan/mod.rs` (+1 -1) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. **What:** at the end of each tick the game thread handed every player's held packets to that player's writer thread (`PlayerSender::flush` per guest), waking each one. Each wake is a system call (`semaphore_signal`), and on a busy machine the woken writer often takes the rest of the game thread's time slice. Now `lan::flush_all` gives every batch to one post thread (`horizon-lan-post`) in a single channel send, and that thread wakes the writers. With 50 players, that is one wake a tick from the game thread instead of 50. **Order is kept.** Until the post thread has handed a player's batch over, a packet sent straight to that player (outside a tick) goes through the post thread too. Each player's packets keep their order, also against what is sent to it next. New test: `packets_sent_after_a_posted_tick_follow_it` (50 rounds of tick, flush, then immediate send and disconnect, two players). If the post thread can't be started, batches are handed over on the calling thread, as before. **Measured** (dedicated server, 50 walking bots, seed 12345, view distance 10, Mac mini M4 Pro): The flush step on the server thread, from a local probe (`Instant` around the step, every tick, the 900 ticks of the measured window). Base and this PR ran at the same time, sides swapped in round 2: | per tick | base r1 | this PR r1 | base r2 | this PR r2 | |---|---:|---:|---:|---:| | flush step, mean | 1624 µs | 601 µs | 2271 µs | 601 µs | | flush step, median | 587 µs | 10 µs | 143 µs | 9 µs | | flush step, 90th percentile | 4682 µs | 2132 µs | 7277 µs | 22 µs | Share of the server thread (macOS `sample`, 20 s in the measured window, two runs each): | | base | base | this PR | this PR | |---|---:|---:|---:|---:| | `PlayerSender::flush` | 19.3 % | 12.6 % | 0.0 % | 0.0 % | The post thread itself was asleep 97 % of the time (its 50 wakes a tick). MSPT (`/tick query` average), base and this PR at the same time: | | base | this PR | |---|---:|---:| | round 1 | 17.35 ms | 16.76 ms | | round 2 | 13.61 ms | 13.87 ms | | round 3 | 6.60 ms | 6.57 ms | | round 4 (probe builds) | 7.82 ms | 8.44 ms | | round 5 (probe builds) | 10.86 ms | 11.32 ms | MSPT doesn't move outside the noise on this machine. The server thread used only about 3 ms of CPU a tick (`ps -M`); with a load average of 60 to 170 on 14 threads, most of a tick's wall time went to waiting for a core, and the 1 to 1.7 ms a tick this removes is below that spread. The probe and the sampled share are the load-independent numbers. **How it was measured** - Mac mini (M4 Pro, 14 threads, 64 GB), shared with other agents' builds and parity runs: `~/fleet/load` showed the CPU 72 to 92 % busy with a load average of 56 to 170 through the runs. - `tools/server_bench.py --servers horizon --behaviour walk --bots 50 --warmup 30 --seconds 45 --gamerule locator_bar=false`: seed 12345, view and simulation distance 10, bots on the same machine, `fast` profile, base = main 7bbbf6e9. Every run sent the bots the same chunks (490 to 505 a second, none sent twice). - **Paired runs:** the base and the new build ran at the same time (two servers, 50 bots each), so both saw the same load; they swapped worktrees between rounds. - **Probe:** a local patch, not part of this PR, timed the step with `Instant` on the server thread and logged one line per event. The numbers cover the 900 ticks of the measured window. The steps are much shorter than a scheduler time slice, so their medians are close to CPU time even on a loaded machine. - **Share of the server thread:** macOS `sample` of the engine for 20 s inside the measured window of a separate run per build. It counts wall time, so time spent waiting for a core counts too. - MSPT (`/tick query` average) didn't separate the builds on this machine. The server thread used only about 3 ms of CPU a tick (`ps -M`); with load averages up to 170 on 14 threads, most of a tick's wall time went to waiting for a core, and that spread is larger than these changes. **Checks** - `cargo fmt --all --check`: ok - `cargo clippy --workspace --all-targets -- -D warnings`: ok - `cargo test -p horizon-client --lib`: 824 passed, 0 failed, 11 ignored - Local parity (`tools/parity.py --changed origin/main`, one run on #737, #738, #739 and #748 stacked together, plus the chunk-occupancy commit on `perf/chunk-occupied-columns`): 87 passed, 0 failed (every harness that depends on the 9 changed files). Nothing here needs Ludovic. 🤖 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 15:58:54 +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#2769
No description provided.