Sketch display: each sketch samples within its own budget (#6524) (#6838)

* claim: #6524

* Sketch display: each sketch samples within its own budget (#6524)

After a large DXF was inserted, lines drawn in a later sketch vanished
once finished: only their end points showed and could be picked, and
hiding the DXF sketch brought them back.

The display cache had one sampling budget for every shown sketch (4096
curves, 131 072 vertices per model state and view). Sketches are drawn
and picked in timeline order, so a large imported drawing used it all
and every later curve came back empty (BudgetLimited): not drawn, not
pickable, while the points, drawn separately, stayed.

Each sketch now has its own budget with the same limits (curves,
profiles, vertices and nodes). The cache as a whole is capped at 65 536
curves and 16 384 profiles, so memory stays bounded however many
sketches are shown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FN5eEMrqWWXqi65FcK8GXq

---------

Co-authored-by: sascha2101 <221609842+sascha2101@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-10-26 10:42:35 +00:00
1 parent 1cd14e2412
commit 7ae17e6dd5
2 files changed
+96 -31

No files matched your search

+60 -31
View File
@@ -173,18 +173,37 @@ pub fn boundary_parts(loop_: &Loop2, plane: &Plane, proj: &Proj, ppp: f32) -> Ve
loop_.segs.iter().flat_map(|segment| sample_segment(segment, plane, &pixel_view(proj, ppp), &mut budget).parts).collect()
}
/// What one sketch may sample per model state and view: its curves and profiles cached, and the
/// vertices and nodes they take. Each sketch has its own (#6524): one shared by every sketch
/// let a large one drawn first (an imported drawing) use it all, so a later sketch's curves came
/// back empty, neither drawn nor pickable, while their points still showed.
struct SketchBudget {
sampling: SamplingBudget,
curves: usize,
profiles: usize,
}
impl Default for SketchBudget {
fn default() -> Self {
Self { sampling: SamplingBudget::new(131_072, 1_048_576), curves: 0, profiles: 0 }
}
}
/// The most curves and profiles one sketch caches per model state and view.
const SKETCH_CURVES: usize = 4096;
const SKETCH_PROFILES: usize = 1024;
/// The most curves (and profiles) cached for every sketch together: a bound on memory however
/// many sketches are shown.
const CACHED_CURVES: usize = 65_536;
const CACHED_PROFILES: usize = 16_384;
#[derive(Default)]
pub struct DisplayCache {
state: Option<Arc<ModelState>>,
view: u64,
curves: HashMap<(u64, usize, [u64; 9]), Arc<SampledPath>>,
profiles: HashMap<(u64, usize, [u64; 9]), Arc<Option<Region2>>>,
budget: SamplingBudget,
}
impl Default for DisplayCache {
fn default() -> Self {
Self { state: None, view: 0, curves: HashMap::new(), profiles: HashMap::new(), budget: SamplingBudget::new(131_072, 1_048_576) }
}
budgets: HashMap<u64, SketchBudget>,
}
impl DisplayCache {
@@ -202,7 +221,7 @@ impl DisplayCache {
if view != self.view || self.state.as_ref().is_none_or(|old| !Arc::ptr_eq(old, state)) {
self.curves.clear();
self.profiles.clear();
self.budget = SamplingBudget::new(131_072, 1_048_576);
self.budgets.clear();
self.view = view;
self.state = Some(Arc::clone(state));
}
@@ -213,18 +232,24 @@ impl DisplayCache {
let plane_key =
[plane.origin.x, plane.origin.y, plane.origin.z, plane.x.x, plane.x.y, plane.x.z, plane.y.x, plane.y.y, plane.y.z].map(f64::to_bits);
let key = (sketch, index, plane_key);
if !self.curves.contains_key(&key) && (self.curves.len() >= 4096 || self.budget.remaining_vertices == 0 || self.budget.remaining_nodes == 0) {
if let Some(path) = self.curves.get(&key) {
return path.clone();
}
let budget = self.budgets.entry(sketch).or_default();
if budget.curves >= SKETCH_CURVES
|| self.curves.len() >= CACHED_CURVES
|| budget.sampling.remaining_vertices == 0
|| budget.sampling.remaining_nodes == 0
{
return Arc::new(SampledPath { parts: Vec::new(), quality: SamplingQuality::BudgetLimited, vertex_count: 0 });
}
self.curves
.entry(key)
.or_insert_with(|| {
Arc::new(state.sketch(sketch).map_or_else(
|| SampledPath { parts: Vec::new(), quality: SamplingQuality::Invalid, vertex_count: 0 },
|ss| curve_with_budget(&ss.sketch, index, plane, proj, ppp, &mut self.budget),
))
})
.clone()
let path = Arc::new(state.sketch(sketch).map_or_else(
|| SampledPath { parts: Vec::new(), quality: SamplingQuality::Invalid, vertex_count: 0 },
|ss| curve_with_budget(&ss.sketch, index, plane, proj, ppp, &mut budget.sampling),
));
budget.curves += 1;
self.curves.insert(key, path.clone());
path
}
fn profile(&mut self, state: Arc<ModelState>, sketch: u64, index: usize, plane: &Plane, proj: &Proj, ppp: f32) -> Arc<Option<Region2>> {
@@ -232,22 +257,26 @@ impl DisplayCache {
let plane_key =
[plane.origin.x, plane.origin.y, plane.origin.z, plane.x.x, plane.x.y, plane.x.z, plane.y.x, plane.y.y, plane.y.z].map(f64::to_bits);
let key = (sketch, index, plane_key);
if !self.profiles.contains_key(&key)
&& (self.profiles.len() >= 1024 || self.budget.remaining_vertices == 0 || self.budget.remaining_nodes == 0)
if let Some(region) = self.profiles.get(&key) {
return region.clone();
}
let budget = self.budgets.entry(sketch).or_default();
if budget.profiles >= SKETCH_PROFILES
|| self.profiles.len() >= CACHED_PROFILES
|| budget.sampling.remaining_vertices == 0
|| budget.sampling.remaining_nodes == 0
{
return Arc::new(None);
}
self.profiles
.entry(key)
.or_insert_with(|| {
Arc::new(
state
.sketch(sketch)
.and_then(|ss| ss.profiles.get(index))
.and_then(|profile| display_region_with_budget(&profile.region, plane, proj, ppp, &mut self.budget)),
)
})
.clone()
let region = Arc::new(
state
.sketch(sketch)
.and_then(|ss| ss.profiles.get(index))
.and_then(|profile| display_region_with_budget(&profile.region, plane, proj, ppp, &mut budget.sampling)),
);
budget.profiles += 1;
self.profiles.insert(key, region.clone());
region
}
}
@@ -290,6 +290,42 @@ fn pixel_frame_budget_does_not_cache_unbounded_empty_curve_results() {
assert_eq!(h.app.session.doc.to_json(), baseline);
}
/// #6524: an imported drawing of 5000 lines, shown and sampled first (sketches are drawn in
/// timeline order), uses up its own sketch's budget only. A line drawn in a later sketch is still
/// sampled, so drawn and pickable, instead of coming back empty while its end points show.
#[test]
fn a_large_sketch_drawn_first_leaves_a_later_sketch_its_curves() {
let mut h = Harness::new();
h.app.set_origin_shown(false);
let mut dxf = String::from("0\nSECTION\n2\nENTITIES\n");
for i in 0..5000 {
let (x, y) = (f64::from(i % 100) * 2.0, f64::from(i / 100) * 2.0);
dxf.push_str(&format!("0\nLINE\n8\n0\n10\n{x}\n20\n{y}\n11\n{}\n21\n{y}\n", x + 1.5));
}
dxf.push_str("0\nENDSEC\n0\nEOF\n");
h.app.run("sketch.insert_dxf", json!({"text": dxf, "plane": "XY", "name": "Drawing"})).unwrap();
h.app.run("sketch.finish", json!({})).unwrap();
h.app.run("sketch.create", json!({"plane":"XY","name":"After"})).unwrap();
h.app.run("sketch.line", json!({"points":[[-30,-30],[-10,-20]],"ids":["new"]})).unwrap();
h.settle();
let proj = projection(&h.app, h.app.viewport.rect.unwrap());
let state = h.app.session.world_state();
let drawing = state.sketches.iter().find(|s| s.name == "Drawing").unwrap().clone();
let after = state.sketches.iter().find(|s| s.name == "After").unwrap().clone();
drop(state);
assert_eq!(drawing.sketch.curves.len(), 5000);
// A fresh frame that samples the drawing first, as the viewport does.
*h.app.viewport.display.lock().unwrap() = Default::default();
let limited = (0..drawing.sketch.curves.len())
.filter(|&i| curve_sample(&h.app, drawing.feature, i, &drawing.plane, &proj).quality == SamplingQuality::BudgetLimited)
.count();
assert!(limited > 0, "the drawing must exceed one sketch's budget for this to test anything");
let line = after.sketch.curve_index("new").unwrap();
let path = curve_sample(&h.app, after.feature, line, &after.plane, &proj);
assert_eq!((path.quality, path.vertex_count), (SamplingQuality::Complete, 2), "{path:?}");
assert_eq!(path.parts.len(), 1);
}
/// A zoomed-in ring is sampled finely, but each slab of the fill still crosses only its
/// four boundary edges: its fill must fit the hover highlight's budget (#164).
#[test]