[PR #341] [MERGED] Cache animated sprite mip chains within an 8 MiB budget #2503

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

📋 Pull Request Information

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

Base: main ← Head: perf/20261015-animated-sprite-mips


📝 Commits (1)

  • b1c365f Cache animated sprite mip chains within an 8 MiB budget

📊 Changes

4 files changed (+625 additions, -19 deletions)

View changed files

📝 crates/horizon-client/src/gfx.rs (+161 -19)
➕ docs/animated-sprite-mips-benchmark.md (+148 -0)
➕ tools/bench_sprite_mips.py (+90 -0)
➕ tools/bench_sprite_mips.rs (+226 -0)

📄 Description

What and why

animate_sprites regenerated a full mip chain on every changed frame, including previously displayed frames. Retain chains by animation/source-frame index in a lazy cache owned by the renderer's immutable resource snapshot. Resource reload replaces the snapshot and drops the cache. Cached and uncached frames keep the same selection, skipped-frame behavior, upload order and bytes; mip-level changes remain valid. Existing frame-based interpolation behavior is preserved.

Keep this optimization: CPU animation preparation fell 53.3% in three paired process runs with real prepared 26.3 resources. Pixel-buffer capacities are capped at 8 MiB, with a separate 4,096-entry metadata cap. Frames that cannot fit follow the existing generator; no eviction churn or random-seed consumption. At the measured 64px array size, 256 of 594 source-frame chains fit (map capacity 448); retaining all would require 18.56 MiB.

Measurements

Ryzen 5 5600X, 12 logical CPUs; rustc 1.99.0 (b940084d7); release opt-level 3, LTO off, 16 codegen units. Same dependency resolution, all 2,131 prepared block/item textures, 54 animations, shared layer size 64px. Baseline gfx from 89b58991bbc02eb79b6965fc4ca56357fe2297d9. Each of three paired process runs reverses order in round two, uses a warmup and nine samples per workload (27 samples per variant). Build and paired measurement use separate acquisitions of the global heavy-work lock, with CARGO_BUILD_JOBS=2.

CPU workload, median [min, max] ms Baseline Cache
Setup + first tick + drop 7.532 [7.193, 7.567] 7.545 [7.128, 7.791]
Repeated tick, 1,024-tick sample 3.627 [3.491, 3.720] 1.692 [1.646, 1.739]
Per render call, three renders/tick 1.203 [1.181, 1.244] 0.562 [0.546, 0.585]
Every real source frame once, setup/drop included 98.168 [96.647, 102.813] 97.093 [96.674, 104.441]
8,192 first-seen layers, setup/drop included 1123.475 [1088.996, 1156.957] 1121.300 [1087.745, 1154.184]

Repeated-tick process medians: baseline 3.647 / 3.646 / 3.540 ms, cache 1.702 / 1.674 / 1.692 ms. Setup plus first 4,096 ticks, including cache fill: baseline 14.835 / 15.001 / 14.746 seconds, cache 7.053 / 6.997 / 6.955 seconds. Individual warmed-tick p95: baseline 6.613 / 6.617 / 6.885 ms, cache 4.139 / 3.960 / 3.972 ms. Cold/unique costs remain within observed variation; no cold-start speedup is claimed.

Whole-process peak RSS (KiB): baseline 84412 / 83388 / 85416, cache 84480 / 83820 / 84524. Loading/synthetic-workload peaks and allocator reuse hide the retained cache cost; this does not imply zero added memory. Exact retained chain capacity reaches 8,388,608 bytes plus bounded map/allocator metadata. GPU memory/upload volume is unchanged.

The harness extracts the actual production CPU path and consumes uploads without GPU calls. These are CPU preparation timings, not end-to-end frame time/FPS measurements; unchanged static atlas generation, GPU staging/drawing/presentation are excluded. The unique 8,192-layer case deliberately exceeds practical atlas sizes to exercise admission and fallback. Repeatable method, complete cold/tail/RSS samples and limitations are committed with the harness. Raw data and game resources stay private under .local/.

Vanilla reference

Pinned 26.3 bytecode: SpriteContents.increaseMipLevel, SpriteContents.AnimatedTexture.createAnimationState, SpriteContents.AnimationState.tick/needsToDraw/drawToAtlas, and MipmapGenerator.generateMipLevels (inspected with javap -p -c). This changes memoization only; it does not introduce GPU interpolation into Horizon's existing frame-based path.

  • All six measured processes compared complete upload streams against the original function at 517 tick positions, including duplicate ticks, jumps, rewinds, interpolation metadata and changing mip limits: exact matches.
  • Four focused cache tests cover loops/mip changes, budget exhaustion, tiny-frame entry caps and replacement of resource size/pixels/settings; included in the 635 passing client tests.
  • tools/test_sprite_animation_parity.py: 907 fixtures, 63 resource animations, 258,996 tick states; 203 intentionally rejected fixtures matched.
  • tools/test_mipmap_parity.py: 1,319 cases / 7,914 lines matched exactly.
  • tools/test_breaking_model_parity.py: 2,740 cases / 155,928 vertices matched within harness tolerance; maximum error 1.40395e-07.

Self-check

The block below is the actual completed --quick run. It includes the final production cache implementation; subsequent edits added documentation and the benchmark build-only mode. After those edits, all 196 Python tests, the public-tree check and git diff --check passed again. The full game parity suite was not run locally; it remains pending CI/maintainer selection. There is no recorded dependency index in this checkout, so the three relevant native harnesses were run explicitly. GPU interpolation pixels and end-to-end FPS remain outside this evaluation. An initial RSS wrapper attempt failed before collecting samples because /usr/bin/time was unavailable; the complete paired run above uses Python's Linux resource counters.

pr_ready (tools/pr_ready.py --quick --base 89b58991bbc02eb79b6965fc4ca56357fe2297d9 on archlinux, against 89b58991bb at 89b58991b, 2 min 43 s)

Check Result
Scope ✅ 4 files, +545 −19
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 196 passed (19 s)
rustfmt ✅ (4 s)
clippy ✅ (20 s)
tests: horizon-client ✅ 635 passed (1 min 59 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ not run (--quick): CI's parity job picks the affected harnesses
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

Maintainer edits are enabled.


🔄 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/341 **Author:** [@rdbtCVS](https://github.com/rdbtCVS) **Created:** 10/15/2026 **Status:** ✅ Merged **Merged:** 10/15/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `perf/20261015-animated-sprite-mips` --- ### 📝 Commits (1) - [`b1c365f`](https://github.com/horizon-rust/horizon/commit/b1c365fca135daf0da9b5f70a3a9c7073dde49eb) Cache animated sprite mip chains within an 8 MiB budget ### 📊 Changes **4 files changed** (+625 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/src/gfx.rs` (+161 -19) ➕ `docs/animated-sprite-mips-benchmark.md` (+148 -0) ➕ `tools/bench_sprite_mips.py` (+90 -0) ➕ `tools/bench_sprite_mips.rs` (+226 -0) </details> ### 📄 Description ## What and why `animate_sprites` regenerated a full mip chain on every changed frame, including previously displayed frames. Retain chains by animation/source-frame index in a lazy cache owned by the renderer's immutable resource snapshot. Resource reload replaces the snapshot and drops the cache. Cached and uncached frames keep the same selection, skipped-frame behavior, upload order and bytes; mip-level changes remain valid. Existing frame-based interpolation behavior is preserved. Keep this optimization: CPU animation preparation fell **53.3%** in three paired process runs with real prepared 26.3 resources. Pixel-buffer capacities are capped at **8 MiB**, with a separate 4,096-entry metadata cap. Frames that cannot fit follow the existing generator; no eviction churn or random-seed consumption. At the measured 64px array size, 256 of 594 source-frame chains fit (map capacity 448); retaining all would require 18.56 MiB. ### Measurements Ryzen 5 5600X, 12 logical CPUs; rustc 1.99.0 (`b940084d7`); release opt-level 3, LTO off, 16 codegen units. Same dependency resolution, all 2,131 prepared block/item textures, 54 animations, shared layer size 64px. Baseline gfx from `89b58991bbc02eb79b6965fc4ca56357fe2297d9`. Each of three paired process runs reverses order in round two, uses a warmup and nine samples per workload (27 samples per variant). Build and paired measurement use separate acquisitions of the global heavy-work lock, with `CARGO_BUILD_JOBS=2`. | CPU workload, median [min, max] ms | Baseline | Cache | | --- | --- | --- | | Setup + first tick + drop | 7.532 [7.193, 7.567] | 7.545 [7.128, 7.791] | | Repeated tick, 1,024-tick sample | 3.627 [3.491, 3.720] | 1.692 [1.646, 1.739] | | Per render call, three renders/tick | 1.203 [1.181, 1.244] | 0.562 [0.546, 0.585] | | Every real source frame once, setup/drop included | 98.168 [96.647, 102.813] | 97.093 [96.674, 104.441] | | 8,192 first-seen layers, setup/drop included | 1123.475 [1088.996, 1156.957] | 1121.300 [1087.745, 1154.184] | Repeated-tick process medians: baseline `3.647 / 3.646 / 3.540` ms, cache `1.702 / 1.674 / 1.692` ms. Setup plus first 4,096 ticks, including cache fill: baseline `14.835 / 15.001 / 14.746` seconds, cache `7.053 / 6.997 / 6.955` seconds. Individual warmed-tick p95: baseline `6.613 / 6.617 / 6.885` ms, cache `4.139 / 3.960 / 3.972` ms. Cold/unique costs remain within observed variation; no cold-start speedup is claimed. Whole-process peak RSS (KiB): baseline `84412 / 83388 / 85416`, cache `84480 / 83820 / 84524`. Loading/synthetic-workload peaks and allocator reuse hide the retained cache cost; this does **not** imply zero added memory. Exact retained chain capacity reaches 8,388,608 bytes plus bounded map/allocator metadata. GPU memory/upload volume is unchanged. The harness extracts the actual production CPU path and consumes uploads without GPU calls. These are CPU preparation timings, **not end-to-end frame time/FPS measurements**; unchanged static atlas generation, GPU staging/drawing/presentation are excluded. The unique 8,192-layer case deliberately exceeds practical atlas sizes to exercise admission and fallback. [Repeatable method, complete cold/tail/RSS samples and limitations](docs/animated-sprite-mips-benchmark.md) are committed with the harness. Raw data and game resources stay private under `.local/`. ## Vanilla reference Pinned 26.3 bytecode: `SpriteContents.increaseMipLevel`, `SpriteContents.AnimatedTexture.createAnimationState`, `SpriteContents.AnimationState.tick/needsToDraw/drawToAtlas`, and `MipmapGenerator.generateMipLevels` (inspected with `javap -p -c`). This changes memoization only; it does not introduce GPU interpolation into Horizon's existing frame-based path. - All six measured processes compared complete upload streams against the original function at 517 tick positions, including duplicate ticks, jumps, rewinds, interpolation metadata and changing mip limits: exact matches. - Four focused cache tests cover loops/mip changes, budget exhaustion, tiny-frame entry caps and replacement of resource size/pixels/settings; included in the 635 passing client tests. - `tools/test_sprite_animation_parity.py`: 907 fixtures, 63 resource animations, 258,996 tick states; 203 intentionally rejected fixtures matched. - `tools/test_mipmap_parity.py`: 1,319 cases / 7,914 lines matched exactly. - `tools/test_breaking_model_parity.py`: 2,740 cases / 155,928 vertices matched within harness tolerance; maximum error `1.40395e-07`. ## Self-check The block below is the actual completed `--quick` run. It includes the final production cache implementation; subsequent edits added documentation and the benchmark build-only mode. After those edits, all 196 Python tests, the public-tree check and `git diff --check` passed again. **The full game parity suite was not run locally; it remains pending CI/maintainer selection.** There is no recorded dependency index in this checkout, so the three relevant native harnesses were run explicitly. GPU interpolation pixels and end-to-end FPS remain outside this evaluation. An initial RSS wrapper attempt failed before collecting samples because `/usr/bin/time` was unavailable; the complete paired run above uses Python's Linux resource counters. **pr_ready** (`tools/pr_ready.py --quick --base 89b58991bbc02eb79b6965fc4ca56357fe2297d9` on archlinux, against 89b58991bbc02eb79b6965fc4ca56357fe2297d9 at 89b58991b, 2 min 43 s) | Check | Result | | --- | --- | | Scope | ✅ 4 files, +545 −19 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 196 passed (19 s) | | rustfmt | ✅ (4 s) | | clippy | ✅ (20 s) | | tests: horizon-client | ✅ 635 passed (1 min 59 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | Maintainer edits are enabled. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 15:56:44 +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#2503
No description provided.