# How Manim's Caching System Works for Tex and Text Mobjects: A Deep Dive

> Discover how Manim caches Tex and Text mobjects to speed up rendering. Learn how diskcache avoids costly LaTeX or Pango recompilation for faster animations.

- Repository: [Grant Sanderson/manim](https://github.com/3b1b/manim)
- Tags: deep-dive
- Published: 2026-02-28

---

**Manim's caching system stores generated SVG strings on disk using a `diskcache` wrapper, allowing Tex and Text Mobjects to skip expensive LaTeX compilation or Pango rendering on subsequent identical calls.**

The 3b1b/manim library uses a sophisticated two-tier caching strategy to eliminate redundant processing of mathematical expressions and text. By hashing function arguments and persisting SVG outputs, Manim transforms repeated Tex or Text instantiations from multi-second compilation tasks into millisecond cache retrievals.

## Core Architecture of Manim's Caching System

### The `cache_on_disk` Decorator

The foundation of Manim's persistence layer resides in [`manimlib/utils/cache.py`](https://github.com/3b1b/manim/blob/main/manimlib/utils/cache.py). The `cache_on_disk` decorator (line 21) intercepts function calls and manages the cache lifecycle:

```python
@wraps(func)
def wrapper(*args, **kwargs):
    key = hash_string(f"{func.__name__}{args}{kwargs}")
    value = _cache.get(key)
    if value is None:
        value = func(*args, **kwargs)
        _cache.set(key, value)
    return value

```

This implementation creates a deterministic hash from the function name concatenated with its arguments, then queries a global `_cache` instance. On cache misses, it executes the expensive rendering function and persists the SVG string result.

### Cache Configuration and Limits

The cache uses the `diskcache` library with a strict **1 GiB size limit** (`CACHE_SIZE = 1e9` in [`manimlib/utils/cache.py`](https://github.com/3b1b/manim/blob/main/manimlib/utils/cache.py)). The storage location defaults to `<manim-dir>/cache/tex`, accessible via `get_cache_dir()` in [`manimlib/utils/directories.py`](https://github.com/3b1b/manim/blob/main/manimlib/utils/directories.py). To manually clear all cached entries, Manim provides `clear_cache()` at line 33 of the cache utility file.

## How Tex Mobjects Use Disk Caching

### The LaTeX-to-SVG Pipeline

When you instantiate a `Tex` object (defined in [`manimlib/mobject/svg/tex_mobject.py`](https://github.com/3b1b/manim/blob/main/manimlib/mobject/svg/tex_mobject.py), line 35), the library chains through `latex_to_svg` to `full_tex_to_svg` in [`manimlib/utils/tex_file_writing.py`](https://github.com/3b1b/manim/blob/main/manimlib/utils/tex_file_writing.py). The critical optimization occurs at **line 84**, where `full_tex_to_svg` carries the `@cache_on_disk` decorator:

```python
@cache_on_disk
def full_tex_to_svg(full_tex, compiler, message):
    # Write temporary .tex file

    # Compile with latex/xelatex → dvisvgm

    # Return SVG string

```

### Cache Key Generation

The cache key incorporates the complete LaTeX source (`full_tex`), the compiler selection (`compiler`), and the compilation message (`message`). This ensures that changing any compilation parameter invalidates the cache appropriately while preserving hits for identical mathematical expressions.

## How Text Mobjects Use Dual Caching

### RAM and Disk Caching Strategy

Text rendering in [`manimlib/mobject/svg/text_mobject.py`](https://github.com/3b1b/manim/blob/main/manimlib/mobject/svg/text_mobject.py) implements a more aggressive two-layer cache. The `markup_to_svg` function (line 53) stacks both `@lru_cache` and `@cache_on_disk`:

```python
@lru_cache(maxsize=None)
@cache_on_disk
def markup_to_svg(markup, style_args, ...):
    # Validate markup

    # Call manimpango.MarkupUtils.text2svg

    # Return SVG string

```

The `@lru_cache` layer keeps recent text renders in RAM for instant reuse within the same Python session, while `@cache_on_disk` ensures persistence across separate Manim executions.

### Pango Markup Processing

When `MarkupText` (the base for standard `Text` objects) processes a string, it writes a temporary SVG file via `manimpango.MarkupUtils.text2svg`, immediately reads the file content, deletes the temporary file, and returns the SVG string to the caching layer. This transient file operation makes the disk cache essential for avoiding repeated Pango overhead.

## Cache Directory and Management

Manim stores all cached SVG data in the directory returned by `get_cache_dir()` (located in [`manimlib/utils/directories.py`](https://github.com/3b1b/manim/blob/main/manimlib/utils/directories.py)). By default, this resolves to `<manim_installation>/cache/tex`, configurable via `manim_config.directories.latex_cache`.

To programmatically inspect or clear the cache:

```python
from manimlib.utils.cache import clear_cache
from manimlib.utils.directories import get_cache_dir
import os

# View cache location

print("Cache directory:", get_cache_dir())

# Clear all cached Tex and Text SVGs

clear_cache()

```

## Summary

- **Manim's caching system** uses a `diskcache` wrapper in [`manimlib/utils/cache.py`](https://github.com/3b1b/manim/blob/main/manimlib/utils/cache.py) to persist generated SVG strings, avoiding redundant LaTeX compilation and Pango rendering.
- **Tex Mobjects** rely on `@cache_on_disk` decorating `full_tex_to_svg` in [`manimlib/utils/tex_file_writing.py`](https://github.com/3b1b/manim/blob/main/manimlib/utils/tex_file_writing.py), hashing the complete LaTeX source and compiler arguments.
- **Text Mobjects** implement dual caching via `@lru_cache` and `@cache_on_disk` on `markup_to_svg` in [`manimlib/mobject/svg/text_mobject.py`](https://github.com/3b1b/manim/blob/main/manimlib/mobject/svg/text_mobject.py), providing both session-speed RAM caching and cross-run persistence.
- **Cache management** defaults to a 1 GiB limit in `<manim-dir>/cache/tex`, with `clear_cache()` available for manual invalidation.

## Frequently Asked Questions

### How do I clear Manim's Tex and Text cache manually?

Call `clear_cache()` from [`manimlib/utils/cache.py`](https://github.com/3b1b/manim/blob/main/manimlib/utils/cache.py). This function empties the entire `diskcache.Cache` directory located at `get_cache_dir()`, removing all persisted SVG strings for both LaTeX and Pango text renders.

### Why does the first Tex rendering take longer than subsequent ones?

The initial call to `Tex` triggers `full_tex_to_svg` in [`manimlib/utils/tex_file_writing.py`](https://github.com/3b1b/manim/blob/main/manimlib/utils/tex_file_writing.py), which writes a temporary `.tex` file, compiles it with `latex` or `xelatex`, converts the output via `dvisvgm`, and stores the result. Subsequent identical calls retrieve the SVG string directly from the disk cache keyed by the hash of the LaTeX source and compiler arguments.

### Does Manim cache Text objects differently than Tex objects?

Yes. While both use `@cache_on_disk` for persistence across runs, Text Mobjects (specifically `markup_to_svg` in [`manimlib/mobject/svg/text_mobject.py`](https://github.com/3b1b/manim/blob/main/manimlib/mobject/svg/text_mobject.py)) also employ `@lru_cache` to keep recent renders in RAM. This provides instantaneous reuse within the same Python session, whereas Tex Mobjects rely solely on the disk-based `cache_on_disk` decorator.

### Where are the cached SVG files stored?

Manim stores cache data in the directory returned by `get_cache_dir()` from [`manimlib/utils/directories.py`](https://github.com/3b1b/manim/blob/main/manimlib/utils/directories.py), defaulting to `<manim-installation-directory>/cache/tex`. This location is configurable via `manim_config.directories.latex_cache` and is subject to a 1 GiB size limit enforced by the `diskcache` library.