From 798e554bc719b132ff194bee1ee26ab688f7f041 Mon Sep 17 00:00:00 2001 From: Jesus Date: Sun, 22 Nov 2026 15:25:46 -0300 Subject: [PATCH 1/2] Resident wires: test for a patch that declines before tessellating Erasing most of a drawing has the resident set built again without the erased entities. Undoing the erase then brings them back as new entities between the ones that stayed; new runs only go in at the tail, so the patch declines. The test asks for it to tell before it tessellates them, since the full build tessellates them again. `resident_patch_tessellated` counts the entities a patch tessellates. The patch tessellates all 360 before it declines: the test fails. Co-Authored-By: Claude Fable 5.2 --- src/scene/mod.rs | 5 +++ src/scene/resident_patch_adds_tests.rs | 55 ++++++++++++++++++++++++++ src/scene/resident_wires.rs | 2 + 3 files changed, 62 insertions(+) diff --git a/src/scene/mod.rs b/src/scene/mod.rs index a5deb2486e..d3c052af02 100644 --- a/src/scene/mod.rs +++ b/src/scene/mod.rs @@ -3446,6 +3446,10 @@ pub struct Scene { /// a full re-tessellation. Diagnostics + a hook for the oracle test to prove /// the fast path is actually exercised (not silently always falling back). resident_patch_hits: std::cell::Cell, + /// Count of entities the incremental patch has tessellated, whether it + /// then went in or declined: what a declined patch cost before the full + /// build did the work again. + resident_patch_tessellated: std::cell::Cell, /// Handoff for the GPU wire arena (`OCS_WIRE_GPU_PATCH`): when the MODEL /// resident set was brought up to date by an incremental patch, this records /// `(prev_gen, new_gen, changed handles)` so the render layer can patch just @@ -3805,6 +3809,7 @@ impl Scene { pending_removed_categories: RefCell::new(HashMap::default()), geometry_journal_floor: std::cell::Cell::new(0), resident_patch_hits: std::cell::Cell::new(0), + resident_patch_tessellated: std::cell::Cell::new(0), model_wire_gpu_patch: RefCell::new(HashMap::default()), undo_recording: None, } diff --git a/src/scene/resident_patch_adds_tests.rs b/src/scene/resident_patch_adds_tests.rs index 1221eab333..309c957d22 100644 --- a/src/scene/resident_patch_adds_tests.rs +++ b/src/scene/resident_patch_adds_tests.rs @@ -170,3 +170,58 @@ fn entities_added_in_several_rounds_are_patched_each_time() { } assert_eq!(drawn(&scene), drawn(&reference)); } + +/// Erasing most of a drawing has the set built again without the erased +/// entities, so undoing the erase brings them back as new ones, between the +/// entities that stayed. New runs only go in at the tail, and the patch +/// declines. It has to tell before it tessellates them all, which the full +/// build then does again: undoing an erase of 100,000 objects spent half a +/// second on that. +#[test] +fn a_patch_that_cannot_place_its_new_entities_declines_before_tessellating_them() { + let mut scene = Scene::new(); + let handles: Vec = (0..400).map(|i| scene.add_entity(line(i))).collect(); + let whole = drawn(&scene); + assert_eq!(whole.len(), 400); + + // All but every tenth entity go: more than half the set, so it is built + // again with the forty that stay. + let erased: Vec = handles + .iter() + .enumerate() + .filter(|(index, _)| index % 10 != 0) + .map(|(_, handle)| *handle) + .collect(); + let images: Vec<_> = erased + .iter() + .map(|handle| scene.document.get_entity_arc(*handle).unwrap()) + .collect(); + scene.erase_entities(&erased); + assert_eq!(drawn(&scene).len(), 40); + + assert_eq!(scene.restore_erased_entities(images).len(), 360); + let hits = scene.resident_patch_hits.get(); + let tessellated = scene.resident_patch_tessellated.get(); + let restored = drawn(&scene); + assert_eq!( + scene.resident_patch_hits.get(), + hits, + "entities that come back between the ones that stayed are not patched in" + ); + let cost = scene.resident_patch_tessellated.get() - tessellated; + assert!( + cost <= 4, + "the declined patch tessellated {cost} of the 360 entities before the full build" + ); + // By content: a newly built set lists the entities in the drawing's + // storage order, which the erase and its undo changed. + let sorted = |mut set: Vec<(String, usize, usize)>| { + set.sort(); + set + }; + assert_eq!( + sorted(restored), + sorted(whole), + "the set draws the whole drawing again" + ); +} diff --git a/src/scene/resident_wires.rs b/src/scene/resident_wires.rs index 1607460a50..f4bcb32dec 100644 --- a/src/scene/resident_wires.rs +++ b/src/scene/resident_wires.rs @@ -818,6 +818,8 @@ impl Scene { new_runs.insert(h, faded); } let tessellated = memo_updates.len(); + self.resident_patch_tessellated + .set(self.resident_patch_tessellated.get() + tessellated as u64); { let mut memo = self.resident_tess_memo.borrow_mut(); for (h, run) in memo_updates { From 8a7b1ec9e13ed250d05c621db1fe7331b987400f Mon Sep 17 00:00:00 2001 From: Jesus Date: Sun, 22 Nov 2026 16:05:03 -0300 Subject: [PATCH 2/2] Resident wires: a patch that cannot place its new runs declines first Undoing an erase of 100,000 objects spent 0.6 s in a patch of the resident wire set that then declined. Erasing that much has the set built again without the erased entities, so the undo brings them back as new entities between the ones that stayed. A new run only goes in at the tail, and the patch found that out when it placed the first of them: after it had looked up and tessellated all of them and copied their runs. The full build then tessellated them again. Before that work the patch now looks for a new entity that the order update will list before a run the patch leaves alone. Such an entity declines the patch unless it is hidden or draws nothing, so that one entity is tessellated to tell. Up to four candidates are looked at; after that the patch goes on as before. The usual add, whose key comes after every listed one, is passed over with one comparison. Co-Authored-By: Claude Fable 5.2 --- src/scene/resident_wires.rs | 127 +++++++++++++++++++++++++++++------- 1 file changed, 103 insertions(+), 24 deletions(-) diff --git a/src/scene/resident_wires.rs b/src/scene/resident_wires.rs index f4bcb32dec..7b8682a989 100644 --- a/src/scene/resident_wires.rs +++ b/src/scene/resident_wires.rs @@ -1,5 +1,9 @@ use super::*; +/// How many new entities a patch looks at to tell whether it has to decline, +/// before it stops asking and goes on. +const DECLINE_PROBES: usize = 4; + impl Scene { /// Unified static-hold wire builder — the ONE tessellation path every /// space renders through (Model tiles, BEDIT, the paper sheet block, and @@ -689,6 +693,103 @@ impl Scene { == tess_memo_guard(None, anno, annotation_scale_handle, all_visible, bg, avp) && self.resident_tess_atlas.get() == crate::scene::text::sdf_atlas::generation() && self.resident_tess_complex_lt.get() == self.viewport_complex_lt_guard(); + let tessellate = { + let document = &self.document; + let selection = &empty_sel; + let block_cache: &cache::block_cache::BlockCache = &blk; + let viewport_lt = self.viewport_lt_for(anno_scale_override); + let reference_only = self.reference_only.handles(); + move |e: &EntityType| { + tessellate_entity( + document, + selection, + avp, + bg, + anno, + annotation_scale_handle, + e, + Some(block_cache), + None, + None, + viewport_lt, + reference_only, + ) + } + }; + // A new run can only go in at the tail (see the placement below). A + // new entity that the order update below will list before a run this + // patch leaves alone therefore declines the patch, unless it is + // hidden or draws nothing. One such entity is enough to tell, before + // every changed entity is looked up and tessellated for a patch that + // will not go in: undoing an erase the set had since been built again + // without spent 0.6 s on 100,000 of them, and the full build then + // tessellated them again. + { + let has_run = |handle: &Handle| { + layout.ranges.contains_key(handle) || layout.vacant.contains_key(handle) + }; + let is_new = |&(handle, kind): &(Handle, ChangeKind)| { + !matches!(kind, ChangeKind::Removed) && !has_run(&handle) + }; + if deltas.iter().any(is_new) { + let changed: HashSet = deltas.iter().map(|&(handle, _)| handle).collect(); + let cache = self.sort_cache.borrow(); + let sort_map = cache.as_ref().and_then(|(_, index)| index.get(&block)); + let effective = |handle: Handle| { + sort_map + .and_then(|map| map.get(&handle.value())) + .copied() + .unwrap_or(handle.value()) + }; + // The last run in the order that the patch leaves alone. + let kept = layout.order.iter().rposition(|handle| { + !changed.contains(handle) && layout.ranges.contains_key(handle) + }); + if let Some(kept) = kept { + let kept_key = effective(layout.order[kept]); + let mut probes = 0; + for &(handle, _) in deltas.iter().filter(|delta| is_new(delta)) { + // The usual add: a key after every listed one. + let key = effective(handle); + if key >= kept_key { + continue; + } + let place = layout + .order + .partition_point(|&existing| effective(existing) <= key); + if place > kept { + continue; + } + if probes == DECLINE_PROBES { + break; + } + probes += 1; + // One the order already lists stands where it is, and + // is left to the placement. + if layout.order.contains(&handle) { + continue; + } + let Some(entity) = self.document.get_entity(handle) else { + continue; + }; + if !self.resident_entity_visible( + entity, + block, + frozen_layers, + annotation_scale_handle, + all_visible, + ) { + continue; + } + self.resident_patch_tessellated + .set(self.resident_patch_tessellated.get() + 1); + if !tessellate(entity).is_empty() { + return declined("new-run-not-at-the-tail"); + } + } + } + } + } // References to a block re-expanded after its definition changed // share one prototype: the first one comes back with the full run // and the others as a stub of it. Longest runs first gives the full @@ -767,32 +868,10 @@ impl Scene { // references at once: tessellate on all cores, as the full build does. let tessellated: Vec<(Handle, Vec)> = { use crate::par::prelude::*; - let document = &self.document; - let selection = &empty_sel; - let block_cache: &cache::block_cache::BlockCache = &blk; - let viewport_lt = self.viewport_lt_for(anno_scale_override); - let reference_only = self.reference_only.handles(); + let tessellate = &tessellate; groups .par_iter() - .flat_map_iter(move |group| { - group.iter().map(move |&(h, e)| { - let wires = tessellate_entity( - document, - selection, - avp, - bg, - anno, - annotation_scale_handle, - e, - Some(block_cache), - None, - None, - viewport_lt, - reference_only, - ); - (h, wires) - }) - }) + .flat_map_iter(move |group| group.iter().map(move |&(h, e)| (h, tessellate(e)))) .collect() }; for (h, mut raw) in tessellated {