Skip to content

Commit 6f9df8d

Browse files
committed
Improve hashing algorithm
1 parent 3756605 commit 6f9df8d

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

manimlib/mobject/svg/svg_mobject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from manimlib.utils.config_ops import digest_config
1818
from manimlib.utils.directories import get_mobject_data_dir
1919
from manimlib.utils.images import get_full_vector_image_path
20+
from manimlib.utils.iterables import hash_obj
2021
from manimlib.logger import log
2122

2223

@@ -63,8 +64,7 @@ def __init__(self, file_name=None, **kwargs):
6364
self.move_into_position()
6465

6566
def init_svg_mobject(self):
66-
hasher = hashlib.sha256(str(self.hash_seed).encode())
67-
hash_val = hasher.hexdigest()
67+
hash_val = hash_obj(self.hash_seed)
6868
if hash_val in SVG_HASH_TO_MOB_MAP:
6969
mob = SVG_HASH_TO_MOB_MAP[hash_val].copy()
7070
self.add(*mob)

manimlib/utils/iterables.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,14 @@ def remove_nones(sequence):
139139

140140
def concatenate_lists(*list_of_lists):
141141
return [item for l in list_of_lists for item in l]
142+
143+
144+
def hash_obj(obj):
145+
if isinstance(obj, dict):
146+
new_obj = {k: hash_obj(v) for k, v in obj.items()}
147+
return hash(tuple(frozenset(sorted(new_obj.items()))))
148+
149+
if isinstance(obj, (set, tuple, list)):
150+
return hash(tuple([hash_obj(e) for e in obj]))
151+
152+
return hash(obj)

0 commit comments

Comments
 (0)