mirror of
https://github.com/storytold/wordcraft.git
synced 2026-12-02 10:47:02 -06:00
review: avoid per-block/object heap allocation in revision retention walk (#8129)
Dispatch paragraphs directly in collect_block and non-Group inline objects directly in collect_object/remap_object, initializing iterative stacks lazily with Vec::new() so heap allocation occurs only for tables nested inside tables or non-empty groups nested inside groups. On the common no-surviving-references fast path, retain_referenced_revisions performs zero heap allocations for paragraphs, non-nested tables, and non-nested inline objects while keeping every resolution output identical.
This commit is contained in:
@@ -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<u32>]) -> 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<u32>]) -> 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<u32>])
|
||||
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<u32>
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user