[PR #6687] [MERGED] Free emptied terrain buffer pages off the render thread (up to 14 ms hitches while flying) #7031

Closed
opened 2026-11-03 06:32:24 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/6687
Author: @ludovic111
Created: 11/2/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @ludovic111

Base: main ← Head: perf/arena-free-thread


📝 Commits (1)

  • 873ff80 Free emptied terrain buffer pages a few frames later, off the render thread

📊 Changes

2 files changed (+96 additions, -15 deletions)

View changed files

📝 crates/horizon-client-render/src/gfx.rs (+12 -6)
📝 crates/horizon-client-render/src/gfx/arena.rs (+84 -9)

📄 Description

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

What and why

When the terrain arena empties a page, Arena::trim dropped its wgpu::Buffer right there, on the render thread, at the start of draw_frame. That page can be up to 256 MiB, and up to 2 GiB for the translucent arena and its sorted indices. Dropping the last reference to a buffer that size frees it in the driver, and that can take a whole frame: unmapping and returning the memory, or on lavapipe returning host pages. Pages empty all the time while you fly, because sections behind you unload and compact_arena / shrink_translucent move meshes off sparse pages. That gave recurring hitches.

The change:

  • Arena::trim and Arena::clear return the buffers of the pages they emptied instead of dropping them.
  • The renderer keeps them in a Graveyard for GRAVE_FRAMES (4) frames. A bind group or a frame still in flight can hold a buffer for a frame or two, and then its last reference would drop in maintain on the render thread anyway.
  • After that, they go to a short-lived "Buffer freeing" thread, which drops them.

Nothing else changes: the same pages empty at the same time, and the GPU sees the same buffers until they are unused.

Measured

playhorizon (8 vCPU, lavapipe software Vulkan at 320x180, load average 15–52), tools/pacing_bench.py --distances 12 --spike-ms 8, all five scenarios (village flight, 64 b/s flight, teleports, Nether trips, inventory). This is one run each, before and after a first version of this change that freed on a thread at once, without the graveyard. Lavapipe makes wall time GPU noise, so this compares the render thread's own CPU time, which HORIZON_SPIKES logs per span:

render-thread CPU before after
worst Arena::trim of the section arena (rp_arena_trim) 14.4 ms — (not on the render thread)
worst trim of the translucent and index arenas (rp_trims) 5.6 ms —
frames with a trim ≥ 2 ms 5 0
worst renderer span 16.6 ms 8.5 ms
worst frame of the run 20.8 ms 13.9 ms
frames over twice the median and over 8 ms 35 5

The load differed between the two runs (41–52 before, 15–31 after), so the last two rows overstate the change; the trim rows do not depend on it: that work is no longer on the render thread at all. The whole-frame figures (p50, p99, p99.9, lows, frames > 33 ms) on a software renderer are set by lavapipe. An A/B on a real GPU comes with the frame-pacing series (tools/pacing_bench.py, PR to follow).

gfx::arena::tests::graveyard_frees_after_its_frames checks that a released page comes back for freeing exactly GRAVE_FRAMES frames later, in order.

Checks

pr_ready (tools/pr_ready.py --quick on ludovics-mac-mini, against origin/main at 4bc8e359c, 16 min 2 s)

Check Result
Scope ✅ 2 files, +96 −15
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 598 passed (5 min 1 s)
rustfmt ✅ (12 s)
clippy ✅ (7 min 35 s)
tests: horizon-client-render ✅ 989 passed (2 min 47 s)
doc tests ✅ 0 passed (1 s)
content root tests ➖ content root untouched
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ 132 affected, to run in CI (--quick): ambient_sound, armor_stand_frost, arrow, arrow_wind_charge, audio_attributes, auto_jump, block_entity_anim, block_entity_render, block_entity_tickers, block_particles, block_removal, block_tint and 120 more
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

🤖 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/6687 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/arena-free-thread` --- ### 📝 Commits (1) - [`873ff80`](https://github.com/horizon-rust/horizon/commit/873ff80cd9249459c3df4371f020033a337b1293) Free emptied terrain buffer pages a few frames later, off the render thread ### 📊 Changes **2 files changed** (+96 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client-render/src/gfx.rs` (+12 -6) 📝 `crates/horizon-client-render/src/gfx/arena.rs` (+84 -9) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. ## What and why When the terrain arena empties a page, `Arena::trim` dropped its `wgpu::Buffer` right there, on the render thread, at the start of `draw_frame`. That page can be up to 256 MiB, and up to 2 GiB for the translucent arena and its sorted indices. Dropping the last reference to a buffer that size frees it in the driver, and that can take a whole frame: unmapping and returning the memory, or on lavapipe returning host pages. Pages empty all the time while you fly, because sections behind you unload and `compact_arena` / `shrink_translucent` move meshes off sparse pages. That gave recurring hitches. The change: - `Arena::trim` and `Arena::clear` return the buffers of the pages they emptied instead of dropping them. - The renderer keeps them in a `Graveyard` for `GRAVE_FRAMES` (4) frames. A bind group or a frame still in flight can hold a buffer for a frame or two, and then its last reference would drop in `maintain` on the render thread anyway. - After that, they go to a short-lived "Buffer freeing" thread, which drops them. Nothing else changes: the same pages empty at the same time, and the GPU sees the same buffers until they are unused. ## Measured playhorizon (8 vCPU, lavapipe software Vulkan at 320x180, load average 15–52), `tools/pacing_bench.py --distances 12 --spike-ms 8`, all five scenarios (village flight, 64 b/s flight, teleports, Nether trips, inventory). This is one run each, before and after a first version of this change that freed on a thread at once, without the graveyard. Lavapipe makes wall time GPU noise, so this compares the render thread's own CPU time, which `HORIZON_SPIKES` logs per span: | render-thread CPU | before | after | |---|---|---| | worst `Arena::trim` of the section arena (`rp_arena_trim`) | 14.4 ms | — (not on the render thread) | | worst trim of the translucent and index arenas (`rp_trims`) | 5.6 ms | — | | frames with a trim ≥ 2 ms | 5 | 0 | | worst `renderer` span | 16.6 ms | 8.5 ms | | worst frame of the run | 20.8 ms | 13.9 ms | | frames over twice the median and over 8 ms | 35 | 5 | The load differed between the two runs (41–52 before, 15–31 after), so the last two rows overstate the change; the trim rows do not depend on it: that work is no longer on the render thread at all. The whole-frame figures (p50, p99, p99.9, lows, frames > 33 ms) on a software renderer are set by lavapipe. An A/B on a real GPU comes with the frame-pacing series (`tools/pacing_bench.py`, PR to follow). `gfx::arena::tests::graveyard_frees_after_its_frames` checks that a released page comes back for freeing exactly `GRAVE_FRAMES` frames later, in order. ## Checks **pr_ready** (`tools/pr_ready.py --quick` on ludovics-mac-mini, against origin/main at 4bc8e359c, 16 min 2 s) | Check | Result | | --- | --- | | Scope | ✅ 2 files, +96 −15 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 598 passed (5 min 1 s) | | rustfmt | ✅ (12 s) | | clippy | ✅ (7 min 35 s) | | tests: horizon-client-render | ✅ 989 passed (2 min 47 s) | | doc tests | ✅ 0 passed (1 s) | | content root tests | ➖ content root untouched | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ 132 affected, to run in CI (--quick): ambient_sound, armor_stand_frost, arrow, arrow_wind_charge, audio_attributes, auto_jump, block_entity_anim, block_entity_render, block_entity_tickers, block_particles, block_removal, block_tint and 120 more | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | 🤖 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 06:32:24 +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#7031
No description provided.