Reduce clipping memory usage and allocation The scratch strip buffer may be quite large when the render context has been used for rendering complex paths. If the clip geometry is simple, storing that buffer in the clip state has two downsides: - the buffer will have unused capacity, inflating the memory footprint of the renderer, - as the buffer is moved into the clip state, a new buffer has to be allocated, and for subsequent rendering of complex paths will have to grow large again. This PR proposes keeping the original allocation for the scratch strip buffer and memcpy'ing the clip's strips to a new allocation of the right size. (As a side-effect the size of the `Clip` struct is reduced from 40 to 32 bytes.) There are alternatives: the first downside could be alleviated by a `shrink_to_fit`, and allocator thrashing of the second downside could be reduced by allocating the new buffer with the same capacity as the old buffer. It's probably better to just keep the original allocation for the scratch buffer, though.
diff --git a/sparse_strips/vello_common/src/coarse.rs b/sparse_strips/vello_common/src/coarse.rs index 0f1f0b9..2948779 100644 --- a/sparse_strips/vello_common/src/coarse.rs +++ b/sparse_strips/vello_common/src/coarse.rs
@@ -42,7 +42,7 @@ /// The intersected bounding box after clip pub clip_bbox: Bbox, /// The rendered path in sparse strip representation - pub strips: Vec<Strip>, + pub strips: Box<[Strip]>, /// The fill rule used for this clip pub fill_rule: Fill, } @@ -301,7 +301,8 @@ /// - If covered by zero winding: `push_zero_clip` /// - If fully covered by non-zero winding: do nothing (clip is a no-op) /// - If partially covered: `push_clip` - pub fn push_clip(&mut self, strips: Vec<Strip>, fill_rule: Fill) { + pub fn push_clip(&mut self, strips: impl Into<Box<[Strip]>>, fill_rule: Fill) { + let strips = strips.into(); let n_strips = strips.len(); // Calculate the bounding box of the clip path in strip coordinates
diff --git a/sparse_strips/vello_cpu/src/render.rs b/sparse_strips/vello_cpu/src/render.rs index f6b9815..68931b4 100644 --- a/sparse_strips/vello_cpu/src/render.rs +++ b/sparse_strips/vello_cpu/src/render.rs
@@ -139,8 +139,8 @@ pub fn clip(&mut self, path: &BezPath) { flatten::fill(path, self.transform, &mut self.line_buf); self.make_strips(self.fill_rule); - let strips = core::mem::take(&mut self.strip_buf); - self.wide.push_clip(strips, self.fill_rule); + self.wide + .push_clip(self.strip_buf.as_slice(), self.fill_rule); } /// Set the current blend mode.