Cache paragraph units across preview revisions

build_document was the real latency bottleneck (~196 ms), not page layout
(~2 ms). Add a bounded thread-local cache of laid-out normal paragraph units,
keyed by text, face, heading level, size, width and alignment, plus a full
paragraph-shaping geometry signature. Geometry changes clear the cache; floats,
tables, math and footnote-bearing paragraphs are not cached.

Release measurements after the cache: article-basic 47 ms, arXiv late edit
p50 57.5 ms / p95 70.3 ms (one page), 10-page simple stress document p50 13 ms
/ p95 14.7 ms. Cold first builds remain 150–370 ms. The 3-page and 10-page
measured cases now meet the <100 ms target; heading/front-matter edits still
reflow multiple pages.

The existing hang-indent server test caught a missing style field in the
signature; hang/looseness/fillskip/tolerance/penalty fields are now included.
Test count stays 1073.
This commit is contained in:
gloamxun committed 2026-10-27 21:57:50 +08:00
1 parent da30705cd0
commit 7528ea26e2
2 files changed
+121 -10

No files matched your search

+15 -10
View File
@@ -59,11 +59,12 @@ execution is not a goal.
- the web client applies sparse page patches by their `index`;
- `changedPages` already identifies the pages that need repainting.
- Incremental bottlenecks:
- release-mode measurements: article-basic warm buffer edit **37.5 ms**
(one page), arXiv late edit **p50 187 ms / p95 232 ms** (one to two of
three pages changed), arXiv early/heading edit about **480 ms**; a
one-page simple document meets the <100 ms target, the 3-page arXiv
template and the 10-page target do not;
- release-mode measurements after the paragraph-unit cache: article-basic
**47 ms**, arXiv late edit **p50 57.5 ms / p95 70.3 ms** (one page changed),
a 10-page simple document **p50 13 ms / p95 14.7 ms**; the measured
3-page and 10-page cases now meet the <100 ms target, while the cold
first build is still 150–370 ms and a heading/front-matter edit still
reflows multiple pages;
- patch responses are serialised only for changed pages, and display lists
are skipped for unchanged pages; the arXiv late edit still carried one page
at 175 KB, and a heading edit carried two at 315 KB of a 396 KB full
@@ -72,10 +73,12 @@ execution is not a goal.
full, then repeated small buffer edits with HTTP/server latency, bytes and
changedPages;
- there is no WebSocket/SSE stream yet, only request/response patches;
- release profiling shows the real bottleneck is **build_document**, not page
layout: for the arXiv late edit geometry ≈5 ms, paragraphs ≈6 ms,
build_document ≈196 ms, layout ≈2 ms. The next optimization must reuse or
cache paragraph units/math layout, not the page breaker.
- release profiling showed the real bottleneck was **build_document**, not
page layout. A bounded paragraph-unit cache keyed by text/font/style (and
cleared when the paragraph-shaping geometry changes) now reuses unchanged
units; build_document drops from ~196 ms to ~30–40 ms for the arXiv late
edit. Float/table/math units are still rebuilt, and the first cold build
does not benefit.
- Developer profiling: `TEXRRA_PROFILE=1` prints geometry/paragraph/before-render
timings from the project preview path. Earlier notes in this document used a
debug binary (430 ms/2.1 s) and overstate the release latency.
@@ -146,7 +149,9 @@ Acceptance:
Acceptance:
- WebSocket/SSE revision stream with page patches;
- warm p95 edit-to-preview <100 ms for a 10-page document;
- warm p95 edit-to-preview <100 ms for a 10-page document (currently met on a
simple 10-page stress document at p95 14.7 ms, and on the 3-page arXiv
sample at p95 70.3 ms);
- typing does not wait for disk save;
- full PDF compilation continues in the background;
- aborted revisions never paint stale pages.