Return dirty-page preview patches

Project previews accept ?patch=1 and only include pages listed in changedPages.
Cache hits return an empty patch list. The web client applies sparse patches by
page index instead of rebuilding every page.

Add a two-page server test that edits only the second page and checks that the
patched payload contains exactly the changed page indices. Measure the arXiv
heading edit: 2 of 3 pages still changed (315 KB vs 396 KB full, 2.66 s), so
dirty-page transfer is in place but incremental layout latency is not.

Test count is 1073 (67 server).
This commit is contained in:
gloamxun committed 2026-10-27 21:18:39 +08:00
1 parent 86e95881e7
commit 9231c3a7d6
8 files changed
+90 -17

No files matched your search

+36 -7
View File
@@ -1158,6 +1158,29 @@ fn preview_without_changes(json: &str) -> String {
replace_array_field(&json, "\"changedPages\"")
}
/// Keep only the pages listed in `changedPages`; the web client already has
/// the other page SVGs from the previous revision.
fn patch_preview_json(json: &str) -> Option<String> {
let mut value: serde_json::Value = serde_json::from_str(json).ok()?;
let changed: Vec<usize> = value
.get("changedPages")?
.as_array()?
.iter()
.filter_map(|index| index.as_u64().map(|index| index as usize))
.collect();
let pages = value.get("pages")?.as_array()?.clone();
let patches: Vec<serde_json::Value> = pages
.into_iter()
.filter(|page| {
page.get("index")
.and_then(|index| index.as_u64())
.is_some_and(|index| changed.contains(&(index as usize)))
})
.collect();
value["pages"] = serde_json::Value::Array(patches);
Some(value.to_string())
}
fn store_route_d_preview(key: (String, u64), json: String) {
if let Ok(mut cache) = route_d_cache().lock() {
cache.push((key, json));
@@ -1553,17 +1576,23 @@ fn handle_project_request(
let images = load_project_images(&expanded, &store, id);
let key = route_d_cache_key(id, &expanded, &images);
if let Some(json) = route_d_cached_preview(&key) {
return respond_json_engine(
request,
200,
&preview_without_changes(&json),
"route-d",
);
let body = preview_without_changes(&json);
let body = if query_param(url, "patch").is_some() {
patch_preview_json(&body).unwrap_or(body)
} else {
body
};
return respond_json_engine(request, 200, &body, "route-d");
}
match route_d::project_preview_json_with_images(id, &expanded, &images) {
Ok(json) => {
store_route_d_preview(key, json.clone());
return respond_json_engine(request, 200, &json, "route-d");
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");
}
Err(_) => {}
}