[PR #1256] [MERGED] Random ticks: tick a section only while it holds a randomly ticking block or fluid (vanilla's count, not the palette) #3459

Closed
opened 2026-11-03 04:09:40 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/1256
Author: @ludovic111
Created: 10/22/2026
Status: ✅ Merged
Merged: 10/23/2026
Merged by: @ludovic111

Base: main ← Head: perf/random-tick-counts


📝 Commits (2)

  • 9d58d6f Random ticks: a section ticks while it holds a randomly ticking block, as in vanilla
  • a069c27 Random ticks: ask for the next ticking section, and read a section's blocks faster

📊 Changes

8 files changed (+580 additions, -82 deletions)

View changed files

➕ crates/horizon-client/examples/random_tick_sections_probe.rs (+132 -0)
📝 crates/horizon-client/src/game.rs (+29 -19)
📝 crates/horizon-client/src/random_tick_sections.rs (+119 -53)
📝 crates/horizon-core/src/paletted.rs (+110 -0)
📝 crates/horizon-world/src/level/mod.rs (+4 -4)
📝 crates/horizon-world/src/level/tick.rs (+11 -6)
➕ mods/horizon/src/test/java/dev/horizonlab/parity/VanillaRandomTickSectionsOracle.java (+102 -0)
➕ tools/test_random_tick_sections_parity.py (+73 -0)

📄 Description

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

Random ticks now pick sections the way vanilla does (perf-server-14, plan E step 1). This is a parity fix.

What was wrong. Vanilla random-ticks a section only while it holds a randomly ticking block or fluid (LevelChunkSection.isRandomlyTicking: tickingBlockCount > 0 || tickingFluidCount > 0). setBlockState keeps those counts current, and recalcBlockCounts sets them on load. Horizon instead ticked every section whose palette listed such a state. A palette keeps a state after its last block is replaced, so a section whose only grass block, lava or crop was removed went on drawing randomTickSpeed positions from randValue every tick. That shifted every position drawn after it in the chunk, and in the chunks ticked after it. A unit test asserted the palette rule.

The change

  • Exact test. PalettedContainer::has(predicate) answers whether any stored value matches, so stale palette entries don't count. When a few palette indices match, it tests whole storage words for them (a field-wise zero test) instead of reading all 4096 entries. Otherwise it scans.
  • Per-section cache. RandomTickSections keeps one bit per section. It reads a section again only when that section's Arc changed (it holds a Weak per section, as it already did per chunk), so an edit re-reads only the edited section.
  • Live check. Behaviours::tick_chunk_random asks next_ticking(level, from) for the lowest ticking section from a height up, as the chunk is now. Vanilla checks isRandomlyTicking as it reaches each section, so a tick that empties or fills a section above counts. The game re-reads the chunk's sections only after World::revision moved.
  • Test. The unit test now asserts vanilla's rule: a stale entry doesn't tick, and only the edited section is re-read.

Parity proof

New harness tools/test_random_tick_sections_parity.py, 3000 cases:

  • Vanilla side. VanillaRandomTickSectionsOracle runs ServerLevel.tickChunk's precipitation draws and section loop on real LevelChunkSections, with the real getBlockRandomPos.
  • Horizon side. random_tick_sections_probe runs tick_chunk_random with the game's RandomTickSections.
  • What each case does.
    • Loads sections filled with every block state in turn (so each state's own answer is compared), plus mixed still/ticking sections.
    • Ticks, sets ticking blocks, ticks, then removes them (leaving them in the palette, including the lone ticking block of mixed sections), and ticks twice more.
    • Uses speeds 0 to 4.
  • What is compared. Every drawn position, its block and fluid random-tick flags, and the final randValue.
  • Result. It passes on this branch. With main's palette rule it fails at line 1.

tools/parity.py --changed origin/main (322 harnesses) at a069c271: 317 run, all passed. 35 of them failed the first time on a broken link of the parity target after a session restart, and passed on --failed once horizon-client was rebuilt. The other 5 already had passes for their inputs.

Numbers

ThinkPad server (nice 19), with 40 mixed bots driven from the desktop through tools/server_bench.py: seed 12345, view distance 10, 120 s warmup, 180 s measured. Spans are averaged over the last 3600 ticks of each run (HORIZON_SPIKES=0, all spans logged). Base is c16b41ab.

Interleaved set (new, base, base, new, new, base):

run MSPT avg tick_blocks ms tick_mobs ms mobs
new 1 30.88 5.05 12.51 459
base 1 31.20 5.16 12.58 454
base 2 30.77 5.05 12.23 453
new 2 30.88 5.31 12.58 443
new 3 32.14 5.41 13.12 452
base 3 30.65 5.00 12.38 460
mean 31.30 vs 30.87 5.26 vs 5.07 12.74 vs 12.40

Across all 6 base runs and 5 clean runs of this branch (the others were taken while another agent's benchmark ran), tick_blocks averages 5.11 for base and 5.28 for this branch.

This is not a speedup. In a fresh world few sections keep a ticking state they no longer hold, so the sections skipped are rare. The exact test and the per-section re-reads cost about what they save: tick_blocks is +0.17 to +0.19 ms on average (about 3 %), close to the run-to-run spread of ±0.2 ms. MSPT moves by less than the spread between runs of the same build. On a 40-bot server, random ticks cost the same block reads vanilla makes, about 3 per ticking section per chunk. Those reads miss the cache (PalettedContainer::get is 12.5 % of the server thread on the desktop profile).

I also tried reading a section's drawn blocks together before ticking them, so the misses would overlap. It was slower: tick_blocks 5.76–6.05 ms against 5.23–5.41, so it is not in this PR. The branch perf/random-tick-reads keeps it.

Self-check

  • cargo fmt --all --check: clean
  • cargo clippy --workspace --all-targets -- -D warnings: clean
  • cargo test -p horizon-core --lib paletted: 8 passed, including has checked against a scan at every width, with stale entries, for blocks and biomes
  • cargo test -p horizon-client --lib random_tick: 4 passed, including the memoized-vs-block-scan test over edits
  • Merges cleanly with main and with the open contributor PRs #1200, #1202, #1203 and #1205 (git merge-tree)

🤖 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/1256 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/22/2026 **Status:** ✅ Merged **Merged:** 10/23/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/random-tick-counts` --- ### 📝 Commits (2) - [`9d58d6f`](https://github.com/horizon-rust/horizon/commit/9d58d6f525babf5a1478d98cab34cb9cc73f268a) Random ticks: a section ticks while it holds a randomly ticking block, as in vanilla - [`a069c27`](https://github.com/horizon-rust/horizon/commit/a069c2716f9a2bcf394859a53e44e0538825f9ad) Random ticks: ask for the next ticking section, and read a section's blocks faster ### 📊 Changes **8 files changed** (+580 additions, -82 deletions) <details> <summary>View changed files</summary> ➕ `crates/horizon-client/examples/random_tick_sections_probe.rs` (+132 -0) 📝 `crates/horizon-client/src/game.rs` (+29 -19) 📝 `crates/horizon-client/src/random_tick_sections.rs` (+119 -53) 📝 `crates/horizon-core/src/paletted.rs` (+110 -0) 📝 `crates/horizon-world/src/level/mod.rs` (+4 -4) 📝 `crates/horizon-world/src/level/tick.rs` (+11 -6) ➕ `mods/horizon/src/test/java/dev/horizonlab/parity/VanillaRandomTickSectionsOracle.java` (+102 -0) ➕ `tools/test_random_tick_sections_parity.py` (+73 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Random ticks now pick sections the way vanilla does (perf-server-14, plan E step 1). This is a parity fix. **What was wrong.** Vanilla random-ticks a section only while it holds a randomly ticking block or fluid (`LevelChunkSection.isRandomlyTicking`: `tickingBlockCount > 0 || tickingFluidCount > 0`). `setBlockState` keeps those counts current, and `recalcBlockCounts` sets them on load. Horizon instead ticked every section whose palette *listed* such a state. A palette keeps a state after its last block is replaced, so a section whose only grass block, lava or crop was removed went on drawing `randomTickSpeed` positions from `randValue` every tick. That shifted every position drawn after it in the chunk, and in the chunks ticked after it. A unit test asserted the palette rule. **The change** - **Exact test.** `PalettedContainer::has(predicate)` answers whether any stored value matches, so stale palette entries don't count. When a few palette indices match, it tests whole storage words for them (a field-wise zero test) instead of reading all 4096 entries. Otherwise it scans. - **Per-section cache.** `RandomTickSections` keeps one bit per section. It reads a section again only when that section's `Arc` changed (it holds a `Weak` per section, as it already did per chunk), so an edit re-reads only the edited section. - **Live check.** `Behaviours::tick_chunk_random` asks `next_ticking(level, from)` for the lowest ticking section from a height up, as the chunk is *now*. Vanilla checks `isRandomlyTicking` as it reaches each section, so a tick that empties or fills a section above counts. The game re-reads the chunk's sections only after `World::revision` moved. - **Test.** The unit test now asserts vanilla's rule: a stale entry doesn't tick, and only the edited section is re-read. ### Parity proof New harness `tools/test_random_tick_sections_parity.py`, 3000 cases: - **Vanilla side.** `VanillaRandomTickSectionsOracle` runs `ServerLevel.tickChunk`'s precipitation draws and section loop on real `LevelChunkSection`s, with the real `getBlockRandomPos`. - **Horizon side.** `random_tick_sections_probe` runs `tick_chunk_random` with the game's `RandomTickSections`. - **What each case does.** - Loads sections filled with every block state in turn (so each state's own answer is compared), plus mixed still/ticking sections. - Ticks, sets ticking blocks, ticks, then removes them (leaving them in the palette, including the lone ticking block of mixed sections), and ticks twice more. - Uses speeds 0 to 4. - **What is compared.** Every drawn position, its block and fluid random-tick flags, and the final `randValue`. - **Result.** It passes on this branch. With main's palette rule it fails at line 1. `tools/parity.py --changed origin/main` (322 harnesses) at a069c271: 317 run, all passed. 35 of them failed the first time on a broken link of the parity target after a session restart, and passed on `--failed` once `horizon-client` was rebuilt. The other 5 already had passes for their inputs. ### Numbers ThinkPad server (nice 19), with 40 mixed bots driven from the desktop through `tools/server_bench.py`: seed 12345, view distance 10, 120 s warmup, 180 s measured. Spans are averaged over the last 3600 ticks of each run (`HORIZON_SPIKES=0`, all spans logged). Base is c16b41ab. Interleaved set (new, base, base, new, new, base): | run | MSPT avg | `tick_blocks` ms | `tick_mobs` ms | mobs | |---|---:|---:|---:|---:| | new 1 | 30.88 | 5.05 | 12.51 | 459 | | base 1 | 31.20 | 5.16 | 12.58 | 454 | | base 2 | 30.77 | 5.05 | 12.23 | 453 | | new 2 | 30.88 | 5.31 | 12.58 | 443 | | new 3 | 32.14 | 5.41 | 13.12 | 452 | | base 3 | 30.65 | 5.00 | 12.38 | 460 | | **mean** | **31.30 vs 30.87** | **5.26 vs 5.07** | 12.74 vs 12.40 | | Across all 6 base runs and 5 clean runs of this branch (the others were taken while another agent's benchmark ran), `tick_blocks` averages 5.11 for base and 5.28 for this branch. This is not a speedup. In a fresh world few sections keep a ticking state they no longer hold, so the sections skipped are rare. The exact test and the per-section re-reads cost about what they save: `tick_blocks` is +0.17 to +0.19 ms on average (about 3 %), close to the run-to-run spread of ±0.2 ms. MSPT moves by less than the spread between runs of the same build. On a 40-bot server, random ticks cost the same block reads vanilla makes, about 3 per ticking section per chunk. Those reads miss the cache (`PalettedContainer::get` is 12.5 % of the server thread on the desktop profile). I also tried reading a section's drawn blocks together before ticking them, so the misses would overlap. It was slower: `tick_blocks` 5.76–6.05 ms against 5.23–5.41, so it is not in this PR. The branch `perf/random-tick-reads` keeps it. ### Self-check - `cargo fmt --all --check`: clean - `cargo clippy --workspace --all-targets -- -D warnings`: clean - `cargo test -p horizon-core --lib paletted`: 8 passed, including `has` checked against a scan at every width, with stale entries, for blocks and biomes - `cargo test -p horizon-client --lib random_tick`: 4 passed, including the memoized-vs-block-scan test over edits - Merges cleanly with main and with the open contributor PRs #1200, #1202, #1203 and #1205 (`git merge-tree`) 🤖 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 04:09:40 +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#3459
No description provided.