[PR #544] [MERGED] Run single player on an integrated server thread (opt-in) #2632

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

📋 Pull Request Information

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

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


📝 Commits (3)

  • c57c409 Split Resources into shared read-only data and per-game pack state
  • c2f4ecc Build the game in Game::new instead of inside run()
  • 56dc435 Run single player on an integrated server thread (opt-in)

📊 Changes

105 files changed (+1307 additions, -911 deletions)

View changed files

📝 crates/horizon-client/src/control.rs (+12 -8)
📝 crates/horizon-client/src/fog.rs (+0 -16)
📝 crates/horizon-client/src/game.rs (+526 -456)
📝 crates/horizon-client/src/game/advancements.rs (+5 -5)
📝 crates/horizon-client/src/game/advancements_screen.rs (+9 -9)
📝 crates/horizon-client/src/game/agent.rs (+1 -1)
📝 crates/horizon-client/src/game/armor_stands.rs (+5 -5)
📝 crates/horizon-client/src/game/backdrop.rs (+4 -4)
📝 crates/horizon-client/src/game/beacon.rs (+5 -11)
📝 crates/horizon-client/src/game/beehives.rs (+2 -2)
📝 crates/horizon-client/src/game/block_entity_anims.rs (+1 -1)
📝 crates/horizon-client/src/game/block_items.rs (+1 -1)
📝 crates/horizon-client/src/game/blockctl.rs (+3 -3)
📝 crates/horizon-client/src/game/boats.rs (+0 -1)
📝 crates/horizon-client/src/game/books.rs (+4 -4)
📝 crates/horizon-client/src/game/cartography.rs (+3 -3)
📝 crates/horizon-client/src/game/chat.rs (+14 -16)
📝 crates/horizon-client/src/game/command_blocks.rs (+7 -7)
📝 crates/horizon-client/src/game/commands.rs (+8 -8)
📝 crates/horizon-client/src/game/commands/compute.rs (+1 -1)

...and 80 more files

📄 Description

What and why

Single player runs the whole world tick inside the frame: perf-ticks-1 measured a frame with a tick at 3.66 ms of CPU against 0.24 ms without one, and the world tick is 1.51 ms of that. After a world opens, the render thread also runs catch-up ticks (up to 10 per frame) while every villager searches for a job site: most of what is left of the #489 freeze. Vanilla doesn't do any of that on the render thread: single player is an IntegratedServer on its own "Server thread", and the window is a client of it.

This PR adds that, behind an opt-in switch (--server-thread or HORIZON_SERVER_THREAD=1), so the default path doesn't change while the gaps below are filled.

With it, opening a single-player world:

  • starts a second Game (from #538's Game::new) on a thread named "Server thread". It shares the window's read-only Resources (#537) and runs the dedicated server's loop at 20 TPS: streaming, world update, tick(). It builds no window and draws nothing: the integrated server skips meshing, and the dedicated server is unchanged.
  • has the window join it as a remote player, through the same code as joining any server. This PR uses a socket on 127.0.0.1 with no compression; #545 swaps it for an in-memory connection, as vanilla does.
  • lets in only the window's own profile, and makes it an operator when the world allows commands.
  • keeps the save format. The window's player is the world's own player: it loads from and saves to players/data/<singleplayer_uuid>.dat, so a world opens the same way in either mode, and no guest file appears.
  • pauses the server while a pause screen is open (IntegratedServer.tickServer). Leaving the world or quitting waits for the server to save. Leaving goes back to the title screen, not the multiplayer list.

lan/playerdata.rs splits load/save into apply/store, which work on a Level, so the host's player can go through the world's level.dat / single-player file instead of a guest file. The bench summary also gets low_01pct_fps (the key tools/perf_bench.py already uses).

What doesn't work yet in this mode (plan step 3)

That's why it's opt-in. The default single-player path is untouched, and every harness runs on it.

  • The server's players are LAN guests (lan/guest.rs): no status effects, advancements or statistics, and no dimension travel or portals. A world whose player is outside the Overworld opens the usual way.
  • The client can't decode the command tree yet ("skipped undecodable packet commands"), so there's no command completion.
  • The horizon-ctl state/save queries read the window's state, not the server's.
  • Open to LAN isn't available yet: the server needs to start its listener on request.
  • The pause menu shows the multiplayer buttons.
  • The server builds its own HUD atlas, so the process uses about 150–200 MB more RAM.

Vanilla reference

  • IntegratedServer: its own thread, tickServer pausing while Minecraft.isPaused(), halt/save on leave.
  • MinecraftServer.runServer: the 20 TPS loop with catch-up.
  • PlayerList.load: the single-player tag for the host.
  • Minecraft.doWorldLoad: the window joins its own server as a client.

Harnesses: the opt-in path is covered by dedicated_server (the same server and remote code). Every single-player harness still runs the default path. A smoke run of the new mode is below.

Measurements

Mac mini (M4 Pro). Release build of this stack (#545 included), rebased on main at b4ad6538. A survival world with about 200 mobs, render distance 12, uncapped (no VSync), 20 s of horizon-ctl bench after the chunks settle, with HORIZON_SPIKES=0 logging every frame's CPU time and spans. Runs alternate between the two modes. The machine was shared with other agents' parity runs the whole time (load average 22 to 136 on 14 cores), so the wall-clock FPS numbers swing a lot from run to run. The per-frame CPU split and the spans are the dependable part.

The tick leaves the frame. Average tick span per frame that ran a tick: 3.2–3.75 ms in-frame, 0.26–0.34 ms with the server thread (that remainder is the client's own tick). In the bench's phase split, the tick phase per frame drops 2.6–9x in all six pairs below. In five of the six, a frame with a tick costs 3.4–6.3x a frame without one in-frame, and 2.1–3.1x with the server thread. The sixth pair, the first run, goes the other way.

World open (the #489 window). The 10 s after the player appears, three runs per mode:

in-frame server thread
CPU the window spends in world ticks 0.89 / 1.01 / 2.02 s 0.13 / 0.15 / 0.17 s (client ticks)
Slowest tick inside one frame 60 / 70 / 97 ms 17 / 24 / 21 ms
Most ticks run in one frame 2 / 2 / 7 1–3 (client ticks only)
Frames over 50 ms 1 / 6 / 33 6 / 0 / 10
Slowest frame 64 / 83 / 194 ms 72 / 50 / 73 ms

The window doesn't run catch-up world ticks any more. The server catches up on its own thread. The frames over 50 ms that are left are in renderer and update_world (chunk uploads as they arrive), not ticks.

Steady state, same render distance (load average 22–39):

in-frame server thread in-frame server thread in-frame server thread
Average FPS 353 576 683 489 708 495
1% low FPS 36 29 38 48 62 63
0.1% low FPS 23 16 17 25 34 39
Frame p50 / p99 (ms) 1.45 / 19.70 0.72 / 22.01 0.78 / 14.65 1.31 / 13.98 0.82 / 10.23 1.42 / 11.91
CPU per frame p50 / p99 (ms) 1.29 / 17.62 0.66 / 18.69 0.70 / 12.39 0.98 / 12.20 0.68 / 8.75 0.95 / 10.59
Tick phase, avg per frame (ms) 0.231 0.026 0.123 0.022 0.084 0.016
CPU, frames with a tick: avg / p99 (ms) 7.41 / 33.20 5.44 / 35.10 6.46 / 48.80 3.43 / 24.10 5.25 / 23.00 3.20 / 14.50
CPU, frames without: avg / p99 (ms) 2.01 / 14.70 1.26 / 15.20 1.03 / 8.90 1.41 / 10.60 0.94 / 6.70 1.36 / 9.60
Tick frame / other frame CPU 3.7x 4.3x 6.3x 2.4x 5.6x 2.4x
Process RSS (MB) 1524 1719 1521 1765 1543 1700
Mobs the window sees 192 153 193 153 201 146
Load average (1 min) 38 35 35 31 27 39

The server-thread frames without a tick cost more because the window draws about 30% more terrain. Over the network it receives every chunk the server sends (637 against 529 for single player's own streaming), so it meshes the full render distance. Measured with status, it draws 488 sections against 370 and holds 3497 resident sections against 2705. Its renderer span on non-tick frames is 1.2–1.4 ms against 0.7–0.8 ms. It also sees fewer mobs (about 150 against 200, the server's tracking range). For the same drawn terrain, here is server-thread mode at render distance 11 (408 sections drawn) against in-frame at 12 (370). The machine was much busier (load 49–136):

in-frame server thread in-frame server thread in-frame server thread
Average FPS 303 258 93 136 119 161
1% low FPS 27 20 9 14 11 16
0.1% low FPS 13 13 7 9 7 10
Frame p50 / p99 (ms) 1.46 / 24.84 1.33 / 36.31 2.78 / 87.84 1.67 / 50.52 2.37 / 64.94 1.68 / 49.07
CPU per frame p50 / p99 (ms) 1.00 / 21.77 1.01 / 32.46 1.69 / 84.56 1.40 / 43.08 1.41 / 56.80 1.33 / 43.00
Tick phase, avg per frame (ms) 0.255 0.098 2.056 0.236 1.314 0.154
CPU, frames with a tick: avg / p99 (ms) 7.26 / 54.90 7.75 / 57.10 20.71 / 112.90 10.64 / 66.60 15.85 / 99.30 8.34 / 58.20
CPU, frames without: avg / p99 (ms) 2.13 / 18.90 2.50 / 26.10 5.16 / 49.90 4.48 / 38.00 4.44 / 40.20 4.00 / 38.50
Tick frame / other frame CPU 3.4x 3.1x 4.0x 2.4x 3.6x 2.1x
Process RSS (MB) 1547 1700 1533 1686 1549 1672
Mobs the window sees 204 147 200 152 209 147
Load average (1 min) 54 49 95 98 136 100

So the gain shows up as the tick work leaving the frame, a flatter tick/no-tick pattern and a calmer world open. Average FPS on this shared machine doesn't move consistently. Single player streaming one ring less than the network path is a separate difference worth checking against vanilla.

Smoke run of the new mode (the whole stack, --server-thread, offscreen, through horizon-ctl):

  • The player loads at the saved position and walks.
  • Pause freezes the server's day_time (4396 → 4396), and resume continues it (4436).
  • Leaving returns to the title screen. Reopening puts the player where they left.
  • Only players/data/<singleplayer_uuid>.dat is written. The game exits 0.
  • The server log shows [Server thread/INFO]: Player…[/127.0.0.1:0] logged in.

Checks

  • cargo fmt --all --check: ok
  • cargo clippy --workspace --all-targets -- -D warnings: ok
  • cargo test -p horizon-client --lib: 759 passed, 0 failed, 7 ignored (top of the stack)
  • cargo test -p horizon-client --bin horizon-client: 55 passed
  • Local parity, tools/parity.py --changed origin/main, on the top of the stack (#537, #538, this PR and #545): 84 passed, 0 failed (includes dedicated_server, lan, save_roundtrip, world_reopen and every single-player harness), on the stack rebased on main at a08add23

Stack: #537 → #538 → this PR → #545.

Refs #495, #489

🤖 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/544 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/16/2026 **Status:** ✅ Merged **Merged:** 10/16/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/integrated-server-thread` --- ### 📝 Commits (3) - [`c57c409`](https://github.com/horizon-rust/horizon/commit/c57c409f40cb7a7b1e6939e0c61476a5134d9411) Split Resources into shared read-only data and per-game pack state - [`c2f4ecc`](https://github.com/horizon-rust/horizon/commit/c2f4ecc188d8fa49909550c4ab61eef85e75be7e) Build the game in Game::new instead of inside run() - [`56dc435`](https://github.com/horizon-rust/horizon/commit/56dc4352d2d935b8b199ea40baa8256bb41a6301) Run single player on an integrated server thread (opt-in) ### 📊 Changes **105 files changed** (+1307 additions, -911 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/control.rs` (+12 -8) 📝 `crates/horizon-client/src/fog.rs` (+0 -16) 📝 `crates/horizon-client/src/game.rs` (+526 -456) 📝 `crates/horizon-client/src/game/advancements.rs` (+5 -5) 📝 `crates/horizon-client/src/game/advancements_screen.rs` (+9 -9) 📝 `crates/horizon-client/src/game/agent.rs` (+1 -1) 📝 `crates/horizon-client/src/game/armor_stands.rs` (+5 -5) 📝 `crates/horizon-client/src/game/backdrop.rs` (+4 -4) 📝 `crates/horizon-client/src/game/beacon.rs` (+5 -11) 📝 `crates/horizon-client/src/game/beehives.rs` (+2 -2) 📝 `crates/horizon-client/src/game/block_entity_anims.rs` (+1 -1) 📝 `crates/horizon-client/src/game/block_items.rs` (+1 -1) 📝 `crates/horizon-client/src/game/blockctl.rs` (+3 -3) 📝 `crates/horizon-client/src/game/boats.rs` (+0 -1) 📝 `crates/horizon-client/src/game/books.rs` (+4 -4) 📝 `crates/horizon-client/src/game/cartography.rs` (+3 -3) 📝 `crates/horizon-client/src/game/chat.rs` (+14 -16) 📝 `crates/horizon-client/src/game/command_blocks.rs` (+7 -7) 📝 `crates/horizon-client/src/game/commands.rs` (+8 -8) 📝 `crates/horizon-client/src/game/commands/compute.rs` (+1 -1) _...and 80 more files_ </details> ### 📄 Description ## What and why Single player runs the whole world tick inside the frame: perf-ticks-1 measured a frame with a tick at 3.66 ms of CPU against 0.24 ms without one, and the world tick is 1.51 ms of that. After a world opens, the render thread also runs catch-up ticks (up to 10 per frame) while every villager searches for a job site: most of what is left of the #489 freeze. Vanilla doesn't do any of that on the render thread: single player is an `IntegratedServer` on its own "Server thread", and the window is a client of it. This PR adds that, behind an opt-in switch (`--server-thread` or `HORIZON_SERVER_THREAD=1`), so the default path doesn't change while the gaps below are filled. With it, opening a single-player world: - starts a second `Game` (from #538's `Game::new`) on a thread named "Server thread". It shares the window's read-only `Resources` (#537) and runs the dedicated server's loop at 20 TPS: streaming, world update, `tick()`. It builds no window and draws nothing: the integrated server skips meshing, and the dedicated server is unchanged. - has the window join it as a remote player, through the same code as joining any server. This PR uses a socket on 127.0.0.1 with no compression; #545 swaps it for an in-memory connection, as vanilla does. - lets in only the window's own profile, and makes it an operator when the world allows commands. - keeps the save format. The window's player is the world's own player: it loads from and saves to `players/data/<singleplayer_uuid>.dat`, so a world opens the same way in either mode, and no guest file appears. - pauses the server while a pause screen is open (`IntegratedServer.tickServer`). Leaving the world or quitting waits for the server to save. Leaving goes back to the title screen, not the multiplayer list. `lan/playerdata.rs` splits `load`/`save` into `apply`/`store`, which work on a `Level`, so the host's player can go through the world's `level.dat` / single-player file instead of a guest file. The bench summary also gets `low_01pct_fps` (the key `tools/perf_bench.py` already uses). ### What doesn't work yet in this mode (plan step 3) That's why it's opt-in. The default single-player path is untouched, and every harness runs on it. - The server's players are LAN guests (`lan/guest.rs`): no status effects, advancements or statistics, and no dimension travel or portals. A world whose player is outside the Overworld opens the usual way. - The client can't decode the command tree yet ("skipped undecodable packet commands"), so there's no command completion. - The horizon-ctl `state`/`save` queries read the window's state, not the server's. - Open to LAN isn't available yet: the server needs to start its listener on request. - The pause menu shows the multiplayer buttons. - The server builds its own HUD atlas, so the process uses about 150–200 MB more RAM. ## Vanilla reference - `IntegratedServer`: its own thread, `tickServer` pausing while `Minecraft.isPaused()`, `halt`/save on leave. - `MinecraftServer.runServer`: the 20 TPS loop with catch-up. - `PlayerList.load`: the single-player tag for the host. - `Minecraft.doWorldLoad`: the window joins its own server as a client. Harnesses: the opt-in path is covered by `dedicated_server` (the same server and remote code). Every single-player harness still runs the default path. A smoke run of the new mode is below. ## Measurements Mac mini (M4 Pro). Release build of this stack (#545 included), rebased on main at b4ad6538. A survival world with about 200 mobs, render distance 12, uncapped (no VSync), 20 s of `horizon-ctl bench` after the chunks settle, with `HORIZON_SPIKES=0` logging every frame's CPU time and spans. Runs alternate between the two modes. The machine was shared with other agents' parity runs the whole time (load average 22 to 136 on 14 cores), so the wall-clock FPS numbers swing a lot from run to run. The per-frame CPU split and the spans are the dependable part. **The tick leaves the frame.** Average `tick` span per frame that ran a tick: 3.2–3.75 ms in-frame, 0.26–0.34 ms with the server thread (that remainder is the client's own tick). In the bench's phase split, the tick phase per frame drops 2.6–9x in all six pairs below. In five of the six, a frame with a tick costs 3.4–6.3x a frame without one in-frame, and 2.1–3.1x with the server thread. The sixth pair, the first run, goes the other way. **World open (the #489 window).** The 10 s after the player appears, three runs per mode: | | in-frame | server thread | |---|---|---| | CPU the window spends in world ticks | 0.89 / 1.01 / 2.02 s | 0.13 / 0.15 / 0.17 s (client ticks) | | Slowest tick inside one frame | 60 / 70 / 97 ms | 17 / 24 / 21 ms | | Most ticks run in one frame | 2 / 2 / 7 | 1–3 (client ticks only) | | Frames over 50 ms | 1 / 6 / 33 | 6 / 0 / 10 | | Slowest frame | 64 / 83 / 194 ms | 72 / 50 / 73 ms | The window doesn't run catch-up world ticks any more. The server catches up on its own thread. The frames over 50 ms that are left are in `renderer` and `update_world` (chunk uploads as they arrive), not ticks. **Steady state, same render distance (load average 22–39):** | | in-frame | server thread | in-frame | server thread | in-frame | server thread | |---|---|---|---|---|---|---| | Average FPS | 353 | 576 | 683 | 489 | 708 | 495 | | 1% low FPS | 36 | 29 | 38 | 48 | 62 | 63 | | 0.1% low FPS | 23 | 16 | 17 | 25 | 34 | 39 | | Frame p50 / p99 (ms) | 1.45 / 19.70 | 0.72 / 22.01 | 0.78 / 14.65 | 1.31 / 13.98 | 0.82 / 10.23 | 1.42 / 11.91 | | CPU per frame p50 / p99 (ms) | 1.29 / 17.62 | 0.66 / 18.69 | 0.70 / 12.39 | 0.98 / 12.20 | 0.68 / 8.75 | 0.95 / 10.59 | | Tick phase, avg per frame (ms) | 0.231 | 0.026 | 0.123 | 0.022 | 0.084 | 0.016 | | CPU, frames with a tick: avg / p99 (ms) | 7.41 / 33.20 | 5.44 / 35.10 | 6.46 / 48.80 | 3.43 / 24.10 | 5.25 / 23.00 | 3.20 / 14.50 | | CPU, frames without: avg / p99 (ms) | 2.01 / 14.70 | 1.26 / 15.20 | 1.03 / 8.90 | 1.41 / 10.60 | 0.94 / 6.70 | 1.36 / 9.60 | | Tick frame / other frame CPU | 3.7x | 4.3x | 6.3x | 2.4x | 5.6x | 2.4x | | Process RSS (MB) | 1524 | 1719 | 1521 | 1765 | 1543 | 1700 | | Mobs the window sees | 192 | 153 | 193 | 153 | 201 | 146 | | Load average (1 min) | 38 | 35 | 35 | 31 | 27 | 39 | The server-thread frames without a tick cost more because the window draws about 30% more terrain. Over the network it receives every chunk the server sends (637 against 529 for single player's own streaming), so it meshes the full render distance. Measured with `status`, it draws 488 sections against 370 and holds 3497 resident sections against 2705. Its `renderer` span on non-tick frames is 1.2–1.4 ms against 0.7–0.8 ms. It also sees fewer mobs (about 150 against 200, the server's tracking range). For the same drawn terrain, here is server-thread mode at render distance 11 (408 sections drawn) against in-frame at 12 (370). The machine was much busier (load 49–136): | | in-frame | server thread | in-frame | server thread | in-frame | server thread | |---|---|---|---|---|---|---| | Average FPS | 303 | 258 | 93 | 136 | 119 | 161 | | 1% low FPS | 27 | 20 | 9 | 14 | 11 | 16 | | 0.1% low FPS | 13 | 13 | 7 | 9 | 7 | 10 | | Frame p50 / p99 (ms) | 1.46 / 24.84 | 1.33 / 36.31 | 2.78 / 87.84 | 1.67 / 50.52 | 2.37 / 64.94 | 1.68 / 49.07 | | CPU per frame p50 / p99 (ms) | 1.00 / 21.77 | 1.01 / 32.46 | 1.69 / 84.56 | 1.40 / 43.08 | 1.41 / 56.80 | 1.33 / 43.00 | | Tick phase, avg per frame (ms) | 0.255 | 0.098 | 2.056 | 0.236 | 1.314 | 0.154 | | CPU, frames with a tick: avg / p99 (ms) | 7.26 / 54.90 | 7.75 / 57.10 | 20.71 / 112.90 | 10.64 / 66.60 | 15.85 / 99.30 | 8.34 / 58.20 | | CPU, frames without: avg / p99 (ms) | 2.13 / 18.90 | 2.50 / 26.10 | 5.16 / 49.90 | 4.48 / 38.00 | 4.44 / 40.20 | 4.00 / 38.50 | | Tick frame / other frame CPU | 3.4x | 3.1x | 4.0x | 2.4x | 3.6x | 2.1x | | Process RSS (MB) | 1547 | 1700 | 1533 | 1686 | 1549 | 1672 | | Mobs the window sees | 204 | 147 | 200 | 152 | 209 | 147 | | Load average (1 min) | 54 | 49 | 95 | 98 | 136 | 100 | So the gain shows up as the tick work leaving the frame, a flatter tick/no-tick pattern and a calmer world open. Average FPS on this shared machine doesn't move consistently. Single player streaming one ring less than the network path is a separate difference worth checking against vanilla. **Smoke run of the new mode** (the whole stack, `--server-thread`, offscreen, through horizon-ctl): - The player loads at the saved position and walks. - Pause freezes the server's `day_time` (4396 → 4396), and resume continues it (4436). - Leaving returns to the title screen. Reopening puts the player where they left. - Only `players/data/<singleplayer_uuid>.dat` is written. The game exits 0. - The server log shows `[Server thread/INFO]: Player…[/127.0.0.1:0] logged in`. ## Checks - `cargo fmt --all --check`: ok - `cargo clippy --workspace --all-targets -- -D warnings`: ok - `cargo test -p horizon-client --lib`: 759 passed, 0 failed, 7 ignored (top of the stack) - `cargo test -p horizon-client --bin horizon-client`: 55 passed - Local parity, `tools/parity.py --changed origin/main`, on the top of the stack (#537, #538, this PR and #545): 84 passed, 0 failed (includes `dedicated_server`, `lan`, `save_roundtrip`, `world_reopen` and every single-player harness), on the stack rebased on main at a08add23 Stack: #537 → #538 → this PR → #545. Refs #495, #489 🤖 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:57:48 +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#2632
No description provided.