Skip to content

manifest: reuse the input buffer while hashing a file - #4941

Open
Brandon-C-Wheeler wants to merge 1 commit into
ItsNotRexed:mainfrom
Brandon-C-Wheeler:fix/manifest-hash-buffer
Open

Brandon-C-Wheeler wants to merge 1 commit into
ItsNotRexed:mainfrom
Brandon-C-Wheeler:fix/manifest-hash-buffer

Conversation

@Brandon-C-Wheeler

Copy link
Copy Markdown

Fixes #4922. Coordination claim.

What and why

When a hash worker asks for the next 8 MiB chunk, its previous chunk is still live. The manifest tool now allocates one buffer per open file, capped at 8 MiB, and fills it with readinto. Each SHA-256 update consumes exactly the bytes read, including after a short read. The regression exercises multiple short reads and a partial final chunk using the same buffer.

Measured on Windows 11 x64

Median peak Python allocations measured with tracemalloc on CPython 3.14.0. Each run hashed four synthetic 32 MiB files. Each worker count and revision ran in three fresh processes, with file creation outside the measurement.

Hash workers Before After
1 16.02 MiB 8.02 MiB
4 64.04 MiB 32.04 MiB

Every run returned the expected sizes and SHA-256 digests. Through the CLI, the patched four-worker verifier accepted the manifest created by the original serial tool, and the original verifier accepted the patched tool’s manifest.

Tests

  • CPython 3.14.0, python -I -m unittest discover -s tests -p "test_manifest*.py" — 75 passed, 3 skipped.
  • CPython 3.12, python -I tests/test_manifest.py — 36 passed, 1 skipped.
  • The new regression failed on the original hash loop. The existing same-size file-change test still modifies the open file during a chunk read.

#4918 adds an EOF check immediately after the same loop. I tested the two changes together at 077bda2b, resolving the adjacent hunk by keeping this loop and #4918’s EOF check. On Windows, 39 manifest tests passed and 1 skipped in that combination.

AI-assisted with Codex. Tests and measurements used synthetic files.

@SphyserX

Copy link
Copy Markdown

Tested 4e45aca on Arch Linux (in a VM), CPython 3.14: pass.

  • python -I -m unittest discover -s tests -p "test_manifest*.py": 78 tests, OK. With tools/manifest.py from main and the PR's tests: FAILED (failures=2), i.e. the new buffer-reuse test and the readinto version of the edit-while-reading test.
  • manifest.sha256() on a random 50 MB file (several full 8 MiB chunks plus a partial one) matches hashlib.sha256 of the whole file.
  • Code read: buffer[:remaining] limits the last read to the expected size, as f.read(min(CHUNK, remaining)) did. h.update(buffer[:count]) only hashes what was read. A zero-size file never enters the loop.

@tupacfan04

Copy link
Copy Markdown
Contributor

PASS. test_manifest*.py discover gives 78 OK / 3 skipped, the verifier 41 OK, awc_manifest 18 OK, shader_stage_manifest 14 OK, compileall clean, and merge is clean against main and #4943.

The claim reproduces: the 32 MiB hash peak drops from 16.78 MB to 8.39 MB under tracemalloc, with the digest unchanged. readinto(buffer[:remaining]) is equivalent to the old read(min(CHUNK, remaining)), and the short-read, empty-file and EOF-before-size cases are all preserved (tools/manifest.py:68-79). The new test fails on main (0 not greater than 1).

Only nit: tests/test_manifest.py:553 replaces the read patch, so the old read-path coverage is gone - correct for the new code, just worth knowing.

Approve. AI-assisted review, no game files, no GPU.

@tupacfan04

Copy link
Copy Markdown
Contributor

Independent Windows check at 4e45aca1.

Environment: Windows 11 x64, CPython 3.13.7, py -I -B.

Tests:

  • tests/test_manifest.py: 37/37 OK (1 skipped, the POSIX backslash case)
  • python -I -m unittest discover -s tests -p "test_manifest*.py": 78/78 OK (3 skipped)

Peak Python allocation, tracemalloc around manifest.create() on four synthetic 32 MiB files, 4 hash workers, fresh process per revision:

  • parent 3faac557: 64.03 MiB
  • head 4e45aca1: 32.04 MiB

Both produced the same four SHA-256 digests and sizes; the manifest bytes differ only in the created timestamp. This matches the reported 64 -> 32 MiB at four workers.

Negative control: with only the updated test file copied onto the parent, test_sha256_reuses_buffer_across_short_reads fails with 0 not greater than 1 (the old loop never calls readinto), and the reworked test_same_size_change_during_read_keeps_the_previous_manifest fails with ManifestError not raised. Both pass at head.

git diff --check clean; merge-tree with current main 3faac557 clean.

@Atomic258

Copy link
Copy Markdown

Independent check of 4e45aca1 — pass (first check on this PR).

Environment: Windows 11, CPython 3.15.2, CPU only. Synthetic manifest trees, no GPU, no game files.

  • python -I tests/test_manifest.py → 37 tests, 36 pass / 1 skip
  • Negative control: PR tests against main's tools/manifest.py → 2 failures (buffer-reuse tracking sees 0 buffers on the old per-read allocation path), so the tests pin the reuse
  • compileall clean, git diff --check main...HEAD clean

Diff reuses one read buffer across a file's hash chunks instead of allocating per chunk — same bytes hashed, less churn. Matches the title.

AI-assisted (Devin).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

manifest: reuse the hash input buffer to halve peak chunk memory per worker

4 participants