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

Closed
opened 2026-11-26 20:17:03 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/129
Author: @natterman12
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/24/2026
Merged by: @ubiquitarianism

Base: main ← Head: manifest/reuse-hash-buffer


📝 Commits (1)

  • 9541c00 manifest: hash through one reused buffer, halving peak chunk memory per worker (OLD-4922)

📊 Changes

2 files changed (+53 additions, -9 deletions)

View changed files

📝 tests/test_manifest.py (+43 -6)
📝 tools/manifest.py (+10 -3)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/129 **Author:** [@natterman12](https://github.com/natterman12) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/24/2026 **Merged by:** [@ubiquitarianism](https://github.com/ubiquitarianism) **Base:** `main` ← **Head:** `manifest/reuse-hash-buffer` --- ### 📝 Commits (1) - [`9541c00`](https://github.com/ubiquitarianism/leonida/commit/9541c00907bdcf6cfadc16a0b6a2e0da2bf51383) manifest: hash through one reused buffer, halving peak chunk memory per worker (OLD-4922) ### 📊 Changes **2 files changed** (+53 additions, -9 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_manifest.py` (+43 -6) 📝 `tools/manifest.py` (+10 -3) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:17:03 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#181
No description provided.