perf: speed up large markdown rendering

This commit is contained in:
2026-11-06 17:18:11 +07:00
parent 310cad32be
commit e524c1a305
3 changed files with 124 additions and 39 deletions
+47
View File
@@ -447,3 +447,50 @@ fn parse_body(source: &str) -> Vec<MarkdownBlock> {
let state = MarkdownViewState::new("parse-md", source);
state.blocks().to_vec()
}
/// Mirrors the gallery's "Large file" sample: five blocks per section.
fn large_markdown(sections: usize) -> String {
let mut source = String::new();
for section in 0..sections {
source.push_str(&format!("## Section {section}\n\n"));
source.push_str(&format!(
"Handler {section} retries stale sockets and logs every outcome.\n\n"
));
source.push_str(&format!(
"- [ ] Audit handler {section}\n- See [issue {section}](https://example.com/issues/{section})\n\n"
));
source.push_str("```rust\nfn handle() -> bool { true }\n```\n\n");
}
source
}
#[test]
fn a_large_document_swaps_and_renders_without_freezing() {
let source = large_markdown(1000);
let started = std::time::Instant::now();
let (_cx, mut visual, state) = view_window("# small\n");
state.update_in(&mut visual, |state, _window, cx| {
state.set_source(source, cx)
});
visual.update(|window, cx| {
window.draw(cx).clear(cx);
});
let elapsed = started.elapsed();
assert!(
elapsed < std::time::Duration::from_secs(15),
"swapping to a large document took {elapsed:?}; the frame must not
measure every block"
);
let count = _cx.update(|cx| state.read(cx).blocks().len());
assert_eq!(count, 5000, "five blocks per section");
assert!(
visual.debug_bounds("test-md-block-0").is_some(),
"the first block renders after the swap"
);
assert!(
visual.debug_bounds("test-md-block-4999").is_none(),
"blocks past the viewport never render"
);
}