[PR #2481] [MERGED] World opening: read the world data while the player chooses a world #4356

Closed
opened 2026-11-03 04:16:34 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

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

Base: main ← Head: perf/prefetch-world-data


📝 Commits (2)

  • 26561d3 Preload the world data while the player chooses a world
  • cc58083 Give the dropped preload's memory back to the OS

📊 Changes

6 files changed (+162 additions, -13 deletions)

View changed files

📝 crates/horizon-client/src/game.rs (+12 -0)
📝 crates/horizon-client/src/game/lan.rs (+16 -6)
📝 crates/horizon-client/src/game/world_loading.rs (+76 -0)
📝 crates/horizon-client/src/structure_blocks.rs (+15 -4)
📝 crates/horizon-client/src/vanillagen.rs (+11 -3)
📝 crates/horizon-threads/src/lib.rs (+32 -0)

📄 Description

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

Part of the roadmap item world-entry (faster world creation and entry).

What and why

Before the first chunk can be generated or sent, a world opening (Create World or Play) loads three things:

  • the worldgen bundle (vanillagen::shared_data);
  • the loot tables (structure_blocks::loot_data);
  • the registries and tags a server sends (SyncData, in lan_host).

None of them depends on the seed, and each is loaded once per process, so the first world opening after a launch pays for all three. On the Mac mini they took 0.06 → 0.91 s after open, before the spawn search could start.

Now a helper thread ("World data preload", behind the server and render threads like the workers) starts reading them when the player opens Singleplayer (world select) or Create World, so the reading overlaps the time spent choosing a world. A world opened while it is still reading waits for that part instead of reading it again: each cache holds its lock while it loads.

Nothing is read at the title screen itself. A player who goes back to the title or to Multiplayer and opens no world within a minute gets the memory back. The three caches can now let go of their data (forget_*), and horizon_threads::release_free_memory asks the allocator to hand the freed pages to the OS (malloc_trim on glibc, malloc_zone_pressure_relief on macOS). Without that call the allocator kept them: +130 MB on Linux. Once a world opens, the data is the world's and stays, as before. The noise pipeline depends on the seed and is built where it was.

Measured

entry_bench (scratch driver on perf_bench's control socket), RD 12, fast profile, offscreen, fresh dirs, alternated. Each run: 2 s on the title screen, click singleplayer, 3 s on world select, then open (create, then reopen in a fresh process). Times from open.

Linux, 4 threads (playhorizon, AMD EPYC 7662 limited to 4 threads with taskset -c 0-3; an emulated small CPU, not laptop hardware; rendering on llvmpipe):

main this PR
server thread, create: first chunk 2.30 / 2.14 / 2.20 s 1.55 / 1.66 s
server thread, reopen: first chunk 1.50 / 1.50 / 1.44 s 0.93 / 1.03 s
server thread, create: all chunks in view 14.2 / 14.1 / 13.5 s 12.9 / 12.9 s
server thread, reopen: all chunks in view 5.9 / 5.5 / 5.6 s 5.1 / 5.1 s
in-frame, create: first chunk 4.17 / 3.58 / 3.61 s 3.43 / 3.60 / 3.18 s
in-frame, reopen: first chunk 1.77 / 1.61 / 1.60 s 1.25 / 0.93 / 0.92 s

(One server-thread run of this PR is missing: its binary wasn't in place yet.)

Mac mini M4 Pro (load 80-100 from other sessions, so noisy):

main this PR
server thread, create: first chunk 2.12 / 2.17 / 3.96 s 2.24 / 1.98 / 2.25 s
server thread, reopen: first chunk 1.05 / 1.16 / 1.16 s 0.61 / 0.54 / 0.54 s
in-frame, create: first chunk 4.18 / 2.07 / 4.21 s 3.34 / 3.67 / 1.06 s
in-frame, reopen: first chunk 4.29 / 2.51 / 6.06 s 1.96 / 2.91 / 1.31 s

The earlier real-hardware reference (ThinkPad i5-7200U, perf-next-3): RD 12 entry 26.4 s vs vanilla 75.3 s.

Memory (title_rss.py: no world opened; 15 s on the title, 15 s on world select, then 70 s back on the title):

title world select back on the title after 70 s
Linux, main 748 MB 756 MB 756 MB
Linux, this PR 747 MB 875 MB 802 MB
Mac, main (RSS / footprint) 574 MB 575 MB 546 MB / 652 MB
Mac, this PR (RSS / footprint) 583 MB 694 MB 651 MB / 682 MB

The title screen is unchanged. After the drop, the allocated bytes are back to main's (Mac vmmap: 321 MB vs 320 MB in the malloc zone). What stays (+46 MB on Linux, +30 MB footprint on the Mac) is heap fragmentation the allocator can't return.

Testing

  • Parity: the five harnesses that read the world data (chunk_generation, worldgen, worldgen_random, save_roundtrip, world_reopen) passed on an integration commit with #2480 (5 passed, 0 failed, Mac mini). That run used the first version of this PR, which read the data at the title screen. The loading code is the same; only when it starts changed. Rerun on this head (cc5808357): 5 passed, 0 failed.
  • The drop was checked by hand: the log says preloaded world data dropped, no world opened 60 s after going back to the title.
  • pr_ready (--quick):

pr_ready (tools/pr_ready.py --quick on ludovics-mac-mini, against origin/main at 9312411e5, 3 min 58 s)

Check Result
Scope ✅ 6 files, +162 −13
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ➖ no Python change
rustfmt ✅ (6 s)
clippy ✅ (9 s)
tests: horizon-client, horizon-threads ✅ 1338 passed (3 min 12 s)
doc tests ✅ 0 passed (3 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 82 affected, to run in CI (--quick): arrow, auto_jump, block_entity_anim, block_entity_render, block_entity_tickers, block_particles, block_removal, breaking_model, cauldron, chat_screen, command_exec, command_parse and 70 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

🤖 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/2481 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/26/2026 **Status:** ✅ Merged **Merged:** 10/26/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/prefetch-world-data` --- ### 📝 Commits (2) - [`26561d3`](https://github.com/horizon-rust/horizon/commit/26561d315c5d187c4d1f70af4f92455d030f9967) Preload the world data while the player chooses a world - [`cc58083`](https://github.com/horizon-rust/horizon/commit/cc58083574d857b72c3abc6578254b364e256937) Give the dropped preload's memory back to the OS ### 📊 Changes **6 files changed** (+162 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/game.rs` (+12 -0) 📝 `crates/horizon-client/src/game/lan.rs` (+16 -6) 📝 `crates/horizon-client/src/game/world_loading.rs` (+76 -0) 📝 `crates/horizon-client/src/structure_blocks.rs` (+15 -4) 📝 `crates/horizon-client/src/vanillagen.rs` (+11 -3) 📝 `crates/horizon-threads/src/lib.rs` (+32 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Part of the roadmap item `world-entry` (faster world creation and entry). ## What and why Before the first chunk can be generated or sent, a world opening (Create World or Play) loads three things: - the worldgen bundle (`vanillagen::shared_data`); - the loot tables (`structure_blocks::loot_data`); - the registries and tags a server sends (`SyncData`, in `lan_host`). None of them depends on the seed, and each is loaded once per process, so the first world opening after a launch pays for all three. On the Mac mini they took 0.06 → 0.91 s after `open`, before the spawn search could start. Now a helper thread ("World data preload", behind the server and render threads like the workers) starts reading them when the player opens **Singleplayer** (world select) or **Create World**, so the reading overlaps the time spent choosing a world. A world opened while it is still reading waits for that part instead of reading it again: each cache holds its lock while it loads. Nothing is read at the title screen itself. A player who goes back to the title or to Multiplayer and opens no world within a minute gets the memory back. The three caches can now let go of their data (`forget_*`), and `horizon_threads::release_free_memory` asks the allocator to hand the freed pages to the OS (`malloc_trim` on glibc, `malloc_zone_pressure_relief` on macOS). Without that call the allocator kept them: +130 MB on Linux. Once a world opens, the data is the world's and stays, as before. The noise pipeline depends on the seed and is built where it was. ## Measured `entry_bench` (scratch driver on perf_bench's control socket), RD 12, fast profile, offscreen, fresh dirs, alternated. Each run: 2 s on the title screen, `click singleplayer`, 3 s on world select, then `open` (create, then reopen in a fresh process). Times from `open`. **Linux, 4 threads** (playhorizon, AMD EPYC 7662 limited to 4 threads with `taskset -c 0-3`; an emulated small CPU, not laptop hardware; rendering on llvmpipe): | | main | this PR | |---|---|---| | server thread, create: first chunk | 2.30 / 2.14 / 2.20 s | **1.55 / 1.66 s** | | server thread, reopen: first chunk | 1.50 / 1.50 / 1.44 s | **0.93 / 1.03 s** | | server thread, create: all chunks in view | 14.2 / 14.1 / 13.5 s | 12.9 / 12.9 s | | server thread, reopen: all chunks in view | 5.9 / 5.5 / 5.6 s | 5.1 / 5.1 s | | in-frame, create: first chunk | 4.17 / 3.58 / 3.61 s | 3.43 / 3.60 / 3.18 s | | in-frame, reopen: first chunk | 1.77 / 1.61 / 1.60 s | **1.25 / 0.93 / 0.92 s** | (One server-thread run of this PR is missing: its binary wasn't in place yet.) **Mac mini M4 Pro** (load 80-100 from other sessions, so noisy): | | main | this PR | |---|---|---| | server thread, create: first chunk | 2.12 / 2.17 / 3.96 s | 2.24 / 1.98 / 2.25 s | | server thread, reopen: first chunk | 1.05 / 1.16 / 1.16 s | **0.61 / 0.54 / 0.54 s** | | in-frame, create: first chunk | 4.18 / 2.07 / 4.21 s | 3.34 / 3.67 / 1.06 s | | in-frame, reopen: first chunk | 4.29 / 2.51 / 6.06 s | 1.96 / 2.91 / 1.31 s | The earlier real-hardware reference (ThinkPad i5-7200U, perf-next-3): RD 12 entry 26.4 s vs vanilla 75.3 s. **Memory** (`title_rss.py`: no world opened; 15 s on the title, 15 s on world select, then 70 s back on the title): | | title | world select | back on the title after 70 s | |---|---|---|---| | Linux, main | 748 MB | 756 MB | 756 MB | | Linux, this PR | 747 MB | 875 MB | 802 MB | | Mac, main (RSS / footprint) | 574 MB | 575 MB | 546 MB / 652 MB | | Mac, this PR (RSS / footprint) | 583 MB | 694 MB | 651 MB / 682 MB | The title screen is unchanged. After the drop, the allocated bytes are back to main's (Mac `vmmap`: 321 MB vs 320 MB in the malloc zone). What stays (+46 MB on Linux, +30 MB footprint on the Mac) is heap fragmentation the allocator can't return. ## Testing - Parity: the five harnesses that read the world data (chunk_generation, worldgen, worldgen_random, save_roundtrip, world_reopen) passed on an integration commit with #2480 (5 passed, 0 failed, Mac mini). That run used the first version of this PR, which read the data at the title screen. The loading code is the same; only when it starts changed. Rerun on this head (cc5808357): 5 passed, 0 failed. - The drop was checked by hand: the log says `preloaded world data dropped, no world opened` 60 s after going back to the title. - pr_ready (`--quick`): **pr_ready** (`tools/pr_ready.py --quick` on ludovics-mac-mini, against origin/main at 9312411e5, 3 min 58 s) | Check | Result | | --- | --- | | Scope | ✅ 6 files, +162 −13 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ➖ no Python change | | rustfmt | ✅ (6 s) | | clippy | ✅ (9 s) | | tests: horizon-client, horizon-threads | ✅ 1338 passed (3 min 12 s) | | doc tests | ✅ 0 passed (3 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 82 affected, to run in CI (--quick): arrow, auto_jump, block_entity_anim, block_entity_render, block_entity_tickers, block_particles, block_removal, breaking_model, cauldron, chat_screen, command_exec, command_parse and 70 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | 🤖 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-03 04:16:34 +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#4356
No description provided.