[PR #207] [MERGED] perf(client): parse large fact JSON from in-memory bytes #2407

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

📋 Pull Request Information

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

Base: main ← Head: perf/slice-large-json-20261014


📝 Commits (1)

  • 2217ebc perf(client): parse large fact JSON from in-memory bytes

📊 Changes

4 files changed (+208 additions, -7 deletions)

View changed files

➕ crates/horizon-client/examples/json_load_bench.rs (+124 -0)
📝 crates/horizon-client/src/assets.rs (+2 -3)
📝 crates/horizon-client/src/blocks.rs (+8 -4)
➕ tools/json_load_fixtures.py (+74 -0)

📄 Description

BlockFacts::load and Assets::load parse large fact files through serde_json's reader path. Read gameplay-facts.json and facts.json with fs::read, then parse with from_slice to reduce the cost of these two stages. Downstream registry/state processing is unchanged.

The temporary input buffer drops at the end of each parse statement. Compared with the 8 KiB BufReader buffer, this adds roughly one file's size alongside the JSON tree: ~53.1 MiB for gameplay facts and ~9.6 MiB for registry facts. The two input buffers are not retained or held together. Reading the whole file also means malformed files are fully read before parsing fails.

Benchmark

AMD Ryzen 5 5600X, Linux 7.2.8-zen1-2-zen x86_64, rustc 1.99.0, serde_json 1.0.151, Cargo fast optimized profile. No concurrent build/benchmark during measurement. Each method uses the identical file, validates full Value-tree equality, warms up twice, then measures 15 repetitions with alternating order and black_box. Times include open/read/parse and temporary byte-buffer release; Value destruction is excluded. Filesystem pages are warm.

Changed read/parse stage Fixture bytes / states BufReader median fs::read + from_slice median Speedup
gameplay-facts.json 55,700,207 / 49,522 488.730 ms 301.119 ms 1.62×
facts.json 10,114,906 / 49,807 61.767 ms 29.560 ms 2.09×

Prepared production resources were unavailable locally. Committed fixture generation mirrors the fields/nesting in VanillaGameplayFactsExporter and VanillaHeightmapOracle, using synthetic values/distributions at the reported production sizes. This compares the exact replaced read/Value-parse expressions; it does not benchmark complete BlockFacts/Assets loading, cold storage, or overall startup. Full Assets loading additionally needs model/sprite resources unavailable here. Real exports and other machines may show different gains.

Reproduce (or pass an existing resource directory to the example):

python3 tools/json_load_fixtures.py /tmp/horizon-json-fixtures
cargo run -p horizon-client --profile fast --example json_load_bench -- /tmp/horizon-json-fixtures 15

Validation

  • cargo test -p horizon-client --profile fast --example json_load_bench: 1 passed. Checks equal parsed values (including escapes/numbers/shapes), matching JSON error categories for malformed/truncated JSON, trailing data and invalid UTF-8, plus missing-file IO errors.
  • cargo build -p horizon-client --profile fast --lib --bin horizon-client --example json_load_bench: passed.
  • cargo clippy -p horizon-client --profile fast --lib --bin horizon-client --example json_load_bench -- -D warnings: passed.
  • cargo fmt --all -- --check, git diff --check, and Python fixture-generator compilation: passed.

Build/check commands used CARGO_TARGET_DIR=/home/bob/Documents/horizon/target to reuse compiled dependencies.


🔄 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/207 **Author:** [@rdbtCVS](https://github.com/rdbtCVS) **Created:** 10/14/2026 **Status:** ✅ Merged **Merged:** 10/14/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/slice-large-json-20261014` --- ### 📝 Commits (1) - [`2217ebc`](https://github.com/horizon-rust/horizon/commit/2217ebcb331921d753a21e4538d0525005436507) perf(client): parse large fact JSON from in-memory bytes ### 📊 Changes **4 files changed** (+208 additions, -7 deletions) <details> <summary>View changed files</summary> ➕ `crates/horizon-client/examples/json_load_bench.rs` (+124 -0) 📝 `crates/horizon-client/src/assets.rs` (+2 -3) 📝 `crates/horizon-client/src/blocks.rs` (+8 -4) ➕ `tools/json_load_fixtures.py` (+74 -0) </details> ### 📄 Description `BlockFacts::load` and `Assets::load` parse large fact files through serde_json's reader path. Read `gameplay-facts.json` and `facts.json` with `fs::read`, then parse with `from_slice` to reduce the cost of these two stages. Downstream registry/state processing is unchanged. The temporary input buffer drops at the end of each parse statement. Compared with the 8 KiB BufReader buffer, this adds roughly one file's size alongside the JSON tree: ~53.1 MiB for gameplay facts and ~9.6 MiB for registry facts. The two input buffers are not retained or held together. Reading the whole file also means malformed files are fully read before parsing fails. ### Benchmark AMD Ryzen 5 5600X, Linux 7.2.8-zen1-2-zen x86_64, rustc 1.99.0, serde_json 1.0.151, Cargo `fast` optimized profile. No concurrent build/benchmark during measurement. Each method uses the identical file, validates full Value-tree equality, warms up twice, then measures 15 repetitions with alternating order and black_box. Times include open/read/parse and temporary byte-buffer release; Value destruction is excluded. Filesystem pages are warm. | Changed read/parse stage | Fixture bytes / states | BufReader median | fs::read + from_slice median | Speedup | | --- | --- | --- | --- | --- | | gameplay-facts.json | 55,700,207 / 49,522 | 488.730 ms | 301.119 ms | 1.62× | | facts.json | 10,114,906 / 49,807 | 61.767 ms | 29.560 ms | 2.09× | Prepared production resources were unavailable locally. Committed fixture generation mirrors the fields/nesting in VanillaGameplayFactsExporter and VanillaHeightmapOracle, using synthetic values/distributions at the reported production sizes. This compares the exact replaced read/Value-parse expressions; it does not benchmark complete BlockFacts/Assets loading, cold storage, or overall startup. Full Assets loading additionally needs model/sprite resources unavailable here. Real exports and other machines may show different gains. Reproduce (or pass an existing resource directory to the example): ```sh python3 tools/json_load_fixtures.py /tmp/horizon-json-fixtures cargo run -p horizon-client --profile fast --example json_load_bench -- /tmp/horizon-json-fixtures 15 ``` ### Validation - `cargo test -p horizon-client --profile fast --example json_load_bench`: 1 passed. Checks equal parsed values (including escapes/numbers/shapes), matching JSON error categories for malformed/truncated JSON, trailing data and invalid UTF-8, plus missing-file IO errors. - `cargo build -p horizon-client --profile fast --lib --bin horizon-client --example json_load_bench`: passed. - `cargo clippy -p horizon-client --profile fast --lib --bin horizon-client --example json_load_bench -- -D warnings`: passed. - `cargo fmt --all -- --check`, `git diff --check`, and Python fixture-generator compilation: passed. Build/check commands used `CARGO_TARGET_DIR=/home/bob/Documents/horizon/target` to reuse compiled dependencies. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 15:55:49 +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#2407
No description provided.