[PR #4282] [MERGED] Many mobs no longer grow the renderer's staging memory until it runs out #5539

Closed
opened 2026-11-03 05:13:16 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/4282
Author: @d3grando
Created: 10/30/2026
Status: ✅ Merged
Merged: 10/30/2026
Merged by: @levzzz5154

Base: main ← Head: fix-2826


📝 Commits (1)

  • f7b77fb Many mobs no longer grow the renderer's staging memory until it runs out

📊 Changes

1 file changed (+108 additions, -5 deletions)

View changed files

📝 crates/horizon-client-render/src/gfx/upload.rs (+108 -5)

📄 Description

🤖 Opened by d3grando's AI agent (Claude Code, Claude Opus 5.5), not by d3grando personally.

What and why

Fixes #2826.

With many mobs in view, the game's staging memory grew by about 1 GB per hundred mobs and was never given back, until the game ran out of memory.

Cause. gfx/upload.rs sends every per-frame write through a wgpu::util::StagingBelt with 1 MiB chunks. When a write is larger than the chunk, the belt (wgpu 30 StagingBelt::allocate) creates a buffer of exactly that size. It keeps that buffer forever, and reuses it only for writes that fit. The entity batch outgrows 1 MiB at a few hundred mobs. While more mobs appear it grows a little every tick, so it never fits the buffer from the tick before. Each tick therefore created a new buffer, and the belt kept all of them. The reporter's analysis in the issue had this right.

Fix. Uploads now rounds a write larger than its chunk up to a power of two. The write still copies only its own bytes, and the rest of the target is untouched. The large buffers the belt keeps can then be reused, and a growing upload adds only one buffer each time its size doubles. Texture uploads go through the same helper. Uploads::buffer used StagingBelt::write_buffer, which checked that offset and size were 4-byte aligned. Those checks are kept as an explicit assert, because a rounded-up slice would otherwise skip the size check.

The issue was claimed three days ago, and no pull request has been linked to it since (I searched open and closed PRs before starting).

Kind

  • parity: Horizon now does what vanilla 26.3 does
  • bug: a crash or a breakage
  • perf: faster, or less memory
  • feature: something new for players (listed under New in the release notes)
  • extra: Horizon-only, off by default
  • tooling: tools, CI, tests, harnesses
  • docs

Vanilla reference

This is renderer plumbing with no vanilla behaviour to match. Vanilla stages uploads through com.mojang.blaze3d.vertex.StagingBuffer. Its StagingBuffer$PersistentlyMapped version is backed by a net.minecraft.client.renderer.MappableRingBuffer that has a fixed size and is rotated each frame. StagingBuffer.tryAppend returns null when a write doesn't fit, so vanilla never allocates past that size. CPU-side, ByteBufferBuilder.ensureCapacity grows by min(capacity, 2 MiB) and reallocates in place, so it never keeps old blocks either (checked with javap on the 26.3 client jar). After this change, Horizon's staging memory is bounded as well. The new GPU test gfx::upload::tests::growing_large_writes_reuse_the_belt_buffers checks this. No parity harness covers the renderer's staging.

Self-check

tools/pr_ready.py doesn't run on Windows (it imports tools/parity.py, which needs SIGHUP), so I ran its steps by hand on Windows 11 with an RTX 5080 (Vulkan):

  • cargo fmt --all -- --check: clean
  • cargo clippy -p horizon-client-render --all-targets -- -D warnings: clean
  • cargo test -p horizon-client-render --lib gfx::: 110 passed, 0 failed, 7 ignored
  • The new test, with the upload.rs code change reverted (test kept): fails
    test gfx::upload::tests::growing_large_writes_reuse_the_belt_buffers ... FAILED
    the belt kept 65 buffers over 64 frames
    
    With the fix: passes (3 passed; 0 failed for gfx::upload::tests).
  • cargo build --profile fast -p horizon-client --bins: builds

In game. Single player on the server thread, creative, render distance 8, Vulkan. I summoned NoAI mobs one by one in a 50-wide grid in front of the camera, over 8 species, and read the game's private bytes and the renderer's gpu_allocated_bytes (horizon-ctl status) every 100 mobs:

Mobs main: private / GPU allocated this branch: private / GPU allocated
0 1849 MB / 415 MB 1858 MB / 408 MB
700 1871 MB / 425 MB 1884 MB / 427 MB
800 2390 MB / 865 MB 1884 MB / 427 MB
1000 4058 MB / 2559 MB 1885 MB / 443 MB
1200 6241 MB / 4630 MB 1909 MB / 455 MB
1400 8946 MB / 7223 MB (stopped by my 8 GB guard; still 8946 MB 30 s later) 1910 MB / 455 MB
2000 (not reached) 1911 MB / 487 MB

On this branch, all 2,004 mobs render normally, and the log has no errors.

🤖 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/4282 **Author:** [@d3grando](https://github.com/d3grando) **Created:** 10/30/2026 **Status:** ✅ Merged **Merged:** 10/30/2026 **Merged by:** [@levzzz5154](https://github.com/levzzz5154) **Base:** `main` ← **Head:** `fix-2826` --- ### 📝 Commits (1) - [`f7b77fb`](https://github.com/horizon-rust/horizon/commit/f7b77fbc8844e8dd4d49ea1db29c04cce1c2868c) Many mobs no longer grow the renderer's staging memory until it runs out ### 📊 Changes **1 file changed** (+108 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client-render/src/gfx/upload.rs` (+108 -5) </details> ### 📄 Description > 🤖 Opened by d3grando's AI agent (Claude Code, Claude Opus 5.5), not by d3grando personally. ## What and why Fixes #2826. With many mobs in view, the game's staging memory grew by about 1 GB per hundred mobs and was never given back, until the game ran out of memory. **Cause.** `gfx/upload.rs` sends every per-frame write through a `wgpu::util::StagingBelt` with 1 MiB chunks. When a write is larger than the chunk, the belt (wgpu 30 `StagingBelt::allocate`) creates a buffer of exactly that size. It keeps that buffer forever, and reuses it only for writes that fit. The entity batch outgrows 1 MiB at a few hundred mobs. While more mobs appear it grows a little every tick, so it never fits the buffer from the tick before. Each tick therefore created a new buffer, and the belt kept all of them. The reporter's analysis in the issue had this right. **Fix.** `Uploads` now rounds a write larger than its chunk up to a power of two. The write still copies only its own bytes, and the rest of the target is untouched. The large buffers the belt keeps can then be reused, and a growing upload adds only one buffer each time its size doubles. Texture uploads go through the same helper. `Uploads::buffer` used `StagingBelt::write_buffer`, which checked that offset and size were 4-byte aligned. Those checks are kept as an explicit assert, because a rounded-up slice would otherwise skip the size check. The issue was claimed three days ago, and no pull request has been linked to it since (I searched open and closed PRs before starting). ## Kind - [ ] parity: Horizon now does what vanilla 26.3 does - [x] bug: a crash or a breakage - [ ] perf: faster, or less memory - [ ] feature: something new for players (listed under New in the release notes) - [ ] extra: Horizon-only, off by default - [ ] tooling: tools, CI, tests, harnesses - [ ] docs ## Vanilla reference This is renderer plumbing with no vanilla behaviour to match. Vanilla stages uploads through `com.mojang.blaze3d.vertex.StagingBuffer`. Its `StagingBuffer$PersistentlyMapped` version is backed by a `net.minecraft.client.renderer.MappableRingBuffer` that has a fixed size and is rotated each frame. `StagingBuffer.tryAppend` returns null when a write doesn't fit, so vanilla never allocates past that size. CPU-side, `ByteBufferBuilder.ensureCapacity` grows by `min(capacity, 2 MiB)` and reallocates in place, so it never keeps old blocks either (checked with javap on the 26.3 client jar). After this change, Horizon's staging memory is bounded as well. The new GPU test `gfx::upload::tests::growing_large_writes_reuse_the_belt_buffers` checks this. No parity harness covers the renderer's staging. ## Self-check `tools/pr_ready.py` doesn't run on Windows (it imports `tools/parity.py`, which needs SIGHUP), so I ran its steps by hand on Windows 11 with an RTX 5080 (Vulkan): - `cargo fmt --all -- --check`: clean - `cargo clippy -p horizon-client-render --all-targets -- -D warnings`: clean - `cargo test -p horizon-client-render --lib gfx::`: 110 passed, 0 failed, 7 ignored - The new test, with the `upload.rs` code change reverted (test kept): **fails** ``` test gfx::upload::tests::growing_large_writes_reuse_the_belt_buffers ... FAILED the belt kept 65 buffers over 64 frames ``` With the fix: **passes** (`3 passed; 0 failed` for `gfx::upload::tests`). - `cargo build --profile fast -p horizon-client --bins`: builds **In game.** Single player on the server thread, creative, render distance 8, Vulkan. I summoned NoAI mobs one by one in a 50-wide grid in front of the camera, over 8 species, and read the game's private bytes and the renderer's `gpu_allocated_bytes` (`horizon-ctl status`) every 100 mobs: | Mobs | main: private / GPU allocated | this branch: private / GPU allocated | |---|---|---| | 0 | 1849 MB / 415 MB | 1858 MB / 408 MB | | 700 | 1871 MB / 425 MB | 1884 MB / 427 MB | | 800 | 2390 MB / 865 MB | 1884 MB / 427 MB | | 1000 | 4058 MB / 2559 MB | 1885 MB / 443 MB | | 1200 | 6241 MB / 4630 MB | 1909 MB / 455 MB | | 1400 | 8946 MB / 7223 MB (stopped by my 8 GB guard; still 8946 MB 30 s later) | 1910 MB / 455 MB | | 2000 | (not reached) | 1911 MB / 487 MB | On this branch, all 2,004 mobs render normally, and the log has no errors. 🤖 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 05:13:16 +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#5539
No description provided.