[PR #561] [MERGED] Renderer: cull sections by chunk column from a dense list (section walk 5x faster, exact) #2644

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

📋 Pull Request Information

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

Base: main ← Head: perf/section-culling-columns


📝 Commits (1)

  • 167339c Renderer: cull sections by chunk column, from a dense list

📊 Changes

3 files changed (+417 additions, -59 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+22 -41)
➕ crates/horizon-client/src/gfx/resident.rs (+381 -0)
📝 crates/horizon-client/src/mesher/facing.rs (+14 -18)

📄 Description

What

Renderer::render's per-frame section walk (which sections to draw, nearest first) no longer iterates a HashMap of every resident section:

  • Dense storage, grouped by chunk column (new gfx/resident.rs). Sections live in a dense list, with an FxHash map from key to slot used only by upload/remove, and a list of chunk columns holding their sections' slots and lowest/highest section y.
  • Column first. A column is skipped with one test when it is past the render distance (the test only depends on x and z, so it is the same as each section's) or when its box misses the frustum.
  • Exact result. Each section's box lies inside its column's box. The frustum test picks the box corner furthest along each plane normal, and float rounding is monotonic, so a rejected column contains no section the per-section test would accept. The per-section test is the same function as before. The new test column_walk_finds_exactly_the_sections_in_view checks the visible list (same sections, distances and origins, same order) against the old per-section walk for 4 cameras × 3 render distances, with uneven columns and occlusion holes.
  • Integer sort, deterministic ties. Visible entries carry the squared distance as bits (non-negative floats order like their bits) and the key, so the sort compares integers. Equal distances, which are common when the camera sits on a block centre, now order by key. Before, they followed HashMap's random per-run order.
  • No key lookups in the draw-list loops. The two loops that build the solid and translucent draws reach sections by slot instead of SipHashing their key.
  • FaceGroups looks up a group's axis in a const table instead of searching NEGATIVE for it on every call.

Why

perf-gpu-1 measured section visibility at about 14% of the render thread at RD 32. The walk touched every resident section through hash-map iteration, then SipHashed every visible section twice.

Measurements

In-process timing (the new ignored test column_walk_speed, min of 200 runs; cargo test --profile fast -p horizon-client --lib -- --ignored column_walk_speed --nocapture). The same RD 32 world (80,802 resident sections, 8,893 in view), old walk (map iteration, sort, two lookups per visible section) against the new one:

old walk column walk
run 1 1.84 ms 0.36 ms
run 2 1.81 ms 0.36 ms
run 3 1.76 ms 0.35 ms

In game: Mac mini M4 Pro (Metal), 1280x720 offscreen, seed 1234 creative world, at -960 125 272, yaw 0, pitch 35, noon, render distance 32 (2,681 sections drawn in every run), VSync off, unlimited FPS. Each run: horizon-ctl bench 15 once the world is ready, then an 8 s Time Profiler capture. "Profiled ms/frame" is main-thread samples (1 ms each, running only) in that code divided by the frames rendered during the capture.

The machine was running other agents' CI (load average about 70 on 14 threads). The main thread spent most of each frame preempted or on efficiency cores, so wall-clock FPS swung 2–7x between identical binaries, and even profiled times swing about 2x with core speed. The last row divides the walk by the Metal draw time of the same frames. Every run issues the same draws, so the ratio cancels core speed.

main this PR main this PR main this PR
Average FPS (contended, see above) 19 6 44 28 24 44
1% low FPS 3 2 4 3 3 5
CPU ms/frame, median 19.97 136.80 2.02 5.55 13.07 2.12
GPU ms/frame n/a ¹ n/a n/a n/a n/a n/a
Profiled: section walk + sort 0.097 0.062 0.119 0.080 0.146 0.071
Profiled: Metal draws (same work in all runs) 0.120 0.138 0.185 0.184 0.205 0.181
Profiled: Renderer::render total 0.593 0.662 0.680 0.738 0.869 0.736
Walk ÷ Metal draw time 0.81 0.45 0.64 0.43 0.71 0.39

¹ Metal on Apple Silicon has no timestamp queries inside encoders, so the game can't time the GPU. This change doesn't touch GPU work: same draws, same order apart from ties.

In this scene cave culling leaves far fewer resident sections than the timing test's open world, so the in-game walk was already only about 0.1 ms; it drops by about 40%. The gain grows with resident sections: render distance, open terrain, high altitude.

Correctness

  • Screenshots against origin/main at two views (village street from above, pitch 35, and a hillside at eye level, pitch 3), RD 32, ticks frozen, mobs removed, clouds and animated textures off so a run can repeat. The hillside is pixel-identical. The village view differs in 22 pixels, a ±1 band at the top edge and a few isolated pixels; two runs of main itself differ in 97 pixels at the same places.
  • New tests: column_walk_finds_exactly_the_sections_in_view (above) and slots_and_columns_follow_inserts_and_removals (4,000 random inserts, replacements and removals checked against a HashMap model after every step). The existing facing test checks the axis table.
cargo fmt --all --check                                   ok
cargo clippy --workspace --all-targets -- -D warnings      ok
cargo test -p horizon-client --lib                        760 passed, 6 ignored
tools/parity.py --changed origin/main (84 harnesses)       84 passed, 0 failed (one run on this + #560 merged together)

Needs from Ludovic: nothing.

🤖 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/561 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/16/2026 **Status:** ✅ Merged **Merged:** 10/16/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/section-culling-columns` --- ### 📝 Commits (1) - [`167339c`](https://github.com/horizon-rust/horizon/commit/167339c03c16670dacaabfdaeade842fbb686d45) Renderer: cull sections by chunk column, from a dense list ### 📊 Changes **3 files changed** (+417 additions, -59 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+22 -41) ➕ `crates/horizon-client/src/gfx/resident.rs` (+381 -0) 📝 `crates/horizon-client/src/mesher/facing.rs` (+14 -18) </details> ### 📄 Description ## What `Renderer::render`'s per-frame section walk (which sections to draw, nearest first) no longer iterates a `HashMap` of every resident section: - **Dense storage, grouped by chunk column** (new `gfx/resident.rs`). Sections live in a dense list, with an FxHash map from key to slot used only by `upload`/`remove`, and a list of chunk columns holding their sections' slots and lowest/highest section y. - **Column first.** A column is skipped with one test when it is past the render distance (the test only depends on x and z, so it is the same as each section's) or when its box misses the frustum. - **Exact result.** Each section's box lies inside its column's box. The frustum test picks the box corner furthest along each plane normal, and float rounding is monotonic, so a rejected column contains no section the per-section test would accept. The per-section test is the same function as before. The new test `column_walk_finds_exactly_the_sections_in_view` checks the visible list (same sections, distances and origins, same order) against the old per-section walk for 4 cameras × 3 render distances, with uneven columns and occlusion holes. - **Integer sort, deterministic ties.** Visible entries carry the squared distance as bits (non-negative floats order like their bits) and the key, so the sort compares integers. Equal distances, which are common when the camera sits on a block centre, now order by key. Before, they followed `HashMap`'s random per-run order. - **No key lookups in the draw-list loops.** The two loops that build the solid and translucent draws reach sections by slot instead of SipHashing their key. - `FaceGroups` looks up a group's axis in a const table instead of searching `NEGATIVE` for it on every call. ## Why perf-gpu-1 measured section visibility at about 14% of the render thread at RD 32. The walk touched every resident section through hash-map iteration, then SipHashed every visible section twice. ## Measurements **In-process timing** (the new ignored test `column_walk_speed`, min of 200 runs; `cargo test --profile fast -p horizon-client --lib -- --ignored column_walk_speed --nocapture`). The same RD 32 world (80,802 resident sections, 8,893 in view), old walk (map iteration, sort, two lookups per visible section) against the new one: | | old walk | column walk | |---|---|---| | run 1 | 1.84 ms | 0.36 ms | | run 2 | 1.81 ms | 0.36 ms | | run 3 | 1.76 ms | 0.35 ms | **In game:** Mac mini M4 Pro (Metal), 1280x720 offscreen, seed 1234 creative world, at -960 125 272, yaw 0, pitch 35, noon, render distance 32 (2,681 sections drawn in every run), VSync off, unlimited FPS. Each run: `horizon-ctl bench 15` once the world is ready, then an 8 s Time Profiler capture. "Profiled ms/frame" is main-thread samples (1 ms each, running only) in that code divided by the frames rendered during the capture. The machine was running other agents' CI (load average about 70 on 14 threads). The main thread spent most of each frame preempted or on efficiency cores, so wall-clock FPS swung 2–7x between identical binaries, and even profiled times swing about 2x with core speed. The last row divides the walk by the Metal draw time of the same frames. Every run issues the same draws, so the ratio cancels core speed. | | main | this PR | main | this PR | main | this PR | |---|---|---|---|---|---|---| | Average FPS (contended, see above) | 19 | 6 | 44 | 28 | 24 | 44 | | 1% low FPS | 3 | 2 | 4 | 3 | 3 | 5 | | CPU ms/frame, median | 19.97 | 136.80 | 2.02 | 5.55 | 13.07 | 2.12 | | GPU ms/frame | n/a ¹ | n/a | n/a | n/a | n/a | n/a | | Profiled: section walk + sort | 0.097 | 0.062 | 0.119 | 0.080 | 0.146 | 0.071 | | Profiled: Metal draws (same work in all runs) | 0.120 | 0.138 | 0.185 | 0.184 | 0.205 | 0.181 | | Profiled: `Renderer::render` total | 0.593 | 0.662 | 0.680 | 0.738 | 0.869 | 0.736 | | **Walk ÷ Metal draw time** | **0.81** | **0.45** | **0.64** | **0.43** | **0.71** | **0.39** | ¹ Metal on Apple Silicon has no timestamp queries inside encoders, so the game can't time the GPU. This change doesn't touch GPU work: same draws, same order apart from ties. In this scene cave culling leaves far fewer resident sections than the timing test's open world, so the in-game walk was already only about 0.1 ms; it drops by about 40%. The gain grows with resident sections: render distance, open terrain, high altitude. ## Correctness - Screenshots against origin/main at two views (village street from above, pitch 35, and a hillside at eye level, pitch 3), RD 32, ticks frozen, mobs removed, clouds and animated textures off so a run can repeat. The hillside is pixel-identical. The village view differs in 22 pixels, a ±1 band at the top edge and a few isolated pixels; two runs of main itself differ in 97 pixels at the same places. - New tests: `column_walk_finds_exactly_the_sections_in_view` (above) and `slots_and_columns_follow_inserts_and_removals` (4,000 random inserts, replacements and removals checked against a `HashMap` model after every step). The existing facing test checks the axis table. ``` cargo fmt --all --check ok cargo clippy --workspace --all-targets -- -D warnings ok cargo test -p horizon-client --lib 760 passed, 6 ignored tools/parity.py --changed origin/main (84 harnesses) 84 passed, 0 failed (one run on this + #560 merged together) ``` Needs from Ludovic: nothing. 🤖 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:57:53 +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#2644
No description provided.