perf(cache): hash NumPy arrays without JSON expansion#4883
Open
dexhunter wants to merge 1 commit into
Open
Conversation
Replace lossy array reprs with deterministic logical-byte digests and include ImageMobject pixels in cache invalidation. Refs ManimCommunity#2710 Fixes ManimCommunity#2924 Co-Authored-By: Aiden <aiden@weco.ai>
dexhunter
force-pushed
the
perf/cache-ndarray-hashing
branch
from
July 14, 2026 22:59
8abd050 to
df4ae34
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview: What does this pull request change?
This replaces large plain-
ndarrayJSON representations with deterministic SHA-256 digests over their exact logical bytes. It also includespixel_arraywhen hashing animations and mobjects, while continuing to exclude camera framebuffers.Fixes #2924. Related to #2710.
Motivation and Explanation: Why and how do your changes improve the library?
The previous encoder resized arrays larger than 1,000 elements to
100 x 100and serialized their representation. That had two consequences:ImageMobjectpixel changes;The new array digest includes shape, dtype, field names, and every logical value. It normalizes byte order and memory layout, hashes structured fields separately so record padding is ignored, and preserves the existing fallback for ndarray subclasses and object-dtype arrays.
On the deterministic scene-hash fixture used for autoresearch (120-136 mobjects plus a 1,920 x 1,072 RGBA image), eight paired randomized samples produced these medians:
mainThe paired-ratio metric was
60,543 ppm, a 93.95% reduction in local hashing CPU time. The evaluator rejects candidates unless cache signatures remain stable across fresh processes and equivalent layouts, visible pixel mutations invalidate the hash, camera framebuffer aliases remain excluded, structured dtype names, titles, offsets, item sizes, alignment, overlapping fields, and padding semantics are preserved, and distinct top-level mobjects with colliding Python hashes remain distinct.A separate seven-sample benchmark using real Manim objects makes the workload trade-off explicit:
mainVMobjects with 333 x 3 point arraysImageMobjectsThe image workload is intentionally slower because current
mainomits those visible pixels from the cache key. This change spends about 5 ms across eight 1 MiB images to make their cache invalidation correct; it does not claim that every scene hashes faster.Autoresearch trajectory: https://dashboard.weco.ai/share/Ezfm3RFgvWPOdMcNVY9JBWwBxS-Tu18V
Validation
uv run python -m pytest -q tests/module/utils/test_hashing.py(16 passed)uv run ruff check manim/utils/hashing.py tests/module/utils/test_hashing.pyuv run ruff format --check manim/utils/hashing.py tests/module/utils/test_hashing.pygit diff --checkLinks to added or changed documentation pages
No documentation changes are required; this preserves the public hashing API.
Further Information and Comments
The final scene cache key remains the existing CRC32-based format. SHA-256 is used only to compact each array before it enters that existing serialization and cache-key pipeline.
Reviewer Checklist