[PR #6041] [MERGED] Keep loot tables as their JSON, read on first roll (−7 MB idle) #6621

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

📋 Pull Request Information

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

Base: main ← Head: perf/lazy-loot-tables


📝 Commits (1)

  • 19fcd0d Keep loot tables as their JSON, read on first roll

📊 Changes

4 files changed (+166 additions, -13 deletions)

View changed files

📝 crates/horizon-items/src/items.rs (+1 -1)
➕ crates/horizon-world/src/lazy_json.rs (+84 -0)
📝 crates/horizon-world/src/lib.rs (+1 -0)
📝 crates/horizon-world/src/loot.rs (+80 -12)

📄 Description

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

What

The loot tables (loot::shared_tables, shared by Items and LootData) were held as serde_json::Values: vanilla's ~1,450 tables are 0.6 MB of JSON but ~10,450 objects, and each object is a B-tree node (~0.5 KB) in a Value, so the map held ~7 MB of an idle server's heap, almost all of it for tables nothing rolls.

LootTables keeps each table as its JSON text and reads it into a Value the first time it is read (LazyJson, new in horizon_world::lazy_json: the text, plus a OnceLock<Value>). It reads like the map it replaces (get, contains_key, ["id"]), so callers are unchanged.

  • Every table is still read whole when it loads: a file that is not JSON fails the load as before, and the native hook (native_loot_table_load) still runs on each table's Value, in id order, before anything rolls it; the text is written from the Value after the hook.
  • A table read once stays read: a block or mob table is parsed on its first drop (a few µs), then reads as before.

Measured

playhorizon, --cpus 0-3 --exclusive, dedicated server, new world, fresh data folders per start, alternated with main 5bbecdc49.

main this PR
idle anonymous memory, MIMALLOC_ALLOW_THP=0 (4 starts each) 185.9 / 185.9 / 185.4 / 186.2 MB 178.8 / 179.6 / 178.5 / 178.6 MB (−7.2 MB, every start)
idle RSS, THP off 224.4 / 224.7 / 224.3 / 225.0 MB 218.7 / 219.4 / 218.0 / 218.4 MB (−6 MB)
idle RSS, THP on (3 starts) 286.2 / 282.9 / 286.1 MB 275.9 / 289.2 / 275.0 MB
Done (median, THP off) 1.860 s 1.861 s (same)
20 bots, MSPT (5 alternated rounds) 33.85¹ / 25.22 / 27.32 / 29.25 / 26.75 25.63 / 25.07 / 25.94 / 28.54 / 26.92 (same)
20 bots, streaming span 3.35 / 3.49 / 3.52 / 3.72 / 3.78 ms 3.51 / 3.54 / 4.10 / 3.55 / 3.59 ms

¹ that round ran beside a pr_ready on the host's other CPUs. The memory rows were measured on 88b0ff375, before LazyJson was split out of LootTables (the same code in its own module).

Tests

  • lazy_json::tests::reads_back_as_written: floats, integers past 2^53, escapes and nesting read back as the Value written; read gives it without keeping it.
  • loot::namespace_tests::loot_tables_read_back_as_loaded: a table from disk reads back as the file's JSON; a table that is not JSON fails the load.
  • 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 8c02c0b85, 9 min 1 s)

Check Result
Scope ✅ 4 files, +166 −13
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ❌ 515 run, FAILED (failures=2, skipped=1) (53 s)
rustfmt ❌ failed (1 s)
clippy ❌ failed
tests: horizon-items, horizon-world ✅ 410 passed (3 min 16 s)
doc tests ✅ 0 passed (3 s)
content root tests ✅ 21 passed (4 min 37 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, so both ran on the Mac (cargo fmt --check and cargo clippy --workspace --all-targets -- -D warnings, clippy 0.1.99: clean). The two Python failures are that host's virtual-screen tests (515 OK on the Mac). Players see no change: skip-release.

🤖 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/6041 **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/lazy-loot-tables` --- ### 📝 Commits (1) - [`19fcd0d`](https://github.com/horizon-rust/horizon/commit/19fcd0d8a9ed5c816c76a3a1f02303fd174a8900) Keep loot tables as their JSON, read on first roll ### 📊 Changes **4 files changed** (+166 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-items/src/items.rs` (+1 -1) ➕ `crates/horizon-world/src/lazy_json.rs` (+84 -0) 📝 `crates/horizon-world/src/lib.rs` (+1 -0) 📝 `crates/horizon-world/src/loot.rs` (+80 -12) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What The loot tables (`loot::shared_tables`, shared by `Items` and `LootData`) were held as `serde_json::Value`s: vanilla's ~1,450 tables are 0.6 MB of JSON but ~10,450 objects, and each object is a B-tree node (~0.5 KB) in a `Value`, so the map held ~7 MB of an idle server's heap, almost all of it for tables nothing rolls. `LootTables` keeps each table as its JSON text and reads it into a `Value` the first time it is read (`LazyJson`, new in `horizon_world::lazy_json`: the text, plus a `OnceLock<Value>`). It reads like the map it replaces (`get`, `contains_key`, `["id"]`), so callers are unchanged. - Every table is still read whole when it loads: a file that is not JSON fails the load as before, and the native hook (`native_loot_table_load`) still runs on each table's `Value`, in id order, before anything rolls it; the text is written from the `Value` after the hook. - A table read once stays read: a block or mob table is parsed on its first drop (a few µs), then reads as before. ## Measured playhorizon, `--cpus 0-3 --exclusive`, dedicated server, new world, fresh data folders per start, alternated with main 5bbecdc49. | | main | this PR | |---|---|---| | idle anonymous memory, `MIMALLOC_ALLOW_THP=0` (4 starts each) | 185.9 / 185.9 / 185.4 / 186.2 MB | **178.8 / 179.6 / 178.5 / 178.6 MB** (−7.2 MB, every start) | | idle RSS, THP off | 224.4 / 224.7 / 224.3 / 225.0 MB | **218.7 / 219.4 / 218.0 / 218.4 MB** (−6 MB) | | idle RSS, THP on (3 starts) | 286.2 / 282.9 / 286.1 MB | 275.9 / 289.2 / 275.0 MB | | Done (median, THP off) | 1.860 s | 1.861 s (same) | | 20 bots, MSPT (5 alternated rounds) | 33.85¹ / 25.22 / 27.32 / 29.25 / 26.75 | 25.63 / 25.07 / 25.94 / 28.54 / 26.92 (same) | | 20 bots, `streaming` span | 3.35 / 3.49 / 3.52 / 3.72 / 3.78 ms | 3.51 / 3.54 / 4.10 / 3.55 / 3.59 ms | ¹ that round ran beside a pr_ready on the host's other CPUs. The memory rows were measured on 88b0ff375, before `LazyJson` was split out of `LootTables` (the same code in its own module). ## Tests - `lazy_json::tests::reads_back_as_written`: floats, integers past 2^53, escapes and nesting read back as the `Value` written; `read` gives it without keeping it. - `loot::namespace_tests::loot_tables_read_back_as_loaded`: a table from disk reads back as the file's JSON; a table that is not JSON fails the load. - `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 8c02c0b85, 9 min 1 s) | Check | Result | | --- | --- | | Scope | ✅ 4 files, +166 −13 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ❌ 515 run, FAILED (failures=2, skipped=1) (53 s) | | rustfmt | ❌ failed (1 s) | | clippy | ❌ failed | | tests: horizon-items, horizon-world | ✅ 410 passed (3 min 16 s) | | doc tests | ✅ 0 passed (3 s) | | content root tests | ✅ 21 passed (4 min 37 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, so both ran on the Mac (`cargo fmt --check` and `cargo clippy --workspace --all-targets -- -D warnings`, clippy 0.1.99: clean). The two Python failures are that host's virtual-screen tests (515 OK on the Mac). Players see no change: `skip-release`. 🤖 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:23: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#6621
No description provided.