diff --git a/docs/csg-performance-fixtures.md b/docs/csg-performance-fixtures.md index 75e3290..3a07a1f 100644 --- a/docs/csg-performance-fixtures.md +++ b/docs/csg-performance-fixtures.md @@ -136,6 +136,13 @@ is intentionally exact and conservative: distant rejected edits can skip mesh emission, while candidate cuts still emit the full output mesh. The report exposes `reused_mesh` so the fixture and tests do not infer reuse from timing. +Benchmark correction: `rebuild()` must bypass checkpoint caches. An earlier +fixture pass accidentally let the dirty box baseline travel through cached box +state, which made incremental box edits look better than they were. The live +policy is stricter: box dirty edits use the direct builder unless a future real +mesh patcher proves otherwise; general convex dirty edits keep the valid-prefix +checkpoint path. + ## Latest Local Baseline Captured on 2026-05-07 with `.\tools\run_csg_perf.ps1 -UseRealtimeCsgCpp`: @@ -202,3 +209,11 @@ previous mesh when no geometry was touched. The single-cut case is still slightly slower because cache machinery dominates. The next hard target is dirty mesh range patching for candidate edits; replaying less CSG work is not enough when every real cut still serializes the whole result mesh. + +Corrected lesson after separating `build()` from `rebuild()`: box candidate +edits are currently better served by the direct box builder, not checkpoint +replay. The checkpoint path still wins hard for general convex tail edits: +`rotated_cut_stack_64` remains an order-of-magnitude style win in smoke runs, +and `common_box_chain_64` stays far below full dirty rebuild. Rejected general +convex suffixes can expose `reused_mesh:true`; the fixture includes +`distant_oriented_cutters_128` for that case. diff --git a/examples/csg_perf_fixture.rs b/examples/csg_perf_fixture.rs index 40fd995..b6f1bd1 100644 --- a/examples/csg_perf_fixture.rs +++ b/examples/csg_perf_fixture.rs @@ -36,6 +36,11 @@ fn main() { brushes: 513, build: distant_cutters_512, }, + PerfCase { + name: "distant_oriented_cutters_128", + brushes: 129, + build: distant_oriented_cutters_128, + }, ]; for case in cases { @@ -331,3 +336,25 @@ fn distant_cutters_512() -> Assembler { asm } + +fn distant_oriented_cutters_128() -> Assembler { + let mut asm = Assembler::new(); + asm.solid_box( + "source", + Aabb::from_center_size(Vec3::ZERO, Vec3::splat(8.0)), + MaterialId(1), + ); + + for index in 0..128 { + let row = index / 16; + let col = index % 16; + asm.cut_oriented_box( + format!("far_oriented_void_{index}"), + Vec3::new(1000.0 + col as f32 * 4.0, 1000.0 + row as f32 * 4.0, 0.0), + Vec3::splat(1.0), + Quat::from_rotation_z(index as f32 * 0.037), + ); + } + + asm +} diff --git a/src/assembler.rs b/src/assembler.rs index 27930cb..d01e47c 100644 --- a/src/assembler.rs +++ b/src/assembler.rs @@ -253,7 +253,7 @@ impl Assembler { return output.clone(); } - let output = self.build_uncached(); + let output = self.build_with_checkpoints(); *self.cache.borrow_mut() = Some(output.clone()); *self.first_dirty_index.borrow_mut() = None; output @@ -418,12 +418,17 @@ impl Assembler { .iter() .all(|brush| matches!(brush.geometry, CompiledGeometry::Box(_))) { - return self.build_axis_aligned_boxes(); + return self.build_axis_aligned_boxes_direct(); } self.build_general_direct() } + fn build_with_checkpoints(&self) -> BuildOutput { + self.try_build_axis_aligned_boxes_from_cache(Some(0)) + .unwrap_or_else(|| self.build_general_from_cache(Some(0))) + } + fn build_general_direct(&self) -> BuildOutput { let mut state = GeneralBuildState::default(); for brush in &self.compiled { @@ -432,9 +437,12 @@ impl Assembler { state.into_output(self.brushes.len(), self.generation) } - fn build_axis_aligned_boxes(&self) -> BuildOutput { - self.try_build_axis_aligned_boxes_from_cache(Some(0)) - .expect("box-only build path received a non-box brush") + fn build_axis_aligned_boxes_direct(&self) -> BuildOutput { + let mut state = BoxBuildState::default(); + for brush in &self.compiled { + state.apply_compiled_box(brush); + } + state.into_output_with_mesh(self.brushes.len(), self.generation, None) } fn try_build_axis_aligned_boxes_from_cache( @@ -451,6 +459,11 @@ impl Assembler { let brush_count = self.compiled.len(); let previous_output = self.cache.borrow().as_ref().cloned(); + if let Some(first_dirty_index) = first_dirty_index + && first_dirty_index > 0 + { + return Some(self.build_axis_aligned_boxes_direct()); + } let rebuild_from = match first_dirty_index { Some(index) => index.min(brush_count), None => self @@ -1352,16 +1365,20 @@ mod tests { Aabb::from_center_size(Vec3::ZERO, Vec3::splat(8.0)), MaterialId(1), ); - let far = asm.cut_box( + let far = asm.cut_oriented_box( "far", - Aabb::from_center_size(Vec3::splat(100.0), Vec3::splat(1.0)), + Vec3::splat(100.0), + Vec3::splat(1.0), + bevy_math::Quat::from_rotation_z(0.2), ); let before = asm.build_incremental(); assert!(asm.set_brush_primitive( far, - Primitive::Box { - bounds: Aabb::from_center_size(Vec3::splat(120.0), Vec3::splat(1.0)), + Primitive::OrientedBox { + center: Vec3::splat(120.0), + size: Vec3::splat(1.0), + rotation: bevy_math::Quat::from_rotation_z(0.2), }, )); let after = asm.build_incremental();