Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

perf(layout): avoid redundant box moves and inline style conversion

ldm01e8f951ef2ac
diff --git a/moli-layout/src/inline.rs b/moli-layout/src/inline.rs
index 945a04ce5..03c0977bf 100644
--- a/moli-layout/src/inline.rs
+++ b/moli-layout/src/inline.rs
@@ -2091,7 +2091,23 @@ impl InlineBuildInput {
let mut style_parents = Vec::new();
let mut style_samples = Vec::new();
let mut resolved_runs = Vec::<(Range<usize>, usize)>::new();
- for unit in &self.units {
+ // Source mapping, whitespace and selection still use individual units.
+ // Style conversion only needs a contiguous run with the same style,
+ // direct structural parent and paint state. Avoid repeating it for
+ // every character; unicode-range font selection remains per character.
+ for units in self.units.chunk_by(|left, right| {
+ left.style_box == right.style_box
+ && left.ancestors.last() == right.ancestors.last()
+ && left.control == right.control
+ && left.output_range.end == right.output_range.start
+ }) {
+ let unit = &units[0];
+ let output_range = unit.output_range.start
+ ..units
+ .last()
+ .expect("style chunks are nonempty")
+ .output_range
+ .end;
let mut base_style = world.boxes[unit.style_box.index()]
.style
.parley_text_style();
@@ -2102,7 +2118,7 @@ impl InlineBuildInput {
let structural_parent = unit.ancestors.last().copied().unwrap_or(self.root_style);
if !parley.requires_character_font_resolution(&base_style) {
let sample = (!unit.control)
- .then(|| self.text[unit.output_range.clone()].chars().next())
+ .then(|| self.text[output_range.clone()].chars().next())
.flatten();
parley.resolve_font_families(&mut base_style, None);
let style_slot = intern_resolved_inline_style(
@@ -2113,15 +2129,11 @@ impl InlineBuildInput {
structural_parent,
sample,
);
- append_resolved_inline_run(
- &mut resolved_runs,
- unit.output_range.clone(),
- style_slot,
- );
+ append_resolved_inline_run(&mut resolved_runs, output_range, style_slot);
continue;
}
- for (relative_start, character) in self.text[unit.output_range.clone()].char_indices() {
- let start = unit.output_range.start + relative_start;
+ for (relative_start, character) in self.text[output_range.clone()].char_indices() {
+ let start = output_range.start + relative_start;
let end = start + character.len_utf8();
let mut style = base_style.clone();
parley.resolve_font_families(&mut style, Some(character));
diff --git a/moli-layout/src/world.rs b/moli-layout/src/world.rs
index 2b8651402..4ebd9ce0c 100644
--- a/moli-layout/src/world.rs
+++ b/moli-layout/src/world.rs
@@ -918,6 +918,15 @@ where
stack.extend(self.boxes[id.index()].children.iter().copied());
}
+ // Most trees do not discard any construction boxes. Their IDs already
+ // form the final dense mapping; keep the pass-local allocation instead
+ // of moving every box and rebuilding every child vector unchanged.
+ if reachable.iter().all(|is_reachable| *is_reachable) {
+ self.display_contents_mapping
+ .retain(|_, ids| !ids.is_empty());
+ return;
+ }
+
let mut remap = vec![None; self.boxes.len()];
let mut next_index = 0;
for (index, is_reachable) in reachable.iter().copied().enumerate() {