Skip to content

08 Caching and Limits

Tagin T edited this page Dec 3, 2026 · 5 revisions

English · 简体中文 · 繁體中文 · 日本語 · 한국어 · हिन्दी · Español · Português · Français · Deutsch · Русский

08 Caching, Performance & Limits

Cache layers

RTM's performance comes from not redoing the same work every frame. The caches live at several levels:

Cache Location Invalidated by
text/math compilation manim media/Tex, media/texts content change / clear_tex_cache()
ImageMobject textures mob._rtm_tex_cache _rtm_tex_rev / _rtm_alpha_rev
batched emission _rtm_quad_run / _rtm_poly_run geometry, viewport or colour change
mesh projection MeshBatch._projected, _idx_key, _lit_key mark_geometry_dirty() / viewport change
static layer MLWindow._static_layer camera/background/updater change
3D face shading mob._rtm_shade_cache geometry/colour/light change

Text and math

  • with the [typst] extra installed, MathTex uses an in-process Typst backend and needs no LaTeX;
  • compiled output lands in manim's media cache;
  • parallel workers get private media directories so SVGs and temp files cannot collide;
  • restore_tex_cache / save_tex_cache move a compilation cache between machines.

Images

send_image only does real work for textures that actually changed:

  • fully transparent images are skipped before any pixel work based on alpha;
  • static images cache the opaque RGBA buffer, digest and ctypes copy;
  • set_opacity with a uniform alpha only changes quad opacity instead of rewriting pixel alpha;
  • per-pixel masks are still re-uploaded (camera frames rely on revision counters).

Meshes

  • append_indexed projects unique vertices once;
  • while viewport and geometry are unchanged, projection, gather and the index buffer are reused;
  • mark_geometry_dirty() is the manual invalidation hook.

Static layer

Roots without updaters can be baked into one texture, after which each frame draws a single image quad. The price is "frozen pixels", so any updater — including one that writes other objects — disables baking.

Performance model

A frame's cost roughly splits into:

CPU: walk paint units → project → fill native commands/vertices
GPU: upload → mesh → painter → images → post
IO:  readback → ffmpeg (while recording)

Some rules of thumb at 1080p:

  • an empty FrameServer.render_frame is millisecond-scale;
  • text/curve-heavy scenes are dominated by CPU emitters and vertex counts;
  • large meshes cost mostly Python projection + vertex upload, not draw calls;
  • bloom/tonemap is one full-screen copy plus a half-resolution pass, about 0.4 ms of GPU at 1080p;
  • jobs=N is not linear: readback, encoding, memory bandwidth and process startup all eat into the gain.

Diagnostics

RTM_DIAGNOSTICS=1 prints one line per play:

[rtm] play 3: 60f 0.42s | static=on mesh=0v tex_uploads=0 post=off

Meaning:

  • static=on/off: whether the static layer took part;
  • mesh=...v: peak mesh vertex count for this play; watch it near 262k/524k;
  • tex_uploads: real texture uploads;
  • post=on/off: whether post-processing is enabled.

RTM_AUTO_DEPTH_DEBUG=1 prints root and vertex counts for the automatic depth path.

Hard constraints

  • 1920×1080@60: resolution and frame rate are fixed; other values are corrected with a notice.
  • One window per process: native Vulkan state is process-global; parallel recording uses multiple processes.
  • No per-pixel transparency sort for meshes: coplanar translucent geometry blends in submission order.
  • Stateful animations: Rotate, SpeedModifier, animations with updaters, and custom interpolate_mobject that depends on outside state cannot guarantee pure-function semantics for arbitrary seeks.
  • macOS: needs MoltenVK; newer native features (bloom, lit mesh, painter depth) should be validated at runtime on a Mac.
  • ffmpeg: required on PATH for recording.

Common bottlenecks

Symptom Likely cause Fix
slow with lots of text recompiling/re-sending glyphs every frame use caches, avoid rebuilding Text per frame
slow surfaces painter submits faces one by one MeshBatch.from_surface / RTM_AUTO_DEPTH
wrong recorded frame count estimating with duration*fps use Timeline.frame_times
visible seam between parallel chunks H.264 I-frame quantisation lossless intermediate, or compare on the same keyframe
half the picture missing vertex count above MAX_VERTICES split the scene / reduce bezier subdivision
won't start on mac MoltenVK missing brew install molten-vk

Key files

  • real_time_manim/render/image.py: image texture cache
  • real_time_manim/window/core.py: static layer, mesh submission
  • real_time_manim/window/play.py: static-layer preparation, diagnostics line
  • real_time_manim/render/util.py: 3D shading cache
  • real_time_manim/util.py: text cache helpers

Measured speedups per scene type: 09 Performance by Scene Type.

Clone this wiki locally