mirror of
https://github.com/storytold/wordcraft.git
synced 2026-11-20 09:25:21 +00:00
Layout: typing in a long document is about 5 times faster with proofing on (#6207)
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c99dd5cc6e
commit
0b99c871e9
3 files changed
+40
-5
No files matched your search
@@ -0,0 +1,32 @@
|
||||
//! Typing timing: opens a document, types at the middle of it, and prints the layout time of
|
||||
//! each keystroke (the pagination every edit pays for).
|
||||
//!
|
||||
//! `cargo run --release -p wordcraft-engine --example typing_bench -- doc.docx [keys]`
|
||||
|
||||
use serde_json::json;
|
||||
use wordcraft_engine::Session;
|
||||
|
||||
fn main() -> Result<(), String> {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let path = args.first().ok_or("usage: typing_bench doc.docx [keys]")?;
|
||||
let keys: usize = args.get(1).and_then(|s| s.parse().ok()).unwrap_or(40);
|
||||
let mut s = Session::new(wordcraft_engine::doc::Document::new());
|
||||
s.run("file.open", &json!({"path": path})).map_err(|e| e.to_string())?;
|
||||
let first = s.layout().ms;
|
||||
let mid = s.doc.body.len() / 2;
|
||||
let block = (mid..s.doc.body.len()).chain((0..mid).rev()).find(|&i| s.doc.body.get(i).and_then(|b| b.as_para()).is_some()).unwrap_or(mid);
|
||||
s.run("caret.set", &json!({"pos": {"story": "body", "path": [block], "off": 0}})).map_err(|e| e.to_string())?;
|
||||
let mut ms = Vec::with_capacity(keys);
|
||||
for _ in 0..keys {
|
||||
s.run("text.insert", &json!({"text": "a"})).map_err(|e| e.to_string())?;
|
||||
ms.push(s.layout().ms);
|
||||
}
|
||||
ms.sort_by(f64::total_cmp);
|
||||
let med = ms.get(ms.len() / 2).copied().unwrap_or(0.0);
|
||||
println!(
|
||||
"blocks {}, first layout {first:.1} ms, per key: median {med:.1} ms, min {:.1} ms",
|
||||
s.doc.body.len(),
|
||||
ms.first().copied().unwrap_or(0.0)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -276,7 +276,7 @@ impl Ctx<'_> {
|
||||
}
|
||||
|
||||
/// `p` laid out on its own, as the paragraph after a kept one is measured before it is placed
|
||||
/// (no wrap areas, proofing, typed word or resized text boxes), through the cache when
|
||||
/// (no wrap areas, typed word or resized text boxes), through the cache when
|
||||
/// [`Ctx::para_labelled`] would lay it out with those same inputs: it is then not laid out a
|
||||
/// second time when it is placed. Counts nothing (list labels, equation numbers, page totals).
|
||||
/// `None` when the inputs differ.
|
||||
@@ -285,7 +285,7 @@ impl Ctx<'_> {
|
||||
.objects
|
||||
.iter()
|
||||
.any(|o| matches!(o, InlineObject::Shape { story: Some(_), float, .. } if float.wrap == Wrap::Inline && float.rot == 0.0));
|
||||
if self.opts.proofing || self.boxes.inside() || resized || self.hide_deleted != self.opts.hides_deleted(StoryRef::Body) {
|
||||
if self.boxes.inside() || resized || self.hide_deleted != self.opts.hides_deleted(StoryRef::Body) {
|
||||
return None;
|
||||
}
|
||||
let kept = (self.eq_count, self.uses_totals);
|
||||
|
||||
@@ -3035,8 +3035,8 @@ fn cache_reuses_unchanged_paragraphs() {
|
||||
}
|
||||
|
||||
/// The paragraph after a heading kept with it is measured before the heading is placed. That
|
||||
/// layout goes into the cache under the key it is placed with, so it is laid out once, and the
|
||||
/// pages are those of the uncached measurement (proofing on measures without the cache).
|
||||
/// layout goes into the cache under the key it is placed with, so it is laid out once, with
|
||||
/// proofing off or on, and proofing moves nothing.
|
||||
#[test]
|
||||
fn kept_headings_measure_the_next_paragraph_through_the_cache() {
|
||||
let mut d = Document::new();
|
||||
@@ -3053,7 +3053,10 @@ fn kept_headings_measure_the_next_paragraph_through_the_cache() {
|
||||
assert!(l.pages.len() > 2, "{}", l.pages.len());
|
||||
assert_eq!(c.len(), d.body.len(), "one layout per paragraph");
|
||||
assert_eq!(c.misses, d.body.len() as u64, "each paragraph laid out once");
|
||||
let proofed = layout(&d, &mut LayoutCache::new(), &LayoutOptions { proofing: true, ..Default::default() });
|
||||
let mut c = LayoutCache::new();
|
||||
let proofed = layout(&d, &mut c, &LayoutOptions { proofing: true, ..Default::default() });
|
||||
assert_eq!(c.misses, d.body.len() as u64, "each paragraph laid out once with proofing on");
|
||||
assert!(c.hits >= 40, "each kept heading should reuse its following paragraph layout with proofing on; got {} cache hits", c.hits);
|
||||
let starts = |l: &DocLayout| (0..d.body.len()).map(|i| l.caret(&Pos::body(i, 0)).map(|c| (c.page, c.top.to_bits()))).collect::<Vec<_>>();
|
||||
assert_eq!(starts(&l), starts(&proofed));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user