[PR #5915] [MERGED] Idle memory: block state properties compact, the registry's strings shared (−17 MB RSS) #6558

Closed
opened 2026-11-03 06:22:59 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/5915
Author: @ludovic111
Created: 11/1/2026
Status: ✅ Merged
Merged: 11/1/2026
Merged by: @ludovic111

Base: main ← Head: perf/block-state-properties


📝 Commits (4)

  • e7f99e7 Block states: compact properties with shared strings
  • 49e08de Block states: a palette entry's properties collect in one pass
  • b6709fe Block states: facts.json reads properties straight into shared strings
  • d44fb43 Block states: shared property strings are the process's, not counted

📊 Changes

17 files changed (+557 additions, -61 deletions)

View changed files

📝 crates/horizon-client/examples/mod_port_probe.rs (+1 -1)
📝 crates/horizon-client/examples/probe/mob_ai_probe.rs (+1 -1)
📝 crates/horizon-client/examples/probe/spawn_rules_fixture.rs (+1 -1)
📝 crates/horizon-client/src/game/blockctl.rs (+4 -1)
📝 crates/horizon-client/src/game/machines/native_fluids.rs (+1 -1)
📝 crates/horizon-client/src/game/sculk_fixture.rs (+1 -1)
📝 crates/horizon-client/src/save/gateway.rs (+1 -1)
📝 crates/horizon-items/src/items.rs (+11 -11)
➕ crates/horizon-world/src/block_state_properties.rs (+418 -0)
📝 crates/horizon-world/src/chunk.rs (+9 -8)
📝 crates/horizon-world/src/level/growth_tests.rs (+2 -6)
📝 crates/horizon-world/src/level/tests.rs (+2 -2)
📝 crates/horizon-world/src/lib.rs (+1 -0)
📝 crates/horizon-world/src/main.rs (+1 -1)
📝 crates/horizon-world/src/registry.rs (+7 -3)
📝 crates/horizon-world/src/registry/facts.rs (+95 -22)
📝 crates/horizon-world/src/worldgen/structure/processor/pillars.rs (+1 -1)

📄 Description

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

What

BlockState.properties was a BTreeMap<String, String>. For the registry's 35,723 vanilla states that is a 544-byte B-tree leaf plus two small string allocations per property, about 30 MB of a dedicated server's idle heap (the largest single item in a malloc_history of an idle server), for 158,409 properties made of only 237 distinct strings.

BlockProperties (new, horizon_world::chunk::BlockProperties) keeps them as a sorted boxed slice of pairs and reads like the map it replaces: get (still &String), get_mut, insert, remove, contains_key, iter/keys/values in name order, properties["facing"] (panics on a missing key as the map does), FromIterator/Extend/From<[_; N]>/From<BTreeMap> with the map's "last value of a name wins", and equality, order, hash and Debug pair by pair, as the map's. Serde reads and writes it as a map.

  • The registry shares its states' strings: facts.json is read straight into one shared string per distinct name or value, interned once for the process (no owned string per property, no map), and from_states shares whatever arrives owned (from_nbt).
  • States made elsewhere (chunk palettes, commands, structure templates) own their strings, as before: nothing is interned from player input. Changing a shared value (get_mut) copies it first.
  • BlockState::from_nbt collects a palette entry's properties in one pass.

Callers change only where they named the type: &BTreeMap<String, String> parameters that receive a state's properties (loot context, the pillars processor) and struct literals that passed a map (.into()).

Measured

playhorizon, --cpus 0-3 --exclusive, dedicated server, new world, fresh data folders per start; main = 7e5e3d1ad.

main this PR
live heap, idle (counting allocator, 5 starts each, Arc version) 167.9 MB 154.3 MB (−13.6 MB, every start)
live heap after the core tables 77.6 MB 64.1 MB
RSS after the core tables 131-140 MB 119-121 MB
idle RSS 32 s after Done, THP on (4 alternated starts, median) 299.5 MB 281.5 MB (−18 MB, 4/4)
idle RSS, MIMALLOC_ALLOW_THP=0 ([engine] low_memory), 2+3 starts 241.7 MB 224.7 MB (−17 MB, every start)
Done (4 alternated starts, median) 2.06 s 1.955 s (−105 ms, 4/4)
load core tables (median) 0.70 s 0.68 s
20 bots, MSPT (3 alternated rounds) 27.64 / 28.05 / 25.12 27.81 / 25.47 / 25.63 (same)
20 bots, streaming span 3.73 / 3.55 / 3.57 ms 3.60 / 3.50 / 3.50 ms

The first version shared the strings through Arcs: the same memory, but 20 bots were slower 3/3 (streaming +0.25 ms), world generation threads cloning the same states contended on the counters. Shared strings are now interned once for the process (&'static String: the registries' names and values only, a few hundred, never a player's), so a clone copies pointers.

The live-heap counter counts requested bytes, so it undercounts the small strings (5-10 requested bytes, a 16-byte block each); RSS moves more than the live counter but is noisy with transparent huge pages on, hence the THP-off row.

Tests

  • block_state_properties tests: reads and writes as a BTreeMap (get, insert, get_mut, remove, iteration order, Debug, conversion back), collects as a map does (duplicate names), orders/compares/hashes as the map for a set of cases, shared strings read as owned ones and a change to one state's value leaves the other state's.
  • registry::facts::tests::matches_the_tree_built_registry (real facts.json against the NBT-built registry) and the registry/chunk/NBT tests pass.
  • cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --check: clean (Mac).

pr_ready (tools/pr_ready.py --quick on playhorizon, against origin/main at 7e5e3d1ad, 17 min 15 s)

Check Result
Scope ⚠️ 17 files, +540 −61; 601 lines of code: over 500, split it if it can ship in parts
Cargo.lock ✅ unchanged
Public tree ✅ (1 s)
Python tests ❌ 511 run, FAILED (failures=2, skipped=1) (48 s)
rustfmt ❌ failed
clippy ❌ failed
tests: horizon-client, horizon-items, horizon-world ✅ 2066 passed (12 min 51 s)
doc tests ✅ 0 passed (3 s)
content root tests ✅ 20 passed (3 min 21 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 339 affected, to run in CI (--quick): active_effect_tick, active_effects, ambient_sound, anchor, arrow, attribute_health, attribute_map_queries, attribute, attribute_updates, audio_attributes, auto_jump, bamboo and 327 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

Notes: playhorizon's toolchain has no rustfmt/clippy: cargo fmt --check and cargo clippy --workspace --all-targets -- -D warnings ran clean on the Mac; the two Python failures are that host's virtual-screen tests (python3 -m unittest discover -s tests: 511, OK on the Mac). The merge with main at c1f4a7c19 passes cargo check --workspace --all-targets. Size: 601 lines, of which the new type is ~270 and its tests ~130; the rest are call sites. It doesn't ship in useful parts: the type without the registry's sharing saves the B-tree leaves but keeps 320,000 small strings.

🤖 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/5915 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 11/1/2026 **Status:** ✅ Merged **Merged:** 11/1/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/block-state-properties` --- ### 📝 Commits (4) - [`e7f99e7`](https://github.com/horizon-rust/horizon/commit/e7f99e7ce17dbae500b7a802508fa37b1a432035) Block states: compact properties with shared strings - [`49e08de`](https://github.com/horizon-rust/horizon/commit/49e08de26d4710700f2468fadcae862ceeec6571) Block states: a palette entry's properties collect in one pass - [`b6709fe`](https://github.com/horizon-rust/horizon/commit/b6709fee4fb8854c068cf8da4e2e1ee6fb548cff) Block states: facts.json reads properties straight into shared strings - [`d44fb43`](https://github.com/horizon-rust/horizon/commit/d44fb4367a6423a17b9feca068d78c0e2534934c) Block states: shared property strings are the process's, not counted ### 📊 Changes **17 files changed** (+557 additions, -61 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/examples/mod_port_probe.rs` (+1 -1) 📝 `crates/horizon-client/examples/probe/mob_ai_probe.rs` (+1 -1) 📝 `crates/horizon-client/examples/probe/spawn_rules_fixture.rs` (+1 -1) 📝 `crates/horizon-client/src/game/blockctl.rs` (+4 -1) 📝 `crates/horizon-client/src/game/machines/native_fluids.rs` (+1 -1) 📝 `crates/horizon-client/src/game/sculk_fixture.rs` (+1 -1) 📝 `crates/horizon-client/src/save/gateway.rs` (+1 -1) 📝 `crates/horizon-items/src/items.rs` (+11 -11) ➕ `crates/horizon-world/src/block_state_properties.rs` (+418 -0) 📝 `crates/horizon-world/src/chunk.rs` (+9 -8) 📝 `crates/horizon-world/src/level/growth_tests.rs` (+2 -6) 📝 `crates/horizon-world/src/level/tests.rs` (+2 -2) 📝 `crates/horizon-world/src/lib.rs` (+1 -0) 📝 `crates/horizon-world/src/main.rs` (+1 -1) 📝 `crates/horizon-world/src/registry.rs` (+7 -3) 📝 `crates/horizon-world/src/registry/facts.rs` (+95 -22) 📝 `crates/horizon-world/src/worldgen/structure/processor/pillars.rs` (+1 -1) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What `BlockState.properties` was a `BTreeMap<String, String>`. For the registry's 35,723 vanilla states that is a 544-byte B-tree leaf plus two small string allocations per property, about 30 MB of a dedicated server's idle heap (the largest single item in a `malloc_history` of an idle server), for 158,409 properties made of only 237 distinct strings. `BlockProperties` (new, `horizon_world::chunk::BlockProperties`) keeps them as a sorted boxed slice of pairs and reads like the map it replaces: `get` (still `&String`), `get_mut`, `insert`, `remove`, `contains_key`, `iter`/`keys`/`values` in name order, `properties["facing"]` (panics on a missing key as the map does), `FromIterator`/`Extend`/`From<[_; N]>`/`From<BTreeMap>` with the map's "last value of a name wins", and equality, order, hash and `Debug` pair by pair, as the map's. Serde reads and writes it as a map. - The registry shares its states' strings: `facts.json` is read straight into one shared string per distinct name or value, interned once for the process (no owned string per property, no map), and `from_states` shares whatever arrives owned (`from_nbt`). - States made elsewhere (chunk palettes, commands, structure templates) own their strings, as before: nothing is interned from player input. Changing a shared value (`get_mut`) copies it first. - `BlockState::from_nbt` collects a palette entry's properties in one pass. Callers change only where they named the type: `&BTreeMap<String, String>` parameters that receive a state's properties (loot context, the pillars processor) and struct literals that passed a map (`.into()`). ## Measured playhorizon, `--cpus 0-3 --exclusive`, dedicated server, new world, fresh data folders per start; main = 7e5e3d1ad. | | main | this PR | |---|---|---| | live heap, idle (counting allocator, 5 starts each, `Arc` version) | 167.9 MB | **154.3 MB** (−13.6 MB, every start) | | live heap after the core tables | 77.6 MB | 64.1 MB | | RSS after the core tables | 131-140 MB | 119-121 MB | | idle RSS 32 s after Done, THP on (4 alternated starts, median) | 299.5 MB | **281.5 MB** (−18 MB, 4/4) | | idle RSS, `MIMALLOC_ALLOW_THP=0` (`[engine] low_memory`), 2+3 starts | 241.7 MB | **224.7 MB** (−17 MB, every start) | | Done (4 alternated starts, median) | 2.06 s | **1.955 s** (−105 ms, 4/4) | | `load core tables` (median) | 0.70 s | 0.68 s | | 20 bots, MSPT (3 alternated rounds) | 27.64 / 28.05 / 25.12 | 27.81 / 25.47 / 25.63 (same) | | 20 bots, `streaming` span | 3.73 / 3.55 / 3.57 ms | 3.60 / 3.50 / 3.50 ms | The first version shared the strings through `Arc`s: the same memory, but 20 bots were slower 3/3 (`streaming` +0.25 ms), world generation threads cloning the same states contended on the counters. Shared strings are now interned once for the process (`&'static String`: the registries' names and values only, a few hundred, never a player's), so a clone copies pointers. The live-heap counter counts requested bytes, so it undercounts the small strings (5-10 requested bytes, a 16-byte block each); RSS moves more than the live counter but is noisy with transparent huge pages on, hence the THP-off row. ## Tests - `block_state_properties` tests: reads and writes as a `BTreeMap` (get, insert, get_mut, remove, iteration order, `Debug`, conversion back), collects as a map does (duplicate names), orders/compares/hashes as the map for a set of cases, shared strings read as owned ones and a change to one state's value leaves the other state's. - `registry::facts::tests::matches_the_tree_built_registry` (real `facts.json` against the NBT-built registry) and the registry/chunk/NBT tests pass. - `cargo clippy --workspace --all-targets -- -D warnings`, `cargo fmt --check`: clean (Mac). **pr_ready** (`tools/pr_ready.py --quick` on playhorizon, against origin/main at 7e5e3d1ad, 17 min 15 s) | Check | Result | | --- | --- | | Scope | ⚠️ 17 files, +540 −61; 601 lines of code: over 500, split it if it can ship in parts | | Cargo.lock | ✅ unchanged | | Public tree | ✅ (1 s) | | Python tests | ❌ 511 run, FAILED (failures=2, skipped=1) (48 s) | | rustfmt | ❌ failed | | clippy | ❌ failed | | tests: horizon-client, horizon-items, horizon-world | ✅ 2066 passed (12 min 51 s) | | doc tests | ✅ 0 passed (3 s) | | content root tests | ✅ 20 passed (3 min 21 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 339 affected, to run in CI (--quick): active_effect_tick, active_effects, ambient_sound, anchor, arrow, attribute_health, attribute_map_queries, attribute, attribute_updates, audio_attributes, auto_jump, bamboo and 327 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | Notes: playhorizon's toolchain has no rustfmt/clippy: `cargo fmt --check` and `cargo clippy --workspace --all-targets -- -D warnings` ran clean on the Mac; the two Python failures are that host's virtual-screen tests (`python3 -m unittest discover -s tests`: 511, OK on the Mac). The merge with main at c1f4a7c19 passes `cargo check --workspace --all-targets`. Size: 601 lines, of which the new type is ~270 and its tests ~130; the rest are call sites. It doesn't ship in useful parts: the type without the registry's sharing saves the B-tree leaves but keeps 320,000 small strings. 🤖 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 06:22:59 +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#6558
No description provided.