Skip to content

Performance ​

Read this page when a change affects initial-load latency, heap growth, mounted DOM, layout stability, or heavy resources. Measure the owning layer before optimizing: extension/host heap, serialized payload, and browser DOM/resource memory are different costs.

Memory model ​

SnapTeX's largest avoidable costs are full source duplication, serialized block HTML, mounted DOM, PDF canvases, and TikZ runtimes. The current model limits each one:

  • source text is stored once and blocks keep spans;
  • consecutive source mappings are compressed into segments instead of two per-line arrays;
  • renderer snapshots keep hashes and metadata rather than full block strings;
  • empty anchors, dependency lists, and citation metadata are omitted from block snapshots;
  • virtual mode mounts only viewport-near HTML;
  • block HTML is requested on demand and released after a retention window;
  • PDF canvases and TikZ output follow block resource lifetimes.

Diffing ​

Every block receives a stable hash of its source text. Prefix/suffix comparison and block hashes find changed ranges without comparing long outerHTML strings. Dependency fingerprints add non-source invalidation for title metadata and bibliography state.

Scanner summaries ​

The scanner caches block-local tokens by source hash, so unchanged blocks do not repeat their regular-expression scan. It then walks those compact summaries in document order to rebuild counters and labels. User-defined counter redefinitions are intentionally outside the simple numbering model, except for supported explicit tags.

Virtual layout ​

Shells carry estimated heights. Mounted blocks report real heights. For blocks inserted above the viewport, the runtime applies height changes and scroll compensation in the same frame so the visible content does not make a two-step jump.

A valid pinned anchor is reused without enumerating all shells for each measured block. At the document top no anchor lookup is needed. Pagination updates only changed page classes and margins, so refreshing unchanged boundaries does not invalidate their styles again.

Rendered HTML is stored per shell instance rather than by source hash because two identical source blocks still have different indices and runtime state. Offscreen cleanup removes that HTML while retaining the much smaller normalized content height. Page layout keeps its own element height because page spacing is not part of the reusable content height.

Viewport-near work is expressed relative to viewport height rather than fixed screen pixels, making behavior more consistent across displays.

Paged previews refine shell heights and page boundaries in one forward background pass. The webview requests a small bounded batch of block HTML, while the host renders its members serially and the webview measures them one at a time. Completed pages are published immediately; edits cancel the old generation and restart from the affected page, with visible dirty blocks measured from their mounted DOM.

AST cost control ​

The AST backend does not retain a complete document tree indefinitely. It refines coarse blocks and stores compact artifacts. Lazy block rendering fills missing artifacts during the same background pass that measures block heights, avoiding a second full parse while leaving initial visible rendering unblocked. When an artifact already owns label and citation arrays, renderer snapshots reuse those arrays as read-only metadata rather than cloning them.

Incremental AST splitting retains source spans plus document and coarse-block hashes, not a second old-document text snapshot. Browser resources loaded on demand retain only concurrent read promises; ZIP export does not turn every visited binary file into a permanent in-memory Blob.

Heavy resources use the same bounded pass without retaining their hidden output. Images are released after their layout size is known. PDF measurement reads the first-page viewport without painting a bitmap. TikZ must compile to obtain its final cropped SVG bounds; the hidden SVG is discarded after its height is cached.

Measuring ​

Use production builds when comparing memory or startup behavior. Development source maps, browser devtools, logging, and test extension hosts add significant baseline memory.

For VS Code, enable snaptex.debugMemory; for Web, enable Debug memory in Settings. Compare:

  • extension/host heap;
  • total shells and mounted blocks;
  • cached HTML count;
  • PDF canvases;
  • active and cached TikZ output.

Change one lifetime or representation at a time and compare the same document, viewport, backend, and production build. A lower stable heap can still hide a higher opening peak, so record both peak and settled measurements.

A fast structural preview, not a replacement for a full TeX compiler.