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

Merged
natterman12 merged 1 commit from upstream-pr/129 into main 2026-11-24 03:10:14 +00:00
natterman12 commented 2026-11-24 02:24:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by natterman12 on 2026-11-24T02:24:13Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5464893272 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/129). Originally posted by [natterman12](https://github.com/natterman12) on 2026-11-24T02:24:13Z. --- 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)
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!129
No description provided.