[PR #318] [MERGED] Cache random-tick section eligibility across unchanged chunks #2486

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

📋 Pull Request Information

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

Base: main ← Head: perf/20261015-random-tick-eligibility


📝 Commits (3)

  • 0c161a0 Cache random-tick section eligibility for unchanged chunks
  • fac4944 Document transient chunk wrapper retention
  • ed0febf Merge main and preserve tick chunk membership invalidation

📊 Changes

6 files changed (+579 additions, -13 deletions)

View changed files

➕ crates/horizon-client/examples/random_tick_eligibility_bench.rs (+149 -0)
📝 crates/horizon-client/src/game.rs (+7 -13)
📝 crates/horizon-client/src/game/packs.rs (+1 -0)
📝 crates/horizon-client/src/lib.rs (+1 -0)
➕ crates/horizon-client/src/random_tick_sections.rs (+353 -0)
➕ docs/random-tick-eligibility-benchmark.md (+68 -0)

📄 Description

What and why

Integration update: merged current main (050b2038) in ed0febf1, preserving its TickChunkMembership and pruning cache entries through removed(). The PR still changes only the six optimization files relative to current main. Fresh build, paired-measurement, focused-test and repository-check phases are queued with separate heavy-lock acquisitions. Results below currently describe the initial 89b58991 base and will be refreshed after revalidation.

Game::tick_blocks scans every eligible chunk's section palettes and allocates section lists on every tick. Cache the palette eligibility as a 24-bit mask while the resident chunk is unchanged. A weak chunk handle detects edits through Arc::make_mut and replacement without retaining unloaded block data. Unloads remove entries and successful resource reloads clear the cache.

Preserve the world map's chunk order, ascending section order, the snapshot before entity-inside/scheduled updates, unused/global palette entries, and calls for empty chunks (which still consume precipitation RNG).

Paired optimized fast profile measurements on Ryzen 5 5600X / Linux / rustc 1.99.0: one discarded warmup pair, seven pairs of 300 ticks, alternating original/candidate order, under the shared heavy-work lock. Real prepared 26.3 facts; 289 chunks of 24 sections. Sparse uses one inactive state per section, dense 128; every third section also has grass. Selection and writes are timed; this is not whole-game tick latency. Initial baseline was measured before implementation. Cold includes cache construction/destruction, reload includes refill with retained capacity, and edit rows include weak-handle-induced chunk allocation.

Palette Workload Original Cached Speedup
sparse steady 48.855 2.588 18.88x
sparse cold 50.319 39.110 1.29x
sparse reload 49.051 36.167 1.36x
sparse edit_1pct 49.110 2.850 17.23x
sparse edit_all 59.425 50.759 1.17x
dense steady 1311.924 4.397 298.37x
dense cold 1305.237 553.209 2.36x
dense reload 1292.879 542.848 2.38x
dense edit_1pct 1290.162 9.102 141.74x
dense edit_all 1303.115 561.606 2.32x

Resource loading: 1086 ms; fixture construction: 85 ms sparse / 375 ms dense, shared by both paths. Cache storage is about 13 KiB for 289 entries. The first edit can allocate/move the 176-byte chunk wrapper plus Arc bookkeeping; it does not force a deep block-data copy. Old wrapper allocations can remain until the next query/removal (about 54 KiB for 289 edited chunks, including Arc bookkeeping). Lighting/metadata changes conservatively invalidate eligibility. The outer tick-vector allocation remains. See repeatable benchmark method.

All paired samples, microseconds per tick
sparse steady baseline_us=[51.703, 48.680, 49.048, 48.679, 48.532, 48.855, 48.866] candidate_us=[2.587, 2.614, 2.590, 2.588, 2.585, 2.592, 2.574]
sparse cold baseline_us=[54.192, 51.253, 48.574, 48.488, 50.409, 50.319, 50.079] candidate_us=[39.077, 46.267, 44.924, 38.835, 38.862, 39.110, 42.785]
sparse reload baseline_us=[48.887, 49.301, 49.127, 48.904, 49.129, 49.045, 49.051] candidate_us=[36.079, 35.781, 36.078, 36.688, 36.167, 36.203, 38.523]
sparse edit_1pct baseline_us=[49.288, 49.085, 49.304, 48.993, 49.110, 49.358, 49.110] candidate_us=[2.841, 2.853, 2.842, 2.897, 3.089, 2.850, 2.841]
sparse edit_all baseline_us=[59.249, 59.424, 62.439, 60.678, 59.287, 59.425, 59.562] candidate_us=[50.405, 50.759, 50.972, 50.763, 50.564, 53.453, 50.544]
dense steady baseline_us=[1315.775, 1314.691, 1311.924, 1312.093, 1310.344, 1310.937, 1297.341] candidate_us=[4.618, 4.397, 4.323, 4.418, 4.337, 4.396, 4.430]
dense cold baseline_us=[1304.186, 1305.237, 1301.026, 1314.676, 1319.963, 1319.096, 1290.440] candidate_us=[549.781, 546.646, 546.533, 553.209, 554.094, 556.280, 554.880]
dense reload baseline_us=[1308.562, 1309.415, 1292.613, 1295.405, 1292.879, 1292.671, 1291.540] candidate_us=[587.442, 544.934, 546.152, 542.848, 540.594, 538.586, 540.648]
dense edit_1pct baseline_us=[1293.701, 1291.926, 1288.721, 1290.162, 1287.422, 1297.670, 1289.483] candidate_us=[9.042, 9.130, 9.057, 9.102, 9.995, 9.132, 9.096]
dense edit_all baseline_us=[1303.115, 1301.026, 1300.817, 1302.863, 1304.522, 1304.421, 1320.489] candidate_us=[558.905, 561.209, 561.606, 563.090, 559.738, 563.785, 561.865]

Vanilla reference

Inspected the pinned server-game bytecode with javap -c -p: ServerLevel.tickChunk (precipitation before ascending section ticks), and LevelChunkSection.isRandomlyTicking, isRandomlyTickingBlocks, isRandomlyTickingFluids. Existing Horizon maybe_has eligibility semantics are preserved.

Four focused tests passed with real prepared resources. The resource-backed test runs actual Behaviours::tick_chunk_random for 128 ticks and compares ordered block reads, side effects, final blocks, position LCG and level RNG against the original scan, including speed changes, edits after capture, insertion/removal, lighting eligibility and changed facts. Additional tests cover cache hits, worker snapshots, replacement, compaction, unused/global palette entries and ascending section order.

Self-check

Local validation status (focused results from the initial base; integrated-source reruns are queued):

GitHub required CI verify passed on ed0febf163d5fcc219105c21264e968e9b60ef3a (run). This job covers repository formatting, clippy and workspace tests. The separate parity, vanilla-parity and portable-mod CI jobs were skipped; their status is not counted as a pass. The integrated-source local build has not started yet: its existing command remains queued behind the shared heavy-work lock, with no duplicate launched.

  • cargo fmt --all -- --check: passed.
  • cargo test --profile fast -p horizon-client --lib random_tick_sections -- --nocapture: 4 passed with prepared resources.
  • Public-tree hook: passed for both commits.
  • pr_ready --quick: the old not-started queued run was verified and cancelled for conflict resolution; a new run against current main is queued under the shared heavy-work lock; local clippy/full-crate test results are pending. Repository CI runs formatting, clippy and workspace tests on pull requests.

The full pr_ready execution produced no final Markdown block before termination. Its captured progress was:

pr_ready: 6 files changed since 89b58991bbc02eb79b6965fc4ca56357fe2297d9; checks on local
  Public tree...
  Public tree: pass (0 s)
  Parity...

The exact full command python3 tools/pr_ready.py --base 89b58991bbc02eb79b6965fc4ca56357fe2297d9 was also attempted under the shared lock. It passed the public-tree check, built its parity probes and recorded six parity harness passes with zero failures before the execution session was terminated by SIGTERM (exit 143). It produced no final report; the full sweep is incomplete. No surviving child processes remained, and the signal sender is unknown. The queued --quick run will supply the remaining local repository-check report; the complete vanilla sweep is not claimed to have passed.

Additional vanilla comparison: python3 tools/test_random_tick_parity.py --count 40000 exited 0: 39,998/39,998 evaluated cases matched. Two Java-oracle cases (53 and 35783, mangrove propagule) were excluded after IllegalStateException in RegistryAccess.lookupOrThrow; those cases do not establish parity. The fixture uses a built-in-registry fallback when dynamic registries are absent; the oracle and horizon-world implementation are unchanged by this PR. All outputs stayed private to this worktree.


🔄 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/318 **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-random-tick-eligibility` --- ### 📝 Commits (3) - [`0c161a0`](https://github.com/horizon-rust/horizon/commit/0c161a03364fb006db381e369556113a0b1d75cc) Cache random-tick section eligibility for unchanged chunks - [`fac4944`](https://github.com/horizon-rust/horizon/commit/fac4944cfa4d78840f0d78421a1746064866ab89) Document transient chunk wrapper retention - [`ed0febf`](https://github.com/horizon-rust/horizon/commit/ed0febf163d5fcc219105c21264e968e9b60ef3a) Merge main and preserve tick chunk membership invalidation ### 📊 Changes **6 files changed** (+579 additions, -13 deletions) <details> <summary>View changed files</summary> ➕ `crates/horizon-client/examples/random_tick_eligibility_bench.rs` (+149 -0) 📝 `crates/horizon-client/src/game.rs` (+7 -13) 📝 `crates/horizon-client/src/game/packs.rs` (+1 -0) 📝 `crates/horizon-client/src/lib.rs` (+1 -0) ➕ `crates/horizon-client/src/random_tick_sections.rs` (+353 -0) ➕ `docs/random-tick-eligibility-benchmark.md` (+68 -0) </details> ### 📄 Description ## What and why Integration update: merged current `main` (`050b2038`) in `ed0febf1`, preserving its `TickChunkMembership` and pruning cache entries through `removed()`. The PR still changes only the six optimization files relative to current main. Fresh build, paired-measurement, focused-test and repository-check phases are queued with separate heavy-lock acquisitions. Results below currently describe the initial `89b58991` base and will be refreshed after revalidation. `Game::tick_blocks` scans every eligible chunk's section palettes and allocates section lists on every tick. Cache the palette eligibility as a 24-bit mask while the resident chunk is unchanged. A weak chunk handle detects edits through `Arc::make_mut` and replacement without retaining unloaded block data. Unloads remove entries and successful resource reloads clear the cache. Preserve the world map's chunk order, ascending section order, the snapshot before entity-inside/scheduled updates, unused/global palette entries, and calls for empty chunks (which still consume precipitation RNG). Paired optimized `fast` profile measurements on Ryzen 5 5600X / Linux / rustc 1.99.0: one discarded warmup pair, seven pairs of 300 ticks, alternating original/candidate order, under the shared heavy-work lock. Real prepared 26.3 facts; 289 chunks of 24 sections. Sparse uses one inactive state per section, dense 128; every third section also has grass. Selection and writes are timed; this is not whole-game tick latency. Initial baseline was measured before implementation. Cold includes cache construction/destruction, reload includes refill with retained capacity, and edit rows include weak-handle-induced chunk allocation. | Palette | Workload | Original | Cached | Speedup | | --- | --- | ---: | ---: | ---: | | sparse | steady | 48.855 | 2.588 | 18.88x | | sparse | cold | 50.319 | 39.110 | 1.29x | | sparse | reload | 49.051 | 36.167 | 1.36x | | sparse | edit_1pct | 49.110 | 2.850 | 17.23x | | sparse | edit_all | 59.425 | 50.759 | 1.17x | | dense | steady | 1311.924 | 4.397 | 298.37x | | dense | cold | 1305.237 | 553.209 | 2.36x | | dense | reload | 1292.879 | 542.848 | 2.38x | | dense | edit_1pct | 1290.162 | 9.102 | 141.74x | | dense | edit_all | 1303.115 | 561.606 | 2.32x | Resource loading: 1086 ms; fixture construction: 85 ms sparse / 375 ms dense, shared by both paths. Cache storage is about 13 KiB for 289 entries. The first edit can allocate/move the 176-byte chunk wrapper plus Arc bookkeeping; it does not force a deep block-data copy. Old wrapper allocations can remain until the next query/removal (about 54 KiB for 289 edited chunks, including Arc bookkeeping). Lighting/metadata changes conservatively invalidate eligibility. The outer tick-vector allocation remains. See [repeatable benchmark method](docs/random-tick-eligibility-benchmark.md). <details> <summary>All paired samples, microseconds per tick</summary> ```text sparse steady baseline_us=[51.703, 48.680, 49.048, 48.679, 48.532, 48.855, 48.866] candidate_us=[2.587, 2.614, 2.590, 2.588, 2.585, 2.592, 2.574] sparse cold baseline_us=[54.192, 51.253, 48.574, 48.488, 50.409, 50.319, 50.079] candidate_us=[39.077, 46.267, 44.924, 38.835, 38.862, 39.110, 42.785] sparse reload baseline_us=[48.887, 49.301, 49.127, 48.904, 49.129, 49.045, 49.051] candidate_us=[36.079, 35.781, 36.078, 36.688, 36.167, 36.203, 38.523] sparse edit_1pct baseline_us=[49.288, 49.085, 49.304, 48.993, 49.110, 49.358, 49.110] candidate_us=[2.841, 2.853, 2.842, 2.897, 3.089, 2.850, 2.841] sparse edit_all baseline_us=[59.249, 59.424, 62.439, 60.678, 59.287, 59.425, 59.562] candidate_us=[50.405, 50.759, 50.972, 50.763, 50.564, 53.453, 50.544] dense steady baseline_us=[1315.775, 1314.691, 1311.924, 1312.093, 1310.344, 1310.937, 1297.341] candidate_us=[4.618, 4.397, 4.323, 4.418, 4.337, 4.396, 4.430] dense cold baseline_us=[1304.186, 1305.237, 1301.026, 1314.676, 1319.963, 1319.096, 1290.440] candidate_us=[549.781, 546.646, 546.533, 553.209, 554.094, 556.280, 554.880] dense reload baseline_us=[1308.562, 1309.415, 1292.613, 1295.405, 1292.879, 1292.671, 1291.540] candidate_us=[587.442, 544.934, 546.152, 542.848, 540.594, 538.586, 540.648] dense edit_1pct baseline_us=[1293.701, 1291.926, 1288.721, 1290.162, 1287.422, 1297.670, 1289.483] candidate_us=[9.042, 9.130, 9.057, 9.102, 9.995, 9.132, 9.096] dense edit_all baseline_us=[1303.115, 1301.026, 1300.817, 1302.863, 1304.522, 1304.421, 1320.489] candidate_us=[558.905, 561.209, 561.606, 563.090, 559.738, 563.785, 561.865] ``` </details> ## Vanilla reference Inspected the pinned server-game bytecode with `javap -c -p`: `ServerLevel.tickChunk` (precipitation before ascending section ticks), and `LevelChunkSection.isRandomlyTicking`, `isRandomlyTickingBlocks`, `isRandomlyTickingFluids`. Existing Horizon `maybe_has` eligibility semantics are preserved. Four focused tests passed with real prepared resources. The resource-backed test runs actual `Behaviours::tick_chunk_random` for 128 ticks and compares ordered block reads, side effects, final blocks, position LCG and level RNG against the original scan, including speed changes, edits after capture, insertion/removal, lighting eligibility and changed facts. Additional tests cover cache hits, worker snapshots, replacement, compaction, unused/global palette entries and ascending section order. ## Self-check Local validation status (focused results from the initial base; integrated-source reruns are queued): GitHub required CI **verify passed** on `ed0febf163d5fcc219105c21264e968e9b60ef3a` ([run](https://github.com/ludovic111/horizon/actions/runs/38720598050/job/116373907767)). This job covers repository formatting, clippy and workspace tests. The separate `parity`, `vanilla-parity` and `portable-mod` CI jobs were skipped; their status is not counted as a pass. The integrated-source local build has not started yet: its existing command remains queued behind the shared heavy-work lock, with no duplicate launched. - `cargo fmt --all -- --check`: passed. - `cargo test --profile fast -p horizon-client --lib random_tick_sections -- --nocapture`: 4 passed with prepared resources. - Public-tree hook: passed for both commits. - `pr_ready --quick`: the old not-started queued run was verified and cancelled for conflict resolution; a new run against current main is queued under the shared heavy-work lock; local clippy/full-crate test results are pending. Repository CI runs formatting, clippy and workspace tests on pull requests. The full `pr_ready` execution produced no final Markdown block before termination. Its captured progress was: ```text pr_ready: 6 files changed since 89b58991bbc02eb79b6965fc4ca56357fe2297d9; checks on local Public tree... Public tree: pass (0 s) Parity... ``` The exact full command `python3 tools/pr_ready.py --base 89b58991bbc02eb79b6965fc4ca56357fe2297d9` was also attempted under the shared lock. It passed the public-tree check, built its parity probes and recorded six parity harness passes with zero failures before the execution session was terminated by SIGTERM (exit 143). It produced no final report; the full sweep is **incomplete**. No surviving child processes remained, and the signal sender is unknown. The queued `--quick` run will supply the remaining local repository-check report; the complete vanilla sweep is not claimed to have passed. Additional vanilla comparison: `python3 tools/test_random_tick_parity.py --count 40000` exited 0: **39,998/39,998 evaluated cases matched**. Two Java-oracle cases (53 and 35783, mangrove propagule) were excluded after `IllegalStateException` in `RegistryAccess.lookupOrThrow`; those cases do not establish parity. The fixture uses a built-in-registry fallback when dynamic registries are absent; the oracle and horizon-world implementation are unchanged by this PR. All outputs stayed private to this worktree. --- <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:37 +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#2486
No description provided.