Skip to content

Track world PNG cache bytes incrementally - #5470

Open
Hiiva wants to merge 1 commit into
ItsNotRexed:mainfrom
Hiiva:fix/world-png-cache-budget
Open

Hiiva wants to merge 1 commit into
ItsNotRexed:mainfrom
Hiiva:fix/world-png-cache-budget

Conversation

@Hiiva

@Hiiva Hiiva commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What and why

BUILD-1739, claimed before implementation.

The instanced world builder sums every retained PNG's length after every new texture. While entries fit within the budget, adding N textures performs quadratic accounting work. Profiling 1,000 valid synthetic DDS files on unchanged main recorded 501,500 visits to that cache-sum generator.

Track resident PNG bytes on insertion and eviction. Cache keys, FIFO order, the strict over-budget comparison, negative caching, process lifetime and the exception that retains one oversized PNG are preserved. An explicitly cleared cache resets the total on its next miss. The separate threaded cache used by baked builds is unchanged.

Validation

Windows x64, Python 3.14.6, CPU only.

  • Five new tests use actual synthetic DDS files and the real PNG decoder. They cover exact-budget retention, non-refreshing cache hits, FIFO eviction, multiple evictions for an oversized PNG, zero budget, cached I/O/format failures, a fresh build key and explicit clearing.
  • A length-counting bytes wrapper around real decoder output gives a deterministic work bound. For 128 textures, unchanged main makes 16,512 length requests and fails; this patch makes 256 and passes. The other four policy tests already pass on main.
  • Existing world instancing tests: 32 pass. Existing world build tests: 59 pass.
  • The same 4,000 synthetic 4x4 DDS paths produce identical PNG bytes before and after. The local run took 1.591 seconds on main and 0.266 seconds with the patch; this is a synthetic cache-cost measurement, with no whole-game timing claim.
  • Compileall, make_game help, 3 line-ending checks, 11 CI-registration checks and git diff --check pass.
  • Independent peer review found no actionable issues and reran all five new methods successfully.

Integration

Based on upstream main 3faac557. Depends on: none.

Fresh exact-head three-way checks of tools/world_build.py with #1981, #3938, #5108, #4261, #4433, #4960 and #5047 have no conflicts. Those PRs do not change this cache function; #4960 uses it from its existing caller. The earlier cache-key fixes #2283 and #2875 are already in main.

All inputs are original synthetic fixtures. No game files or GPU work. AI-assisted: yes.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested f102755 on Windows 11, CPython 3.14.7, CPU only (no GPU, no game files): pass.

For 128 synthetic textures, the cache accounting test recorded 256 length requests on this head and 16,512 on merge base 3faac557. The other four new tests passed on both revisions.

I also compared 1,600 lookups with a fixed random seed (5470). The input contained 18 synthetic DDS files and one missing path. I cleared the cache after each group of 400 lookups and tested budgets of 0, 320, 1,600, and 16,000 bytes. Both revisions returned the same PNG data and retained the same keys in the same order after each lookup. On this head the byte counter matched the sum of the resident PNG lengths after every lookup.

I ran these files with py -3.14 -I: tests/test_world_png_cache.py (5 passed), tests/test_world_build.py (59 passed), and tests/test_world_instancing.py (32 passed). The counts above measure cache accounting work only.

AI-assisted.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted independent review. At f102755, all 44 synthetic operations across four budgets retained identical FIFO entries and return values on parent and head. The incremental count equaled the independently summed bytes after every operation. Two cached textured placements produced byte-identical world and GLB files; both revisions used default OPAQUE for their three textured primitives. This verifies cache parity but does not test #4960's BLEND/MASK behavior in combination.

@kacper-serewis kacper-serewis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI-assisted CPU integration with #4960 at 3fb166c, clean tree eb377335. Against #4960 alone, world.json and all six GLBs were byte-identical at PNG budgets 0, 1 and 256 MiB. All 42 textured primitives resolved to embedded images matching the cached DDS pixels: 24 OPAQUE, 10 MASK (effective cutoff 0.5) and 8 BLEND. The test combined two cached placements and texture manifests, added four translated copies to force reuse, and varied only the budget in temporary source copies. Single-worker CLI.

@Kev-Ball

Copy link
Copy Markdown

At f102755f I compared the actual instanced chunk writer with its parent using 194 DDS files from one complete retail texture pool (Xbox 1.0.2.1) and synthetic triangle geometry. Seventeen successive chunks each requested 24 materials, overlapping the previous chunk by 12 and wrapping around at the end. PNGs were limited to 256 px.

I ran that sequence at the default 256 MiB cache budget and again at 256 KiB to force FIFO eviction. Every GLB matched the parent byte for byte at both budgets (12,569,240 output bytes per sequence, 404 embedded material uses and four skips). After every chunk I also compared the retained keys in order and their PNG contents, and checked the new byte counter against the sum of resident PNG lengths. All matched. The default cache retained every key; at 256 KiB, 133 keys present at one chunk boundary were gone by the next, summed over the sequence.

This checks output and cache behavior through the chunk writer using real decoder inputs. CPU only, CachyOS, AI-assisted (OpenAI Codex).

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Head f102755f0358 · Verdict: LGTM

python -I -m pytest tests/test_world_png_cache.py -q → 5 passed, 2 subtests; existing tests/test_world_build.py → 59 passed, 124 subtests. git diff --check clean.

Accounting is now O(1) per insert/evict and resets from an externally cleared cache; FIFO order, oversized-entry retention, failed decodes and exact-budget behavior are pinned. No blockers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants