From baa73aca4e90aacef9a6f00a2b131a5119a0da7b Mon Sep 17 00:00:00 2001 From: fderuiter <127706008+fderuiter@users.noreply.github.com> Date: Fri, 30 Jan 2026 21:13:28 +0000 Subject: [PATCH] perf: Optimize isosurface extraction with value reuse Implements a sliding window optimization in `extract_isosurface` to reuse corner values from the previous iteration, reducing memory reads by 50%. Also defers the construction of `corner_values` and `corner_pos` arrays until after the early exit check for empty voxels, reducing stack usage and initialization overhead. Benchmarks show a ~7% performance improvement (runtime reduced from ~29ms to ~27ms for a 128^3 grid). Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- .../src/applied/isosurface/marching_cubes.rs | 68 +++++++++++-------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/math_explorer/src/applied/isosurface/marching_cubes.rs b/math_explorer/src/applied/isosurface/marching_cubes.rs index 7e5a21f..5118904 100644 --- a/math_explorer/src/applied/isosurface/marching_cubes.rs +++ b/math_explorer/src/applied/isosurface/marching_cubes.rs @@ -143,6 +143,8 @@ pub fn extract_isosurface(grid: &VoxelGrid, threshold: f32) -> Result = None; + // Cache for "Right Face" values of previous iteration. + let mut cached_values: Option<[f32; 4]> = None; for x in 0..grid.width - 1 { let base_idx = zy_base + x; @@ -150,8 +152,6 @@ pub fn extract_isosurface(grid: &VoxelGrid, threshold: f32) -> Result Result Result