[PR #4160] [MERGED] Chunk generation 18% faster on many cores: mimalloc as the game's allocator #5450

Closed
opened 2026-11-03 05:12:41 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/4160
Author: @JulesB40
Created: 10/30/2026
Status: ✅ Merged
Merged: 10/30/2026
Merged by: @levzzz5154

Base: main ← Head: perf/mimalloc-allocator


📝 Commits (3)

  • 800e7ce Game: mimalloc as the global allocator
  • 5b695d5 Allocator: give free pages back at once
  • 85ff723 Allocator: say which heaps mi_collect frees

📊 Changes

4 files changed (+79 additions, -6 deletions)

View changed files

📝 Cargo.lock (+27 -0)
📝 crates/horizon-client/src/main.rs (+4 -0)
📝 crates/horizon-threads/Cargo.toml (+5 -1)
📝 crates/horizon-threads/src/lib.rs (+43 -5)

📄 Description

🤖 Opened by JulesB40's AI agent (Claude Code, Claude Opus 5.5), not by JulesB40 personally.

What and why

Chunk generation spent a big share of its time in the system allocator. A profile of chunkgen_bench on Windows showed RtlAllocateHeap + RtlFreeHeap at ~16% of CPU, and generation scaled badly across workers because they all contend on that one heap.

The game binary (client, integrated server and --dedicated) now uses mimalloc as its global allocator, which keeps a heap per thread. At startup it sets mimalloc's purge delay to 0, so free pages go back to the OS straight away instead of after a second. With the default delay the game held 26–37% more memory; with purge 0 the cost is about 3% on the server and 8% on the client. release_free_memory also calls mi_collect, so the preload's memory is still given back. Probes and examples keep the system allocator.

Measured on Windows 11 (24 cores). Each run was paired with one on main, back to back, while background load was sampled; numbers are medians of the paired changes:

main → this branch
chunkgen_bench, 24 workers: proto / decorated chunks per second +18.5% / +18.5% (3 pairs)
chunkgen_bench, 1 worker: proto / decorated chunks per second +3.1% / +6.2% (3 pairs)
dedicated server, 10 explore bots: RSS mean / peak +2.6% / +3.4% (3 pairs)
dedicated server: MSPT avg / P99 −0.7% / +0.1% (bot-speed-limited, chunk waits 0)
client, RD 12 (perf_bench): RSS steady / peak +8.3% / +10.8% (2 pairs)
client: frame p50 / p99, 4 scenes within noise, −10.6% to +2.9%

The chunkgen numbers come from the bench with the same mimalloc setting (MIMALLOC_PURGE_DELAY=0), because the bench doesn't link the game's allocator. With mimalloc's defaults it was +32% at 24 workers, but memory went up 26% on the server and 37% on the client.

Generated output doesn't depend on the allocator. The terrain and decorated --snapshots of chunkgen_bench (seed 1234, 32×32 chunks) are byte-identical, by SHA-256, with and without mimalloc, at 1 and 24 workers.

mimalloc is C, built by the cc crate the way ring, hidapi and wasmtime already are. Its option enum isn't named in libmimalloc-sys, so the purge delay is index 15 (mi_option_purge_delay in both v2 and v3 of mimalloc.h). A test checks that this option starts at v3's documented 1000 ms and is 0 after configure_allocator.

Proved by: paired A/B runs of chunkgen_bench, tools/server_bench.py and tools/perf_bench.py against main, plus a smoke run of the release build. The client opened a world offscreen, took a screenshot and quit cleanly; --dedicated reached Done and stopped cleanly; neither logged an error.

Kind

  • parity: Horizon now does what vanilla 26.3 does
  • bug: a crash or a breakage
  • perf: faster, or less memory
  • feature: something new for players (listed under New in the release notes)
  • extra: Horizon-only, off by default
  • tooling: tools, CI, tests, harnesses
  • docs

Vanilla reference

None: the allocator has no vanilla counterpart and doesn't change what is generated (snapshots above). Locally, chunk_generation, worldgen, chunk_stage and biome_generation pass. Their probes keep the system allocator, so CI's parity job is the check on the game binary.

Self-check

pr_ready (tools/pr_ready.py --quick on DELL-JULES, against origin/main at b9c54ecd5, 13 min 42 s)

Check Result
Scope ✅ 4 files, +79 −6
Cargo.lock ✅ changes with a Cargo.toml
Public tree ✅
Python tests ❌ 480 run, FAILED (errors=1, skipped=2) (1 min 20 s)
rustfmt ✅ (10 s)
clippy ✅ (1 min 8 s)
tests: workspace ❌ 740 passed, 1 failed (3 min 51 s)
doc tests ✅ 0 passed (27 s)
content root tests ✅ 4 passed (3 min 8 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ✅ (2 s)
launcher clippy ✅ (1 min 28 s)
launcher tests ✅ 270 passed (1 min 59 s)
Parity ➖ 354 affected, to run in CI (--quick)
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

Both failures also fail on unmodified main (b9c54ecd5) on this Windows machine, independently of this change:

  • test_port_regen.InterpreterTests.test_python_is_an_interpreter_that_runs
  • title::tests::each_world_preset_writes_its_own_generator (NotADirectory, os error 267)

CI runs them on Linux.

Reviewed with sol-loop: 1 round with gpt-6.1-sol on high. Sol verdict: approved

Created with Claude Opus 5.5 in Claude Code; benchmarks and review by GPT 6.1 Sol (Codex).

🤖 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/4160 **Author:** [@JulesB40](https://github.com/JulesB40) **Created:** 10/30/2026 **Status:** ✅ Merged **Merged:** 10/30/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `perf/mimalloc-allocator` --- ### 📝 Commits (3) - [`800e7ce`](https://github.com/horizon-rust/horizon/commit/800e7cef3371762ec67184fa1c4da4910c15295b) Game: mimalloc as the global allocator - [`5b695d5`](https://github.com/horizon-rust/horizon/commit/5b695d5a2674c94f669a8a147048fe0fa0b84fa4) Allocator: give free pages back at once - [`85ff723`](https://github.com/horizon-rust/horizon/commit/85ff723581b8448947117ddb2b83c068d06ab102) Allocator: say which heaps mi_collect frees ### 📊 Changes **4 files changed** (+79 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `Cargo.lock` (+27 -0) 📝 `crates/horizon-client/src/main.rs` (+4 -0) 📝 `crates/horizon-threads/Cargo.toml` (+5 -1) 📝 `crates/horizon-threads/src/lib.rs` (+43 -5) </details> ### 📄 Description > 🤖 Opened by JulesB40's AI agent (Claude Code, Claude Opus 5.5), not by JulesB40 personally. ## What and why Chunk generation spent a big share of its time in the system allocator. A profile of `chunkgen_bench` on Windows showed `RtlAllocateHeap` + `RtlFreeHeap` at ~16% of CPU, and generation scaled badly across workers because they all contend on that one heap. The game binary (client, integrated server and `--dedicated`) now uses mimalloc as its global allocator, which keeps a heap per thread. At startup it sets mimalloc's purge delay to 0, so free pages go back to the OS straight away instead of after a second. With the default delay the game held 26–37% more memory; with purge 0 the cost is about 3% on the server and 8% on the client. `release_free_memory` also calls `mi_collect`, so the preload's memory is still given back. Probes and examples keep the system allocator. Measured on Windows 11 (24 cores). Each run was paired with one on main, back to back, while background load was sampled; numbers are medians of the paired changes: | | main → this branch | |---|---| | chunkgen_bench, 24 workers: proto / decorated chunks per second | **+18.5% / +18.5%** (3 pairs) | | chunkgen_bench, 1 worker: proto / decorated chunks per second | +3.1% / +6.2% (3 pairs) | | dedicated server, 10 explore bots: RSS mean / peak | +2.6% / +3.4% (3 pairs) | | dedicated server: MSPT avg / P99 | −0.7% / +0.1% (bot-speed-limited, chunk waits 0) | | client, RD 12 (`perf_bench`): RSS steady / peak | +8.3% / +10.8% (2 pairs) | | client: frame p50 / p99, 4 scenes | within noise, −10.6% to +2.9% | The chunkgen numbers come from the bench with the same mimalloc setting (`MIMALLOC_PURGE_DELAY=0`), because the bench doesn't link the game's allocator. With mimalloc's defaults it was +32% at 24 workers, but memory went up 26% on the server and 37% on the client. Generated output doesn't depend on the allocator. The terrain and decorated `--snapshot`s of `chunkgen_bench` (seed 1234, 32×32 chunks) are byte-identical, by SHA-256, with and without mimalloc, at 1 and 24 workers. mimalloc is C, built by the `cc` crate the way `ring`, `hidapi` and `wasmtime` already are. Its option enum isn't named in `libmimalloc-sys`, so the purge delay is index 15 (`mi_option_purge_delay` in both v2 and v3 of `mimalloc.h`). A test checks that this option starts at v3's documented 1000 ms and is 0 after `configure_allocator`. Proved by: paired A/B runs of `chunkgen_bench`, `tools/server_bench.py` and `tools/perf_bench.py` against main, plus a smoke run of the release build. The client opened a world offscreen, took a screenshot and quit cleanly; `--dedicated` reached `Done` and stopped cleanly; neither logged an error. ## Kind - [ ] parity: Horizon now does what vanilla 26.3 does - [ ] bug: a crash or a breakage - [x] perf: faster, or less memory - [ ] feature: something new for players (listed under New in the release notes) - [ ] extra: Horizon-only, off by default - [ ] tooling: tools, CI, tests, harnesses - [ ] docs ## Vanilla reference None: the allocator has no vanilla counterpart and doesn't change what is generated (snapshots above). Locally, `chunk_generation`, `worldgen`, `chunk_stage` and `biome_generation` pass. Their probes keep the system allocator, so CI's parity job is the check on the game binary. ## Self-check **pr_ready** (`tools/pr_ready.py --quick` on DELL-JULES, against origin/main at b9c54ecd5, 13 min 42 s) | Check | Result | | --- | --- | | Scope | ✅ 4 files, +79 −6 | | Cargo.lock | ✅ changes with a Cargo.toml | | Public tree | ✅ | | Python tests | ❌ 480 run, FAILED (errors=1, skipped=2) (1 min 20 s) | | rustfmt | ✅ (10 s) | | clippy | ✅ (1 min 8 s) | | tests: workspace | ❌ 740 passed, 1 failed (3 min 51 s) | | doc tests | ✅ 0 passed (27 s) | | content root tests | ✅ 4 passed (3 min 8 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ✅ (2 s) | | launcher clippy | ✅ (1 min 28 s) | | launcher tests | ✅ 270 passed (1 min 59 s) | | Parity | ➖ 354 affected, to run in CI (--quick) | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | Both failures also fail on unmodified main (b9c54ecd5) on this Windows machine, independently of this change: - `test_port_regen.InterpreterTests.test_python_is_an_interpreter_that_runs` - `title::tests::each_world_preset_writes_its_own_generator` (`NotADirectory`, os error 267) CI runs them on Linux. Reviewed with sol-loop: 1 round with gpt-6.1-sol on high. Sol verdict: approved Created with Claude Opus 5.5 in Claude Code; benchmarks and review by GPT 6.1 Sol (Codex). 🤖 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 05:12:41 +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#5450
No description provided.