Optimize monotonic conversion a little bit

This commit is contained in:
Patrick Walton 2019-02-01 19:10:42 -08:00
parent ca3a8852a6
commit 70e615205e
2 changed files with 14 additions and 2 deletions

View File

@ -286,7 +286,7 @@ trait ContourClipper where Self::Edge: TEdge {
}
}
let input = mem::replace(self.contour_mut(), Contour::new());
let input = self.contour_mut().take();
for mut segment in input.iter() {
// Easy cases.
match edge.trivially_test_segment(&segment) {

View File

@ -178,6 +178,17 @@ impl Contour {
Contour { points: vec![], flags: vec![], bounds: Rect::zero() }
}
// Replaces this contour with a new one, with arrays preallocated to match `self`.
#[inline]
pub(crate) fn take(&mut self) -> Contour {
let length = self.len() as usize;
mem::replace(self, Contour {
points: Vec::with_capacity(length),
flags: Vec::with_capacity(length),
bounds: Rect::zero(),
})
}
#[inline]
pub fn iter(&self) -> ContourIter {
ContourIter { contour: self, index: 1 }
@ -345,7 +356,8 @@ impl Contour {
#[inline]
pub fn make_monotonic(&mut self) {
let contour = mem::replace(self, Contour::new());
// TODO(pcwalton): Make monotonic in place?
let contour = self.take();
for segment in MonotonicConversionIter::new(contour.iter()) {
self.push_segment(segment);
}