diff --git a/crates/engine/src/cmd/review/changes.rs b/crates/engine/src/cmd/review/changes.rs index 6cdac0b465..b11479cda5 100644 --- a/crates/engine/src/cmd/review/changes.rs +++ b/crates/engine/src/cmd/review/changes.rs @@ -450,40 +450,69 @@ fn remap_cell_props(mut c: &mut wordcraft_doc::props::CellProps, remap: &[Option changed } -fn collect_object(obj: &InlineObject, f: &mut impl FnMut(u32) -> bool) { - let mut stack = vec![obj]; - while let Some(o) = stack.pop() { - match o { - InlineObject::Ruby { props, .. } => collect_char(props, f), - InlineObject::ControlStart { control } => { - if let Some(rp) = &control.run_props { - collect_char(rp, f); - } +fn collect_leaf_object(obj: &InlineObject, f: &mut impl FnMut(u32) -> bool) { + match obj { + InlineObject::Ruby { props, .. } => collect_char(props, f), + InlineObject::ControlStart { control } => { + if let Some(rp) = &control.run_props { + collect_char(rp, f); } - InlineObject::Group { children, .. } => { - stack.extend(children.iter().map(|ch| &ch.obj)); - } - _ => {} } + _ => {} + } +} + +fn collect_object(obj: &InlineObject, f: &mut impl FnMut(u32) -> bool) { + let InlineObject::Group { children, .. } = obj else { + collect_leaf_object(obj, f); + return; + }; + let mut stack = Vec::new(); + let mut cur = children.as_slice(); + loop { + for ch in cur { + match &ch.obj { + InlineObject::Group { children: inner, .. } => { + if !inner.is_empty() { + stack.push(inner.as_slice()); + } + } + other => collect_leaf_object(other, f), + } + } + let Some(next) = stack.pop() else { break }; + cur = next; + } +} + +fn remap_leaf_object(obj: &mut InlineObject, remap: &[Option]) -> bool { + match obj { + InlineObject::Ruby { props, .. } => remap_char(props, remap), + InlineObject::ControlStart { control } => control.run_props.as_mut().is_some_and(|rp| remap_char(rp, remap)), + _ => false, } } fn remap_object(obj: &mut InlineObject, remap: &[Option]) -> bool { + let InlineObject::Group { children, .. } = obj else { + return remap_leaf_object(obj, remap); + }; let mut changed = false; - let mut stack = vec![obj]; - while let Some(o) = stack.pop() { - match o { - InlineObject::Ruby { props, .. } => changed |= remap_char(props, remap), - InlineObject::ControlStart { control } => { - if let Some(rp) = &mut control.run_props { - changed |= remap_char(rp, remap); + let mut stack = Vec::new(); + let mut cur = children.as_mut_slice(); + loop { + for ch in cur { + match &mut ch.obj { + InlineObject::Group { children: inner, .. } => { + if !inner.is_empty() { + stack.push(inner.as_mut_slice()); + } } + other => changed |= remap_leaf_object(other, remap), } - InlineObject::Group { children, .. } => { - stack.extend(children.iter_mut().map(|ch| &mut ch.obj)); - } - _ => {} } + let Some(next) = stack.pop() else { break }; + cur = next; } changed } @@ -506,37 +535,49 @@ fn remap_controls(wrap: &mut wordcraft_doc::ControlWrap, remap: &[Option]) changed } -fn collect_block(block: &wordcraft_doc::Block, f: &mut impl FnMut(u32) -> bool) { - let mut stack = vec![block]; - while let Some(b) = stack.pop() { - match b { - wordcraft_doc::Block::Para(p) => { - collect_para_props(&p.props, f); - for r in &p.runs { - collect_char(&r.props, f); - } - collect_char(&p.mark, f); - if let Some(sec) = p.section.as_deref() { - collect_section(sec, f); - } - for obj in &p.objects { - collect_object(obj, f); - } - } - wordcraft_doc::Block::Table(t) => { - collect_table_props(&t.props, f); - collect_controls(&t.controls, f); - for row in &t.rows { - collect_row_props(&row.props, f); - collect_controls(&row.controls, f); - for cell in &row.cells { - collect_cell_props(&cell.props, f); - collect_controls(&cell.controls, f); - stack.extend(cell.blocks.iter().map(std::sync::Arc::as_ref)); +fn collect_para(p: &Paragraph, f: &mut impl FnMut(u32) -> bool) { + collect_para_props(&p.props, f); + for r in &p.runs { + collect_char(&r.props, f); + } + collect_char(&p.mark, f); + if let Some(sec) = p.section.as_deref() { + collect_section(sec, f); + } + for obj in &p.objects { + collect_object(obj, f); + } +} + +fn collect_table(table: &wordcraft_doc::table::Table, f: &mut impl FnMut(u32) -> bool) { + let mut stack = Vec::new(); + let mut cur = table; + loop { + collect_table_props(&cur.props, f); + collect_controls(&cur.controls, f); + for row in &cur.rows { + collect_row_props(&row.props, f); + collect_controls(&row.controls, f); + for cell in &row.cells { + collect_cell_props(&cell.props, f); + collect_controls(&cell.controls, f); + for b in &cell.blocks { + match b.as_ref() { + wordcraft_doc::Block::Para(p) => collect_para(p, f), + wordcraft_doc::Block::Table(nested) => stack.push(nested), } } } } + let Some(next) = stack.pop() else { break }; + cur = next; + } +} + +fn collect_block(block: &wordcraft_doc::Block, f: &mut impl FnMut(u32) -> bool) { + match block { + wordcraft_doc::Block::Para(p) => collect_para(p, f), + wordcraft_doc::Block::Table(t) => collect_table(t, f), } } @@ -737,6 +778,12 @@ fn rewrite_doc_revisions(doc: &mut wordcraft_doc::Document, remap: &[Option remap_numbering(&mut doc.numbering, remap); } +/// Retain only revision entries still referenced by `doc`, compacting `doc.revisions` and remapping +/// surviving references (or clearing invalid out-of-bounds ones). On the common path with no +/// surviving references, the walk avoids heap allocation for paragraphs, non-nested tables, and +/// non-nested inline objects (an iterative stack allocates only for tables inside tables or groups +/// inside groups), `BTreeSet::new()` never allocates, and `doc.revisions.clear()` runs directly +/// without allocating `remap`/`kept` or running the rewrite pass. fn retain_referenced_revisions(doc: &mut wordcraft_doc::Document) { let old_len = doc.revisions.len(); let mut used = std::collections::BTreeSet::new();