Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

fix to prevent hash()/id() collisions in the memoizer (#4901)

pjfo6f20425eef98
diff --git a/manim/utils/hashing.py b/manim/utils/hashing.py
index d9824ef83..c5c65cce8 100644
--- a/manim/utils/hashing.py
+++ b/manim/utils/hashing.py
@@ -150,7 +150,13 @@ class _Memoizer:
default_func: Callable[[Any], Any],
memoizing: bool = True,
) -> str | Any:
- obj_membership_sign = obj_to_membership_sign(obj)
+ # hash() values and id() addresses are both raw ints that share no meaning, so entries are tagged with
+ # their signature kind: in an untagged set, a hash() value that numerically equals a recorded id() address
+ # (or vice versa) collapses a never-processed object to the placeholder. Whether such a coincidence occurs
+ # varies from process to process (str hashes are randomized), making play hashes unstable across identical
+ # runs and causing unnecessary re-renders of already cached partial movie files.
+ sign_kind = "h" if obj_to_membership_sign is hash else "i"
+ obj_membership_sign = (sign_kind, obj_to_membership_sign(obj))
if obj_membership_sign in cls._already_processed:
return cls.ALREADY_PROCESSED_PLACEHOLDER
if memoizing:
diff --git a/tests/module/utils/test_hashing.py b/tests/module/utils/test_hashing.py
index 9e56e2d6a..53925782f 100644
--- a/tests/module/utils/test_hashing.py
+++ b/tests/module/utils/test_hashing.py
@@ -159,3 +159,30 @@ def test_hash_consistency():
assert_two_objects_produce_same_hash(Square(), Square())
s = Square()
assert_two_objects_produce_same_hash(s, s.copy())
+
+
+def test_memoizer_does_not_confuse_hash_and_id_signatures():
+ """
+ hash() values and id() addresses are recorded in the same set, so a numeric coincidence between the two kinds
+ must not mark a never-processed object as already processed. Whether such a coincidence occurs varies from
+ process to process (str hashes are randomized), so an untagged set makes play hashes unstable across runs.
+ """
+ id_signed = {"a": 1} # dicts are unhashable, so this is recorded under its id.
+ hashing._Memoizer.check_already_processed(id_signed)
+
+ class HashCollidingWithRecordedId:
+ def __hash__(self):
+ return id(id_signed)
+
+ collider = HashCollidingWithRecordedId()
+ assert hashing._Memoizer.check_already_processed(collider) is collider
+
+ # The other direction: an id() address colliding with a recorded hash() value.
+ unhashable = {"b": 2}
+
+ class HashCollidingWithLiveId:
+ def __hash__(self):
+ return id(unhashable)
+
+ hashing._Memoizer.check_already_processed(HashCollidingWithLiveId())
+ assert hashing._Memoizer.check_already_processed(unhashable) is unhashable