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:
1 parent
da30705cd0
commit
7528ea26e2
2 files changed
+121
-10
No files matched your search
+106
@@ -24,7 +24,28 @@ use texrra_tex_core::{
|
||||
|
||||
const SCALE: i64 = 65_536;
|
||||
|
||||
#[derive(Clone, Hash, PartialEq, Eq)]
|
||||
struct ParagraphCacheKey {
|
||||
text: String,
|
||||
font: ParagraphFont,
|
||||
heading: Option<u8>,
|
||||
font_size: Option<i64>,
|
||||
width: Option<i64>,
|
||||
centered: bool,
|
||||
right_aligned: bool,
|
||||
left_skip: i64,
|
||||
right_skip: i64,
|
||||
line_height: i64,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ParagraphCache {
|
||||
geometry: String,
|
||||
units: HashMap<ParagraphCacheKey, Unit>,
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static PARAGRAPH_CACHE: RefCell<ParagraphCache> = RefCell::new(ParagraphCache::default());
|
||||
static ROUTE_D_SESSION: RefCell<Option<(Document, Engine)>> = const { RefCell::new(None) };
|
||||
}
|
||||
|
||||
@@ -13336,6 +13357,47 @@ fn build_document_with_labels(
|
||||
label_units: &mut HashMap<String, u64>,
|
||||
outline_units: &mut Vec<(String, u8, u64)>,
|
||||
) -> Document {
|
||||
// Do not format the whole PageGeometry: it contains the decoded raster
|
||||
// images. These fields cover paragraph shaping and line breaking.
|
||||
let cache_signature = format!(
|
||||
"{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}|{:?}",
|
||||
geometry.line_width,
|
||||
geometry.base_font_size,
|
||||
geometry.par_indent,
|
||||
geometry.baseline_skip,
|
||||
geometry.linespread,
|
||||
geometry.left_skip,
|
||||
geometry.right_skip,
|
||||
geometry.par_shape,
|
||||
geometry.hang_indent,
|
||||
geometry.hang_after,
|
||||
geometry.par_fillskip,
|
||||
geometry.looseness,
|
||||
geometry.lineskip,
|
||||
geometry.lineskiplimit,
|
||||
geometry.emergency_stretch,
|
||||
geometry.pretolerance,
|
||||
geometry.tolerance,
|
||||
geometry.hyphen_penalty,
|
||||
geometry.ex_hyphen_penalty,
|
||||
geometry.double_hyphen_demerits,
|
||||
geometry.final_hyphen_demerits,
|
||||
geometry.line_penalty,
|
||||
geometry.adj_demerits,
|
||||
geometry.french_spacing,
|
||||
geometry.hyphenation_exceptions,
|
||||
geometry.space_skip,
|
||||
geometry.xspace_skip,
|
||||
geometry.space_factor_codes,
|
||||
geometry.document_class,
|
||||
);
|
||||
PARAGRAPH_CACHE.with(|cache| {
|
||||
let mut cache = cache.borrow_mut();
|
||||
if cache.geometry != cache_signature {
|
||||
cache.geometry = cache_signature;
|
||||
cache.units.clear();
|
||||
}
|
||||
});
|
||||
let mut paragraphs = expand_maketitle(paragraphs, geometry);
|
||||
if let Some(name) = geometry.abstract_name.as_deref() {
|
||||
rename_abstract_heading(&mut paragraphs, name);
|
||||
@@ -13532,6 +13594,22 @@ fn build_document_with_labels(
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
let cache_key = if paragraph.footnotes.is_empty() {
|
||||
Some(ParagraphCacheKey {
|
||||
text: paragraph.text.clone(),
|
||||
font: paragraph.font,
|
||||
heading: paragraph.heading_level,
|
||||
font_size: paragraph.font_size,
|
||||
width: paragraph.width,
|
||||
centered: paragraph.centered,
|
||||
right_aligned: paragraph.right_aligned,
|
||||
left_skip: paragraph.left_skip,
|
||||
right_skip: paragraph.right_skip,
|
||||
line_height: paragraph.line_height,
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if paragraph.math {
|
||||
let font = Cmr10Metrics::Math;
|
||||
let atom = if paragraph.multline {
|
||||
@@ -13594,8 +13672,28 @@ fn build_document_with_labels(
|
||||
},
|
||||
None => atom,
|
||||
};
|
||||
let math_started = std::time::Instant::now();
|
||||
let lines = split_math_lines(&atom, &font, line_width);
|
||||
let mut unit = Unit::math_lines(index as u64, &lines, &font);
|
||||
if std::env::var_os("TEXRRA_PROFILE").is_some()
|
||||
&& math_started.elapsed().as_millis() >= 5
|
||||
{
|
||||
eprintln!(
|
||||
"PROFILE math={:.1}ms text={:?}",
|
||||
math_started.elapsed().as_secs_f64() * 1000.0,
|
||||
paragraph.text.chars().take(80).collect::<String>()
|
||||
);
|
||||
}
|
||||
unit.space_before = paragraph.space_before;
|
||||
unit.space_stretch = paragraph.space_stretch;
|
||||
unit.space_shrink = paragraph.space_shrink;
|
||||
return unit;
|
||||
}
|
||||
if let Some(key) = &cache_key
|
||||
&& let Some(mut unit) =
|
||||
PARAGRAPH_CACHE.with(|cache| cache.borrow().units.get(key).cloned())
|
||||
{
|
||||
unit.id = index as u64;
|
||||
unit.space_before = paragraph.space_before;
|
||||
unit.space_stretch = paragraph.space_stretch;
|
||||
unit.space_shrink = paragraph.space_shrink;
|
||||
@@ -13773,6 +13871,14 @@ fn build_document_with_labels(
|
||||
unit.space_before = paragraph.space_before;
|
||||
unit.space_stretch = paragraph.space_stretch;
|
||||
unit.space_shrink = paragraph.space_shrink;
|
||||
if let Some(key) = cache_key {
|
||||
PARAGRAPH_CACHE.with(|cache| {
|
||||
let mut cache = cache.borrow_mut();
|
||||
if cache.units.len() < 1024 {
|
||||
cache.units.insert(key, unit.clone());
|
||||
}
|
||||
});
|
||||
}
|
||||
unit
|
||||
})
|
||||
.collect();
|
||||
|
||||
Reference in new issue
Block a user