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..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 { @@ -818,6 +897,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 {