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

Merged
aslan2142 merged 1 commit from upstream-pr/735 into main 2026-11-25 22:28:09 +00:00
aslan2142 commented 2026-11-24 13:09:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-24T13:09:34Z.


Claimed in the coordination issue as: OLD-9481 https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7132371964

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).

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5472767012 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/735). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-24T13:09:34Z. --- Claimed in the coordination issue as: OLD-9481 https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7132371964 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).
abnalstlll-byte commented 2026-11-24 13:14:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-24T13:14:31Z.

Review: APPROVED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6039826128 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/735#pullrequestreview-6039826128). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T13:14:31Z. **Review:** APPROVED --- 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 commented 2026-11-24 13:26:17 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-24T13:26:17Z.

Review: APPROVED


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.
<!-- greyfinch-sync:ubiquitarianism/leonida:review:6039994556 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/735#pullrequestreview-6039994556). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T13:26:17Z. **Review:** APPROVED --- 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.
aslan2142 commented 2026-11-25 22:28:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-25T22:28:12Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7176400778 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/735#issuecomment-7176400778). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-25T22:28:12Z. --- 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).
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!735
No description provided.