A community-maintained Python framework for creating mathematical animations.
Added warning when there is a large number of items to hash. (#1757)
Hugues Devimeux5eb0b1d2429f
diff --git a/manim/_config/default.cfg b/manim/_config/default.cfgindex 04f73d024..dbf46a3ad 100644--- a/manim/_config/default.cfg+++ b/manim/_config/default.cfg@@ -135,6 +135,8 @@ max_files_cached = 100#Flush cache will delete all the cached partial-movie-files.flush_cache = Falsedisable_caching = False+# Disable the warning when there are too much submobjects to hash.+disable_caching_warning = False# Default tex_template# --tex_templatediff --git a/manim/_config/utils.py b/manim/_config/utils.pyindex 4e9465423..cf13e53a7 100644--- a/manim/_config/utils.py+++ b/manim/_config/utils.py@@ -244,6 +244,7 @@ class ManimConfig(MutableMapping):"background_opacity","custom_folders","disable_caching",+ "disable_caching_warning","ffmpeg_loglevel","format","flush_cache",@@ -519,6 +520,7 @@ class ManimConfig(MutableMapping):"show_in_file_browser","log_to_file","disable_caching",+ "disable_caching_warning","flush_cache","custom_folders","use_opengl_renderer",@@ -1022,6 +1024,12 @@ class ManimConfig(MutableMapping):doc="Whether to use scene caching.",)+ disable_caching_warning = property(+ lambda self: self._d["disable_caching_warning"],+ lambda self, val: self._set_boolean("disable_caching_warning", val),+ doc="Whether a warning is raised if there are too much submobjects to hash.",+ )+png_mode = property(lambda self: self._d["png_mode"],lambda self, val: self._set_from_list("png_mode", val, ["RGB", "RGBA"]),diff --git a/manim/utils/hashing.py b/manim/utils/hashing.pyindex 02b808636..11e947dc5 100644--- a/manim/utils/hashing.py+++ b/manim/utils/hashing.py@@ -12,7 +12,7 @@ from typing import Anyimport numpy as np-from .. import logger+from .. import config, logger# Sometimes there are elements that are not suitable for hashing (too long or run-dependent)# This is used to filter them out.@@ -33,7 +33,8 @@ class _Memoizer:_already_processed = set()# Can be changed to whatever string to help debugging the JSon generation.- ALREADY_PROCESSED_PLACEHOLDER = "ALREADY PROCESSED"+ ALREADY_PROCESSED_PLACEHOLDER = "AP"+ THRESHOLD_WARNING = 170_000@classmethoddef reset_already_processed(cls):@@ -125,11 +126,22 @@ class _Memoizer:default_func,memoizing=True,) -> typing.Union[str, Any]:-obj_membership_sign = obj_to_membership_sign(obj)if obj_membership_sign in cls._already_processed:return cls.ALREADY_PROCESSED_PLACEHOLDERif memoizing:+ if (+ not config.disable_caching_warning+ and len(cls._already_processed) == cls.THRESHOLD_WARNING+ ):+ logger.warning(+ "It looks like the scene contains a lot of sub-mobjects. Caching is sometimes not suited to handle such large scenes, you might consider disabling caching with\+ --disable_caching to potentially speed up the rendering process."+ )+ logger.warning(+ "You can disable this warning by setting disable_caching_warning to True in your config file."+ )+cls._already_processed.add(obj_membership_sign)return default_func(obj)@@ -188,7 +200,8 @@ class _CustomEncoder(json.JSONEncoder):return self._cleaned_iterable(temp)elif isinstance(obj, np.uint8):return int(obj)- return f"Unsupported type for serializing -> {str(type(obj))}"+ # Serialize it with only the type of the object. You can change this to whatever string when debugging the serialization process.+ return str(type(obj))def _cleaned_iterable(self, iterable):"""Check for circular reference at each iterable that will go through the JSONEncoder, as well as key of the wrong format.