Serialise only changed pages for patch previews

render_json now accepts an optional page whitelist. The new
project_preview_patch_json_with_images path passes changedPages before building
page SVGs, instead of serialising all pages and filtering afterwards in the
server. Cache hits still use the old filter helper.

Add scripts/bench-preview.py: repeated buffer edits against a running server,
reporting HTTP/server latency, bytes and changedPages. Measure release builds:
article-basic 37.5 ms (one page), arXiv late edit p50 199 ms / p95 222 ms with
one changed page at 175 KB, arXiv heading edit ~480 ms. This is a solid
improvement over the debug numbers previously quoted, but the 10-page <100 ms
target remains unmet.

TEXRRA_PROFILE=1 now gates the geometry/paragraph/before-render timings. Test
count is 1073 (67 server).
This commit is contained in:
gloamxun committed 2026-10-27 21:36:01 +08:00
1 parent 9231c3a7d6
commit d124c646a1
4 files changed
+162 -14

No files matched your search

+8 -7
View File
@@ -1584,15 +1584,16 @@ fn handle_project_request(
};
return respond_json_engine(request, 200, &body, "route-d");
}
match route_d::project_preview_json_with_images(id, &expanded, &images) {
let patch = query_param(url, "patch").is_some();
let result = if patch {
route_d::project_preview_patch_json_with_images(id, &expanded, &images)
} else {
route_d::project_preview_json_with_images(id, &expanded, &images)
};
match result {
Ok(json) => {
store_route_d_preview(key, json.clone());
let body = if query_param(url, "patch").is_some() {
patch_preview_json(&json).unwrap_or(json)
} else {
json
};
return respond_json_engine(request, 200, &body, "route-d");
return respond_json_engine(request, 200, &json, "route-d");
}
Err(_) => {}
}
+54 -2
View File
@@ -60,9 +60,33 @@ pub fn project_preview_json_with_images(
project_id: &str,
source: &str,
images: &HashMap<String, RasterImage>,
) -> Result<String, String> {
project_preview_json_with_images_mode(project_id, source, images, false)
}
/// Project preview that serialises only the pages rebuilt by this revision.
pub fn project_preview_patch_json_with_images(
project_id: &str,
source: &str,
images: &HashMap<String, RasterImage>,
) -> Result<String, String> {
project_preview_json_with_images_mode(project_id, source, images, true)
}
fn project_preview_json_with_images_mode(
project_id: &str,
source: &str,
images: &HashMap<String, RasterImage>,
patch_only: bool,
) -> Result<String, String> {
let started = std::time::Instant::now();
let (body, geometry) = geometry_and_body_with_images(source, images);
if std::env::var_os("TEXRRA_PROFILE").is_some() {
eprintln!(
"PROFILE geometry={:.1}ms",
started.elapsed().as_secs_f64() * 1000.0
);
}
let body = resolve_page_references(body, &geometry);
let paragraphs = paragraphs_with_geometry(
&body,
@@ -70,6 +94,12 @@ pub fn project_preview_json_with_images(
geometry_counter(&geometry, "equation"),
&geometry,
)?;
if std::env::var_os("TEXRRA_PROFILE").is_some() {
eprintln!(
"PROFILE paragraphs={:.1}ms",
started.elapsed().as_secs_f64() * 1000.0
);
}
let mut document = build_document(&paragraphs, &geometry);
apply_geometry(&mut document, &geometry);
let mut sessions = project_sessions()
@@ -120,6 +150,12 @@ pub fn project_preview_json_with_images(
(engine, changed, recomputed)
}
};
if std::env::var_os("TEXRRA_PROFILE").is_some() {
eprintln!(
"PROFILE before_render={:.1}ms",
started.elapsed().as_secs_f64() * 1000.0
);
}
let mut warnings = source_warnings(source, &geometry);
warnings.extend(document_warnings(&document));
let duration_ms = started.elapsed().as_secs_f64() * 1000.0;
@@ -127,6 +163,7 @@ pub fn project_preview_json_with_images(
&document,
&engine,
Some((&changed, recomputed)),
if patch_only { Some(&changed) } else { None },
&geometry,
&warnings,
duration_ms,
@@ -7528,7 +7565,15 @@ pub fn preview_json(source: &str) -> Result<String, String> {
let mut warnings = source_warnings(source, &geometry);
warnings.extend(document_warnings(&document));
let duration_ms = started.elapsed().as_secs_f64() * 1000.0;
let json = render_json(&document, &engine, None, &geometry, &warnings, duration_ms);
let json = render_json(
&document,
&engine,
None,
None,
&geometry,
&warnings,
duration_ms,
);
// Keep the layout so a following `/edit` request can diff against it and
// reuse page checkpoints instead of starting from a no-op document.
ROUTE_D_SESSION.with(|slot| {
@@ -7695,6 +7740,7 @@ pub fn edit_json(source: &str, unit: usize) -> Result<String, String> {
&next,
&engine,
Some((&changed, recomputed)),
None,
&geometry,
&warnings,
duration_ms,
@@ -14390,6 +14436,7 @@ fn render_json(
document: &Document,
engine: &Engine,
edit: Option<(&[usize], usize)>,
only_pages: Option<&[usize]>,
geometry: &PageGeometry,
warnings: &[String],
duration_ms: f64,
@@ -14478,11 +14525,16 @@ fn render_json(
document.paper_height,
&headings,
);
let mut first_page = true;
for (index, (page, mut list)) in engine.pages().iter().zip(lists).enumerate() {
if only_pages.is_some_and(|pages| !pages.contains(&index)) {
continue;
}
list.place(document.h_offset, document.v_offset);
if index > 0 {
if !first_page {
json.push(',');
}
first_page = false;
let _ = write!(
json,
"{{\"index\":{index},\"hash\":\"{:016x}\",\"svg\":\"{}\"}}",