[PR #659] [MERGED] Collision scans: a counting cursor, and fewer lookups and allocations per block #2708

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

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/659
Author: @ludovic111
Created: 10/17/2026
Status: ✅ Merged
Merged: 10/21/2026
Merged by: @ludovic111

Base: main ← Head: perf/collision-scan


📝 Commits (3)

📊 Changes

5 files changed (+170 additions, -17 deletions)

View changed files

📝 crates/horizon-core/src/geometry/mod.rs (+10 -0)
📝 crates/horizon-core/src/geometry/shape.rs (+53 -4)
📝 crates/horizon-world/src/block_collision.rs (+8 -2)
📝 crates/horizon-world/src/block_collision_scan.rs (+16 -11)
📝 crates/horizon-world/tests/block_collision_scan.rs (+83 -0)

📄 Description

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

What

Block collision scans (vanilla's BlockCollisions, behind every Entity.collide) were the largest single piece of the world tick in the busy world below: SceneCollisions::resolve was 17–22 % of tick_world in sample profiles, and inside it the scan loop itself (self time) was 21–27 %, block reads 13–19 % and moved-shape allocations about a third. Per cell and per hit, the scan did work that vanilla's JIT makes cheap:

  • Cursor: the cell index was divided by the box width and height for every cell. It now counts x, then y, then z, as Cursor3D does: the same cells in the same order, the same candidate budget.
  • Unit cube test: each hit cloned the shared unit cube (four reference counts up and down) to compare identities; VoxelShape::is_block compares with it in place. The box test built a normalised Aabb first; Aabb::intersects_corners compares with the corners directly (vanilla's AABB.intersects(minX, …, maxZ)).
  • Contextual rules: every non-empty block looked its state up in the map of contextual collision rules (scaffolding, liquids, powder snow) under SipHash. A bit in the registry's scan table says which states have one.
  • Moved shapes: VoxelShape::moved decomposed the occupancy into boxes again (two allocations and the merge loops). The moved shape's boxes are its own boxes moved: the same decomposition over coordinates moved by the same additions.

Same results

  • New test cells_follow_the_cursor_order_without_corners: over four boxes (a mob, a box across negative coordinates, a point, a box inside one block), the cells the scan reads are exactly those of the old index formula, in the same order, without the eight corners.
  • New test moved_boxes_are_the_moved_shapes_decomposition: for the unit cube, a stair and a slab moved by ordinary, signed-zero, tiny, huge and i32::MAX/i32::MIN offsets, the boxes and bounds equal those of the moved grid built and decomposed from scratch; is_block follows identity (true for block(), false for a moved or fresh-identity cube).
  • The harnesses covering the scan (block_collision_scan with vanilla's query order, registry_collision_scan, world_movement, world_collision_check) are in the parity run below.

Results

Base/new × 3 interleaved, the same world; tick time and the probe's per-span totals in thread CPU ms per tick (spans on in both builds):

Same world and settings main this PR
Mob movement (travel), mean of 3 runs 1.99 ms (24.5 % of the tick) 1.81 ms (−9 %)
All mob entity ticks, mean of 3 4.64 ms 4.41 ms (−5 %)
Tick CPU time, mean of 3 8.11 ms 8.01 ms
Tick p99, per run 13.0 / 14.7 / 14.9 ms 13.0 / 13.8 / 14.4 ms

Without the span probe (an earlier version of this PR, without the last two items), tick means base/new × 3 were 7.35 / 6.30, 6.22 / 6.03 and 7.03 / 6.23 ms. The whole-tick figure moves with what the 500 mobs happen to do in each run on this loaded machine; the movement span, whose amount of work is stable, is the clearer measure.

How it was measured

  • Machine: Mac mini M4 Pro (14 cores, Metal), shared with other agents' builds and harnesses (load average 60–110 during these runs), so absolute times swing from run to run: compare columns of the same table. Tick times are the integrated server tick in thread CPU time (a local, uncommitted probe times Game::tick with CLOCK_THREAD_CPUTIME_ID, so time spent descheduled doesn't count; the same probe sums the existing spans per tick).
  • World: seed 1234, creative, next to the village of the village benchmark scene (-960, 66, 176), night (time set 18000, advance_time false), a fenced pen with 400 summoned mobs (80 villagers, 80 cows, 80 sheep, 60 zombies, 60 chickens, 40 pigs), about 510 mobs in all. Saved once and restored before every run. Render distance 12, offscreen 1280×720, fast profile (release without LTO).
  • Run: load, wait until no chunk is pending, 300 ticks of warm-up, then bench 40 (about 800 ticks). Builds interleaved base/new.

Self-check

Run on one integration commit holding this PR and the four other server-tick PRs of this round (structure starts, collision scans, entity portal checks, sculk scan, pathfinder caches), which touch separate code:

  • cargo fmt --all --check: clean.
  • cargo clippy --workspace --all-targets -- -D warnings: clean.
  • cargo test -p horizon-client --lib: 787 passed, 0 failed (10 ignored). cargo test -p horizon-world -p horizon-core: all passed.
  • Local parity, tools/parity.py --changed origin/main on the integration commit (all five PRs together, 309 harnesses depend on the changed files): 194 passed, 0 failed; the other 115 did not run because the run hit the 2 h background limit. The passes are in the shared pass cache, so a rerun of the same tree only runs the remaining 115.

Nothing needs Ludovic.

🤖 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/659 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/17/2026 **Status:** ✅ Merged **Merged:** 10/21/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/collision-scan` --- ### 📝 Commits (3) - [`a3bf174`](https://github.com/horizon-rust/horizon/commit/a3bf174ddbadf6b68c3e732908bd149d60d9689b) Collision scans: a counting cursor, and fewer lookups and allocations per block - [`2bfe339`](https://github.com/horizon-rust/horizon/commit/2bfe3391a80a0fe7d2444522dbc383a788595b6a) Merge branch 'perf/collision-scan' of https://github.com/ludovic111/horizon into integ-659 - [`c719d21`](https://github.com/horizon-rust/horizon/commit/c719d215efbf91385100382058763af438824cb2) Merge remote-tracking branch 'origin/main' into integ-659 ### 📊 Changes **5 files changed** (+170 additions, -17 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-core/src/geometry/mod.rs` (+10 -0) 📝 `crates/horizon-core/src/geometry/shape.rs` (+53 -4) 📝 `crates/horizon-world/src/block_collision.rs` (+8 -2) 📝 `crates/horizon-world/src/block_collision_scan.rs` (+16 -11) 📝 `crates/horizon-world/tests/block_collision_scan.rs` (+83 -0) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What Block collision scans (vanilla's `BlockCollisions`, behind every `Entity.collide`) were the largest single piece of the world tick in the busy world below: `SceneCollisions::resolve` was 17–22 % of `tick_world` in `sample` profiles, and inside it the scan loop itself (self time) was 21–27 %, block reads 13–19 % and moved-shape allocations about a third. Per cell and per hit, the scan did work that vanilla's JIT makes cheap: - **Cursor:** the cell index was divided by the box width and height for every cell. It now counts x, then y, then z, as `Cursor3D` does: the same cells in the same order, the same candidate budget. - **Unit cube test:** each hit cloned the shared unit cube (four reference counts up and down) to compare identities; `VoxelShape::is_block` compares with it in place. The box test built a normalised `Aabb` first; `Aabb::intersects_corners` compares with the corners directly (vanilla's `AABB.intersects(minX, …, maxZ)`). - **Contextual rules:** every non-empty block looked its state up in the map of contextual collision rules (scaffolding, liquids, powder snow) under SipHash. A bit in the registry's scan table says which states have one. - **Moved shapes:** `VoxelShape::moved` decomposed the occupancy into boxes again (two allocations and the merge loops). The moved shape's boxes are its own boxes moved: the same decomposition over coordinates moved by the same additions. ## Same results - New test `cells_follow_the_cursor_order_without_corners`: over four boxes (a mob, a box across negative coordinates, a point, a box inside one block), the cells the scan reads are exactly those of the old index formula, in the same order, without the eight corners. - New test `moved_boxes_are_the_moved_shapes_decomposition`: for the unit cube, a stair and a slab moved by ordinary, signed-zero, tiny, huge and `i32::MAX`/`i32::MIN` offsets, the boxes and bounds equal those of the moved grid built and decomposed from scratch; `is_block` follows identity (true for `block()`, false for a moved or fresh-identity cube). - The harnesses covering the scan (`block_collision_scan` with vanilla's query order, `registry_collision_scan`, `world_movement`, `world_collision_check`) are in the parity run below. ## Results Base/new × 3 interleaved, the same world; tick time and the probe's per-span totals in thread CPU ms per tick (spans on in both builds): | Same world and settings | `main` | this PR | | --- | ---: | ---: | | Mob movement (`travel`), mean of 3 runs | 1.99 ms (24.5 % of the tick) | **1.81 ms** (−9 %) | | All mob entity ticks, mean of 3 | 4.64 ms | **4.41 ms** (−5 %) | | Tick CPU time, mean of 3 | 8.11 ms | 8.01 ms | | Tick p99, per run | 13.0 / 14.7 / 14.9 ms | 13.0 / 13.8 / 14.4 ms | Without the span probe (an earlier version of this PR, without the last two items), tick means base/new × 3 were 7.35 / 6.30, 6.22 / 6.03 and 7.03 / 6.23 ms. The whole-tick figure moves with what the 500 mobs happen to do in each run on this loaded machine; the movement span, whose amount of work is stable, is the clearer measure. ### How it was measured - **Machine:** Mac mini M4 Pro (14 cores, Metal), shared with other agents' builds and harnesses (load average 60–110 during these runs), so absolute times swing from run to run: compare columns of the same table. Tick times are the integrated server tick in **thread CPU time** (a local, uncommitted probe times `Game::tick` with `CLOCK_THREAD_CPUTIME_ID`, so time spent descheduled doesn't count; the same probe sums the existing `spans` per tick). - **World:** seed 1234, creative, next to the village of the `village` benchmark scene (-960, 66, 176), night (`time set 18000`, `advance_time false`), a fenced pen with 400 summoned mobs (80 villagers, 80 cows, 80 sheep, 60 zombies, 60 chickens, 40 pigs), about 510 mobs in all. Saved once and restored before every run. Render distance 12, offscreen 1280×720, `fast` profile (release without LTO). - **Run:** load, wait until no chunk is pending, 300 ticks of warm-up, then `bench 40` (about 800 ticks). Builds interleaved base/new. ### Self-check Run on one integration commit holding this PR and the four other server-tick PRs of this round (structure starts, collision scans, entity portal checks, sculk scan, pathfinder caches), which touch separate code: - `cargo fmt --all --check`: clean. - `cargo clippy --workspace --all-targets -- -D warnings`: clean. - `cargo test -p horizon-client --lib`: 787 passed, 0 failed (10 ignored). `cargo test -p horizon-world -p horizon-core`: all passed. - Local parity, `tools/parity.py --changed origin/main` on the integration commit (all five PRs together, 309 harnesses depend on the changed files): **194 passed, 0 failed**; the other 115 did not run because the run hit the 2 h background limit. The passes are in the shared pass cache, so a rerun of the same tree only runs the remaining 115. Nothing needs Ludovic. 🤖 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-02 15:58:26 +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#2708
No description provided.