manim_svg_cache¶
Persistent (cross-process) cache for Manim SVG/Tex geometry.
Manim renders Text/Tex/MathTex
by compiling LaTeX to an SVG and then parsing that SVG into a tree of
VMobject bezier glyphs. The LaTeX -> SVG step is already
cached to disk by Manim, but the SVG -> VMobject parse is not: Manim only
memoises it in a process-local dict (SVG_HASH_TO_MOB_MAP), and it stores /
retrieves entries by deep-copying the whole glyph tree.
For a large label (e.g. a 2500-glyph block of text) that parse plus the two deep copies costs ~60s, and because the memo is in-memory only, every fresh render process pays it again. This module makes the parse result persist to disk so the control points are generated exactly once and cheaply reloaded on every subsequent run.
The design mirrors the on-disk geometry cache used by
algan.mobs.triangulated_bezier_circuit: a stable sha256 content key
selects a file under SETTINGS.paths.cache_directory; the value is the
per-glyph point arrays + style, saved with torch.save() and rebuilt into
lightweight VMobjects (no LaTeX, no svgelements parse, no deep copy).
Manim 0.21 added SVGMobject.id_to_vgroup_dict, which maps each SVG element
id to a VGroup of the glyphs beneath it. MathTex._break_up_by_substrings
indexes it to split a formula into its tex_strings, so a cache hit that
rebuilt only the glyph tree left the dict empty and every Tex after the very
first (i.e. every run once this cache is warm) died on KeyError: 'root'.
The recipe therefore also stores, per group id, the indices of its members in
the top-level glyph list, and _rebuild() reconstructs the dict from them.
A Manim without that attribute simply has no group map, so either round-trips
through the same cache. Recipes are tagged with _RECIPE_TAG so entries
written by an older Algan (or under a manim that did not need the map) are
detected and transparently re-parsed rather than replayed into a broken
mobject – the cache key is content-addressed, not version-keyed, so a
re-vendoring would otherwise read a recipe written by the previous version.
The directory is bounded: each save evicts least-recently-used entries once the
total exceeds a cap (default 512 MB, override via ALGAN_MANIM_SVG_CACHE_MB).
It is wired in by install(), which monkeypatches
SVGMobject.init_svg_mobject on manim – Algan’s vendored Manim subset,
and the only one in the process. Importing this module installs the patch once.
Functions