manifest: hash through one reused buffer, halving peak chunk memory per worker (OLD-4922) #129

Manually merged
github merged 1 commit from gh-pr/129/head into gh-pr/129/base 2026-11-26 08:45:34 +00:00
Owner

@⁠natterman12 on GitHub · 2026-11-24 02:24 UTC · original
manifest/reuse-hash-buffer → main · +53 −9 · 2 files · 1 commits · merged 2026-11-24 03:10 UTC

Claimed in the coordination issue as: OLD-4922 (#14; old issue "manifest: reuse the hash input buffer to halve peak chunk memory per worker", indexed in #18)

What and why

tools/manifest.py, _hash_file() read the file with

while remaining and (chunk := f.read(min(CHUNK, remaining))):

f.read() allocates the next 8 MiB bytes object while chunk still refers to the last one, so each hashing worker holds two chunks at its peak. With --jobs N that is 16 MiB times N for the read buffers alone.

It now fills one memoryview-backed bytearray with readinto(), the way manifest_verifier.sha256_file() already does. Sizes, digests, the manifest format and the three change-during-hashing checks are unchanged.

Measured

tracemalloc peak of one _hash_file() call on a 320 MiB temporary file (synthetic bytes, Windows 11, Python 3.15.2, NVMe):

peak throughput (3 runs, warm cache)
before 16.0 MiB 490 MiB/s
after 8.0 MiB 534 MiB/s

The throughput difference is within what I would expect from run-to-run noise on a warm cache; I am only claiming the memory figure.

Tested

No GPU involved. Windows 11, Python 3.15.2:

  • python -I tests/test_manifest.py: OK, 39 tests (1 skipped, as on main)
  • python -I tests/test_manifest_verifier.py, test_text_write_line_endings.py, test_text_encoding.py: OK
  • New test_hashing_reuses_one_chunk_buffer: six 1 MiB chunks plus a partial one must hash correctly with a peak under 1.5 chunks. It fails on main (peak 2,098,046 bytes for a 1,048,576-byte chunk).
  • New test_hashing_follows_short_reads: a readinto() that returns 7 bytes at a time still hashes every byte once.
  • Changed test_same_size_change_during_read_keeps_the_previous_manifest: it hooked stream.read, which the tool no longer calls, so it now hooks stream.readinto. Same edit, same expected ManifestError.
  • Not run on Linux or macOS.

AI-assisted: yes (Claude Code)

Checklist

  • 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. (not applicable: no translator change)

🤖 Generated with Claude Code

**@⁠natterman12** on GitHub · 2026-11-24 02:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/129) `manifest/reuse-hash-buffer` → `main` · +53 −9 · 2 files · 1 commits · merged 2026-11-24 03:10 UTC Claimed in the coordination issue as: OLD-4922 (#14; old issue "manifest: reuse the hash input buffer to halve peak chunk memory per worker", indexed in #18) ### What and why `tools/manifest.py`, `_hash_file()` read the file with ```python while remaining and (chunk := f.read(min(CHUNK, remaining))): ``` `f.read()` allocates the next 8 MiB `bytes` object while `chunk` still refers to the last one, so each hashing worker holds two chunks at its peak. With `--jobs N` that is 16 MiB times N for the read buffers alone. It now fills one `memoryview`-backed `bytearray` with `readinto()`, the way `manifest_verifier.sha256_file()` already does. Sizes, digests, the manifest format and the three change-during-hashing checks are unchanged. ### Measured `tracemalloc` peak of one `_hash_file()` call on a 320 MiB temporary file (synthetic bytes, Windows 11, Python 3.15.2, NVMe): | | peak | throughput (3 runs, warm cache) | |---|---|---| | before | 16.0 MiB | 490 MiB/s | | after | 8.0 MiB | 534 MiB/s | The throughput difference is within what I would expect from run-to-run noise on a warm cache; I am only claiming the memory figure. ### Tested No GPU involved. Windows 11, Python 3.15.2: - `python -I tests/test_manifest.py`: OK, 39 tests (1 skipped, as on `main`) - `python -I tests/test_manifest_verifier.py`, `test_text_write_line_endings.py`, `test_text_encoding.py`: OK - New `test_hashing_reuses_one_chunk_buffer`: six 1 MiB chunks plus a partial one must hash correctly with a peak under 1.5 chunks. It fails on `main` (peak 2,098,046 bytes for a 1,048,576-byte chunk). - New `test_hashing_follows_short_reads`: a `readinto()` that returns 7 bytes at a time still hashes every byte once. - Changed `test_same_size_change_during_read_keeps_the_previous_manifest`: it hooked `stream.read`, which the tool no longer calls, so it now hooks `stream.readinto`. Same edit, same expected `ManifestError`. - Not run on Linux or macOS. AI-assisted: yes (Claude Code) ### Checklist - [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. (not applicable: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
_hash_file() read each 8 MiB chunk with f.read(), which allocates the next
bytes object while the last one is still referenced: two chunks per worker
at the peak. With --jobs N that is 16 MiB times N for hashing alone.

It now fills one memoryview-backed bytearray with readinto(), as
manifest_verifier.sha256_file() already does. Digests, sizes and the
change-during-read checks are unchanged.

Measured on a 320 MiB file: tracemalloc peak 16.0 -> 8.0 MiB.

Tests: a multi-chunk file must hash right with a peak under 1.5 chunks, a
short readinto() must not skip or repeat bytes, and the edit-during-read
case hooks readinto, the call the tool now makes.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:45:31 +00:00
github manually merged commit 9541c00907 into gh-pr/129/base 2026-11-26 08:45:34 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.