OLD-9481: restore old upstream PR 9481 - world_build: drop a staged chunk PNG once no later chunk uses it #735

Manually merged
github merged 1 commit from gh-pr/735/head into gh-pr/735/base 2026-11-26 09:18:43 +00:00
Owner

@⁠aslan2142 on GitHub · 2026-11-24 13:09 UTC · original
aslan2142:world-build-png-budget → main · +195 −26 · 2 files · 1 commits · merged 2026-11-25 22:28 UTC

Claimed in the coordination issue as: OLD-9481 #14

Restoration of my original pull request, old upstream PR 9481 in ItsNotRexed/Leonida, which was closed unmerged when that repository went private on 2026-11-23 (old numbers link nowhere now). Rebased onto main at 9600cf0c64; the commit applies unchanged, since tools/world_build.py and tests/test_world_build.py have not changed on main since the original base.

What and why

  • The problem: _build_staged_world in tools/world_build.py (the spatial, grid and baked-placement builds) keeps every decoded chunk PNG in its single-flight cache until the build returns. The PNGs held grow with the chunk count.
  • The fix: before any chunk is written, the build counts how many chunks use each texture. A decoded PNG leaves the cache when its last chunk takes it.
  • What stays the same:
    • each texture is still decoded once per build;
    • threads waiting on a decode still share it, through a small _SharedPng (an event plus the bytes);
    • the chunks and world.json are byte-identical.
  • Tests: StagedPngRetentionTest in tests/test_world_build.py, on synthetic chunks with uniform 4x4 DDS textures:
    • the PNGs held stay flat from 8 to 24 chunks (spatial, 1 worker);
    • a 4-worker build stays bounded and matches the serial build;
    • the grid layout releases every PNG that no later chunk uses.

Tested

Linux, Python 3.14.10, CPU only, synthetic inputs (no game files):

  • python -I tests/test_world_build.py:

    • on main with this PR's tests: 68 run, 1 skipped, the 4 new retention tests fail (26 PNGs still held at the last chunk);
    • on this branch: 68 run, 1 skipped, all pass.
  • A synthetic 24-chunk build on main and on this branch:

    • the whole output has the same SHA-256 on both, for 1 and 4 workers;
    • each texture is decoded 26 times on both, and none is left after the build.

    Decoded PNGs held while each chunk is written (main → this branch):

    Layout, workers At the last chunk Peak
    spatial, 1 26 → 3 26 → 3
    spatial, 4 26 → 6 26 → 7
    grid, 1 26 → 3 26 → 11
    grid, 4 26 → 8 26 → 14

    The 4-worker counts depend on thread timing.

  • python -m compileall -q tools tests, git diff --check, and python -I scripts/check_no_game_data.py on both changed files: all clean.

  • On the old repository, the original pull request had five independent checks: Linux, a four-worker concurrency run, and Windows on Python 3.11 and 3.13.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-24 13:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/735) `aslan2142:world-build-png-budget` → `main` · +195 −26 · 2 files · 1 commits · merged 2026-11-25 22:28 UTC Claimed in the coordination issue as: OLD-9481 https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 Restoration of my original pull request, old upstream PR 9481 in `ItsNotRexed/Leonida`, which was closed unmerged when that repository went private on 2026-11-23 (old numbers link nowhere now). Rebased onto main at 9600cf0c648a8b2c4ce18521e416815d3d43eeb2; the commit applies unchanged, since `tools/world_build.py` and `tests/test_world_build.py` have not changed on main since the original base. ### What and why - **The problem:** `_build_staged_world` in `tools/world_build.py` (the spatial, grid and baked-placement builds) keeps every decoded chunk PNG in its single-flight cache until the build returns. The PNGs held grow with the chunk count. - **The fix:** before any chunk is written, the build counts how many chunks use each texture. A decoded PNG leaves the cache when its last chunk takes it. - **What stays the same:** - each texture is still decoded once per build; - threads waiting on a decode still share it, through a small `_SharedPng` (an event plus the bytes); - the chunks and `world.json` are byte-identical. - **Tests:** `StagedPngRetentionTest` in `tests/test_world_build.py`, on synthetic chunks with uniform 4x4 DDS textures: - the PNGs held stay flat from 8 to 24 chunks (spatial, 1 worker); - a 4-worker build stays bounded and matches the serial build; - the grid layout releases every PNG that no later chunk uses. ### Tested Linux, Python 3.14.10, CPU only, synthetic inputs (no game files): - `python -I tests/test_world_build.py`: - on main with this PR's tests: 68 run, 1 skipped, the 4 new retention tests fail (26 PNGs still held at the last chunk); - on this branch: 68 run, 1 skipped, all pass. - A synthetic 24-chunk build on main and on this branch: - the whole output has the same SHA-256 on both, for 1 and 4 workers; - each texture is decoded 26 times on both, and none is left after the build. Decoded PNGs held while each chunk is written (main → this branch): | Layout, workers | At the last chunk | Peak | | --- | --- | --- | | spatial, 1 | 26 → 3 | 26 → 3 | | spatial, 4 | 26 → 6 | 26 → 7 | | grid, 1 | 26 → 3 | 26 → 11 | | grid, 4 | 26 → 8 | 26 → 14 | The 4-worker counts depend on thread timing. - `python -m compileall -q tools tests`, `git diff --check`, and `python -I scripts/check_no_game_data.py` on both changed files: all clean. - On the old repository, the original pull request had five independent checks: Linux, a four-worker concurrency run, and Windows on Python 3.11 and 3.13. GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no translator change) AI-assisted (Claude Code, Claude Opus 5.5).
The staged build (spatial, grid and baked placements) decoded every chunk texture into a per-build
cache that kept each PNG until the build returned, so the decoded PNGs held grew with the chunk
count. Count each chunk's use of a texture before the chunks are written and drop the cache entry
when its last chunk takes it. A texture is still decoded once per build, waiters still share one
decode, and the chunks and world.json are byte-identical.

In a synthetic 24-chunk build, decoded PNGs alive while the last chunk is written fall from 26 to 3
(spatial, 1 worker) and the grid layout's peak from 26 to 11.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:18:42 +00:00
github manually merged commit 198b5c5bcd into gh-pr/735/base 2026-11-26 09:18:43 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 13:14 UTC · original · approved these changes

LGTM. Reference-counted memory bounded PNG staging cache in world_build.py significantly reduces peak RAM footprint during multi-chunk builds. Verified clean-room synthetic unit test coverage in ests/test_world_build.py.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 13:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/735#pullrequestreview-6039826128) · **approved these changes** LGTM. Reference-counted memory bounded PNG staging cache in world_build.py significantly reduces peak RAM footprint during multi-chunk builds. Verified clean-room synthetic unit test coverage in ests/test_world_build.py.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 13:26 UTC · original · approved these changes

Audited and verified. Clean-room compliant.

  • Drops staged chunk PNG once no later chunk requires it in tools/world_build.py, respecting memory/disk budgets.
  • Regression tests pass.
**@⁠abnalstlll-byte** on GitHub · 2026-11-24 13:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/735#pullrequestreview-6039994556) · **approved these changes** Audited and verified. Clean-room compliant. - Drops staged chunk PNG once no later chunk requires it in tools/world_build.py, respecting memory/disk budgets. - Regression tests pass.
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 22:28 UTC · original

Merged: world_build drops a staged chunk PNG once no later chunk uses it.

  • Changed since review: Nothing. It merges cleanly with main, and no review point is open.
  • Tested: tests/test_world_build.py passes on main + this head (Linux, CPython 3.14).
  • Checked on main b73968599 + 198b5c5bc: the semantic suite passes (492 files, 6,315 tests, 0 files failed); test_world_build pass; the 15 docs and writer tests pass; check_no_game_data.py is clean on the 2 changed files.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-25 22:28 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/735#issuecomment-7176400778) Merged: `world_build` drops a staged chunk PNG once no later chunk uses it. - **Changed since review:** Nothing. It merges cleanly with `main`, and no review point is open. - **Tested:** `tests/test_world_build.py` passes on `main` + this head (Linux, CPython 3.14). - **Checked on `main` `b73968599` + `198b5c5bc`:** the semantic suite passes (492 files, 6,315 tests, 0 files failed); `test_world_build` pass; the 15 docs and writer tests pass; `check_no_game_data.py` is clean on the 2 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.