[PR #309] [MERGED] Load gameplay facts with typed decoding and interned shapes #2477

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

📋 Pull Request Information

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

Base: main ← Head: perf/20261015-typed-block-facts


📝 Commits (2)

  • bea9d4b perf(client): decode gameplay facts without a JSON document tree
  • ef92004 test(client): cache legacy shape identity keys

📊 Changes

4 files changed (+576 additions, -60 deletions)

View changed files

➕ crates/horizon-client/examples/block_facts_bench.rs (+104 -0)
📝 crates/horizon-client/src/blocks.rs (+45 -60)
➕ crates/horizon-client/src/blocks/decode.rs (+290 -0)
➕ crates/horizon-client/src/blocks/decode_tests.rs (+137 -0)

📄 Description

What and why

Deserialize the ~53 MiB gameplay-facts document into typed records without building a document-sized Value tree. Borrow repeated shape JSON and map it to interned IDs; build geometry only for a new canonical shape. Structural keys preserve the previous serialized-JSON identity, including integer/float distinctions, signed zero, extra coordinates, ignored entries and object key normalization. String interning also avoids allocating a lookup key on every hit.

Preserves permissive defaults, duplicate-key behavior, fallback shapes and registry validation. A validation pass without a retained tree preserves JSON syntax, numeric-range and recursion errors before schema checks. Public facts and caller APIs are unchanged.

Measurements

Ryzen 5 5600X (12 logical CPUs), Linux, rustc 1.99.0. Results from the same locked baseline/candidate session:

Measurement Baseline Candidate
Steady loader median (9 samples) 655.192 ms 259.882 ms
Fresh process, warm files: loader median (7 alternating pairs) 841.112 ms 258.034 ms
Fresh process, requested cold files: loader median (5 alternating pairs) 878.526 ms 291.591 ms
Warm fresh-process peak RSS median 679,704 KiB 205,692 KiB
Requested-cold fresh-process peak RSS median 679,912 KiB 205,692 KiB
Warm fresh-process total wall median, including registry setup 1,110.010 ms 518.889 ms
Requested-cold fresh-process total wall median, including registry setup 1,140.615 ms 555.261 ms

Steady loading is 2.52× faster; warm fresh-process peak RSS is 69.74% lower (663.8 → 200.9 MiB). Registry setup is unchanged: warm fresh-process medians 226.582 / 225.144 ms, requested-cold medians 227.674 / 226.925 ms. Retained facts/sharing are identical; the validation pass and borrowed input buffer are included in these costs. This measures fact loading, not total client startup or frame time.

All loader samples in milliseconds, in measurement order:

  • Steady baseline: [651.980, 656.283, 672.763, 652.569, 656.676, 668.928, 652.527, 655.192, 652.364]
  • Steady candidate: [267.717, 261.829, 258.741, 260.343, 258.454, 259.486, 259.882, 261.073, 256.048]
  • Warm fresh baseline: [852.740, 836.035, 841.112, 852.691, 844.485, 840.632, 840.715]
  • Warm fresh candidate: [257.886, 256.748, 262.337, 260.400, 259.676, 258.017, 258.034]
  • Requested-cold fresh baseline: [885.861, 900.301, 878.526, 863.366, 876.803]
  • Requested-cold fresh candidate: [291.157, 292.608, 291.591, 290.452, 291.824]

Warm peak RSS ranges: baseline 679,476–680,788 KiB; candidate 205,576–206,604 KiB. Requested-cold ranges: baseline 679,640–680,004 KiB; candidate 205,444–205,768 KiB.

The committed block_facts_bench example loads the real prepared registry from facts.json, then calls the complete production BlockFacts::load. Registry setup is timed separately. Loader times include file reads, validation, decoding, interning and destruction of loader temporaries; final facts destruction is excluded from loader timing and included in whole-process timing.

The PR is rebased onto main 050b2038 to resolve a manifest-only conflict. Main already provides the same Serde derive dependency, so the PR now has no manifest or lockfile changes. The loader, decoder, benchmark and compatibility-test blobs are unchanged by the rebase; main’s baseline blocks.rs is unchanged from the requested measurement base. The measurements below remain anchored to the specified original base.

Both binaries use the workspace fast profile (release optimization, no LTO, 16 codegen units), built with CARGO_BUILD_JOBS=2. The original binary was built from the unmodified loader at 89b58991bbc02eb79b6965fc4ca56357fe2297d9 using the same example, before implementation. All builds/tests and the complete paired session hold /tmp/horizon-optimizations-20261015-heavy.lock.

Real 26.3 input sizes: gameplay-facts.json 55,723,036 bytes, block-behaviours.json 1,982,156 bytes, registry input facts.json 10,126,974 bytes. No game/prepared data is included in this PR.

Reproduce production timing with:

CARGO_BUILD_JOBS=2 cargo build --profile fast -p horizon-client --example block_facts_bench
# Preserve binaries from the base and candidate builds, then alternate their order.
./block_facts_bench RESOURCE_ROOT 9
./block_facts_bench RESOURCE_ROOT digest

Nine steady samples follow a separately reported first load and two warmups. Additionally run seven alternating pairs of fresh processes with warm file caches and five alternating pairs after os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED) on each of the three input files. This requests cold file pages; it is not a system-wide cache flush. Every fresh run uses SAMPLES=1. For peak RSS on this Linux host, which has no /usr/bin/time, use:

python3 -c 'import resource,subprocess,sys; subprocess.run(sys.argv[1:],check=True); print(resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss)' ./block_facts_bench RESOURCE_ROOT 1

Peak RSS is Linux resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss, collected by a new Python wrapper for each process; whole-process wall time wraps that one subprocess. Peak RSS includes registry setup. Measure on an otherwise idle heavy-work lock; do not include builds in the measurements.

Vanilla reference

The unchanged VanillaGameplayFactsExporter.states export reads vanilla 26.3 block-state accessors, including BlockBehaviour.BlockStateBase.getShape, getOffset, getMapColor, and sound/light/collision facts. This change only imports that existing export; it changes no gameplay queries, iteration order or RNG calls.

Compatibility tests compare shape geometry and the complete sharing relation against the original Value/string-key rule, including malformed fields and shape fallbacks. The real-resource benchmark fingerprints every StateFacts field, air ID, and the sharing partition across both outline arrays and edges. Baseline fingerprint: 35723 states, 1731 shared shapes, d13cf38a769aabde.

Self-check

The completed initial self-check is below. The reported test-only Clippy issue is fixed in ef92004e; GitHub verify subsequently passed on current head ef92004e after the manifest-only rebase. It runs public-tree/history checks, Python tests, rustfmt, workspace Clippy with warnings denied, and workspace Rust tests. The redundant queued local repeat was cancelled after that success; no completed local repeat is claimed.

pr_ready (tools/pr_ready.py --base 89b58991bbc02eb79b6965fc4ca56357fe2297d9 on archlinux, against 89b58991bb at 89b58991b, 4 min 20 s)

Check Result
Scope ⚠️ 6 files, +577 −60; 637 lines of code: over 500, split it if it can ship in parts
Cargo.lock ✅ changes with a Cargo.toml
Public tree ✅
Python tests ➖ no Python change
rustfmt ✅ (4 s)
clippy ❌ failed (16 s)
tests: workspace ✅ 963 passed (4 min 0 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 300 affected, to run in CI (no vanilla data here): active_effect_tick, active_effects, ambient_sound, anchor, arrow, attribute_health, attribute_map_queries, attribute, attribute_updates, audio_attributes, auto_jump, bamboo and 288 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

The initial Clippy cmp_owned error in the compatibility test was fixed by caching legacy serialized keys; comparing Value directly would erase the signed-zero distinction. All 963 workspace tests passed in that run.

The isolated worktree stages the existing prepared resource inputs for loading benchmarks, but not the full Java oracle environment. pr_ready lists 300 parity harnesses for CI; no full Java/world parity run is claimed here. Real-export fact/sharing fingerprints and the focused compatibility tests passed.

The scope warning includes the benchmark and compatibility tests. After rebasing onto main’s existing Serde dependency, the PR contains only four loader/benchmark/test files; 395 changed lines are production loader 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/309 **Author:** [@rdbtCVS](https://github.com/rdbtCVS) **Created:** 10/15/2026 **Status:** ✅ Merged **Merged:** 10/15/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/20261015-typed-block-facts` --- ### 📝 Commits (2) - [`bea9d4b`](https://github.com/horizon-rust/horizon/commit/bea9d4b7654f541bb0ff194f9135c43688b485ab) perf(client): decode gameplay facts without a JSON document tree - [`ef92004`](https://github.com/horizon-rust/horizon/commit/ef92004ede937e55ba79688181f84a3be12b6ddc) test(client): cache legacy shape identity keys ### 📊 Changes **4 files changed** (+576 additions, -60 deletions) <details> <summary>View changed files</summary> ➕ `crates/horizon-client/examples/block_facts_bench.rs` (+104 -0) 📝 `crates/horizon-client/src/blocks.rs` (+45 -60) ➕ `crates/horizon-client/src/blocks/decode.rs` (+290 -0) ➕ `crates/horizon-client/src/blocks/decode_tests.rs` (+137 -0) </details> ### 📄 Description ## What and why Deserialize the ~53 MiB gameplay-facts document into typed records without building a document-sized `Value` tree. Borrow repeated shape JSON and map it to interned IDs; build geometry only for a new canonical shape. Structural keys preserve the previous serialized-JSON identity, including integer/float distinctions, signed zero, extra coordinates, ignored entries and object key normalization. String interning also avoids allocating a lookup key on every hit. Preserves permissive defaults, duplicate-key behavior, fallback shapes and registry validation. A validation pass without a retained tree preserves JSON syntax, numeric-range and recursion errors before schema checks. Public facts and caller APIs are unchanged. ### Measurements Ryzen 5 5600X (12 logical CPUs), Linux, rustc 1.99.0. Results from the same locked baseline/candidate session: | Measurement | Baseline | Candidate | | --- | ---: | ---: | | Steady loader median (9 samples) | 655.192 ms | 259.882 ms | | Fresh process, warm files: loader median (7 alternating pairs) | 841.112 ms | 258.034 ms | | Fresh process, requested cold files: loader median (5 alternating pairs) | 878.526 ms | 291.591 ms | | Warm fresh-process peak RSS median | 679,704 KiB | 205,692 KiB | | Requested-cold fresh-process peak RSS median | 679,912 KiB | 205,692 KiB | | Warm fresh-process total wall median, including registry setup | 1,110.010 ms | 518.889 ms | | Requested-cold fresh-process total wall median, including registry setup | 1,140.615 ms | 555.261 ms | Steady loading is **2.52× faster**; warm fresh-process peak RSS is **69.74% lower** (663.8 → 200.9 MiB). Registry setup is unchanged: warm fresh-process medians 226.582 / 225.144 ms, requested-cold medians 227.674 / 226.925 ms. Retained facts/sharing are identical; the validation pass and borrowed input buffer are included in these costs. This measures fact loading, not total client startup or frame time. All loader samples in milliseconds, in measurement order: - Steady baseline: `[651.980, 656.283, 672.763, 652.569, 656.676, 668.928, 652.527, 655.192, 652.364]` - Steady candidate: `[267.717, 261.829, 258.741, 260.343, 258.454, 259.486, 259.882, 261.073, 256.048]` - Warm fresh baseline: `[852.740, 836.035, 841.112, 852.691, 844.485, 840.632, 840.715]` - Warm fresh candidate: `[257.886, 256.748, 262.337, 260.400, 259.676, 258.017, 258.034]` - Requested-cold fresh baseline: `[885.861, 900.301, 878.526, 863.366, 876.803]` - Requested-cold fresh candidate: `[291.157, 292.608, 291.591, 290.452, 291.824]` Warm peak RSS ranges: baseline 679,476–680,788 KiB; candidate 205,576–206,604 KiB. Requested-cold ranges: baseline 679,640–680,004 KiB; candidate 205,444–205,768 KiB. The committed `block_facts_bench` example loads the real prepared registry from `facts.json`, then calls the complete production `BlockFacts::load`. Registry setup is timed separately. Loader times include file reads, validation, decoding, interning and destruction of loader temporaries; final facts destruction is excluded from loader timing and included in whole-process timing. The PR is rebased onto main `050b2038` to resolve a manifest-only conflict. Main already provides the same Serde derive dependency, so the PR now has no manifest or lockfile changes. The loader, decoder, benchmark and compatibility-test blobs are unchanged by the rebase; main’s baseline `blocks.rs` is unchanged from the requested measurement base. The measurements below remain anchored to the specified original base. Both binaries use the workspace `fast` profile (release optimization, no LTO, 16 codegen units), built with `CARGO_BUILD_JOBS=2`. The original binary was built from the unmodified loader at `89b58991bbc02eb79b6965fc4ca56357fe2297d9` using the same example, before implementation. All builds/tests and the complete paired session hold `/tmp/horizon-optimizations-20261015-heavy.lock`. Real 26.3 input sizes: `gameplay-facts.json` 55,723,036 bytes, `block-behaviours.json` 1,982,156 bytes, registry input `facts.json` 10,126,974 bytes. No game/prepared data is included in this PR. Reproduce production timing with: ```sh CARGO_BUILD_JOBS=2 cargo build --profile fast -p horizon-client --example block_facts_bench # Preserve binaries from the base and candidate builds, then alternate their order. ./block_facts_bench RESOURCE_ROOT 9 ./block_facts_bench RESOURCE_ROOT digest ``` Nine steady samples follow a separately reported first load and two warmups. Additionally run seven alternating pairs of fresh processes with warm file caches and five alternating pairs after `os.posix_fadvise(fd, 0, 0, os.POSIX_FADV_DONTNEED)` on each of the three input files. This requests cold file pages; it is not a system-wide cache flush. Every fresh run uses `SAMPLES=1`. For peak RSS on this Linux host, which has no `/usr/bin/time`, use: ```sh python3 -c 'import resource,subprocess,sys; subprocess.run(sys.argv[1:],check=True); print(resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss)' ./block_facts_bench RESOURCE_ROOT 1 ``` Peak RSS is Linux `resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss`, collected by a new Python wrapper for each process; whole-process wall time wraps that one subprocess. Peak RSS includes registry setup. Measure on an otherwise idle heavy-work lock; do not include builds in the measurements. ## Vanilla reference The unchanged `VanillaGameplayFactsExporter.states` export reads vanilla 26.3 block-state accessors, including `BlockBehaviour.BlockStateBase.getShape`, `getOffset`, `getMapColor`, and sound/light/collision facts. This change only imports that existing export; it changes no gameplay queries, iteration order or RNG calls. Compatibility tests compare shape geometry and the complete sharing relation against the original Value/string-key rule, including malformed fields and shape fallbacks. The real-resource benchmark fingerprints every `StateFacts` field, air ID, and the sharing partition across both outline arrays and edges. Baseline fingerprint: `35723` states, `1731` shared shapes, `d13cf38a769aabde`. ## Self-check The completed initial self-check is below. The reported test-only Clippy issue is fixed in `ef92004e`; GitHub [verify](https://github.com/ludovic111/horizon/actions/runs/38716797354) subsequently passed on current head `ef92004e` after the manifest-only rebase. It runs public-tree/history checks, Python tests, rustfmt, workspace Clippy with warnings denied, and workspace Rust tests. The redundant queued local repeat was cancelled after that success; no completed local repeat is claimed. **pr_ready** (`tools/pr_ready.py --base 89b58991bbc02eb79b6965fc4ca56357fe2297d9` on archlinux, against 89b58991bbc02eb79b6965fc4ca56357fe2297d9 at 89b58991b, 4 min 20 s) | Check | Result | | --- | --- | | Scope | ⚠️ 6 files, +577 −60; 637 lines of code: over 500, split it if it can ship in parts | | Cargo.lock | ✅ changes with a Cargo.toml | | Public tree | ✅ | | Python tests | ➖ no Python change | | rustfmt | ✅ (4 s) | | clippy | ❌ failed (16 s) | | tests: workspace | ✅ 963 passed (4 min 0 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 300 affected, to run in CI (no vanilla data here): active_effect_tick, active_effects, ambient_sound, anchor, arrow, attribute_health, attribute_map_queries, attribute, attribute_updates, audio_attributes, auto_jump, bamboo and 288 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | The initial Clippy `cmp_owned` error in the compatibility test was fixed by caching legacy serialized keys; comparing `Value` directly would erase the signed-zero distinction. All 963 workspace tests passed in that run. The isolated worktree stages the existing prepared resource inputs for loading benchmarks, but not the full Java oracle environment. `pr_ready` lists 300 parity harnesses for CI; no full Java/world parity run is claimed here. Real-export fact/sharing fingerprints and the focused compatibility tests passed. The scope warning includes the benchmark and compatibility tests. After rebasing onto main’s existing Serde dependency, the PR contains only four loader/benchmark/test files; 395 changed lines are production loader code. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 15:56:31 +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#2477
No description provided.