Skip to content

Commit

Permalink
Minor code cleanup and avoid over allocation in some places
Browse files Browse the repository at this point in the history
- Use with_capacity instead of reserve in some places to avoid over
  allocation behavior of Vec
- Replace for loop on iterator with extend for consistency
  • Loading branch information
jbuckmccready committed Aug 31, 2023
1 parent c666c36 commit ad4d663
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
16 changes: 7 additions & 9 deletions cavalier_contours/src/polyline/internal/pline_boolean.rs
Expand Up @@ -373,18 +373,16 @@ where
slices_remaining.reserve(2 * boolean_info.overlapping_slices.len());

// add pline1 overlapping slices
for overlapping_slice in boolean_info.overlapping_slices.iter() {
let slice = BooleanPlineSlice::from_overlapping(
pline2,
overlapping_slice,
overlapping_slice.opposing_directions,
);
slices_remaining.push(slice);
}
slices_remaining.extend(
boolean_info
.overlapping_slices
.iter()
.map(|s| BooleanPlineSlice::from_overlapping(pline2, s, s.opposing_directions)),
);

let start_of_pline2_overlapping_slices = slices_remaining.len();

// add pline2 overlapping slices (note they are already oriented the same as pline2)
// add pline2 overlapping slices (note they are already oriented with same direction as pline2)
slices_remaining.extend(
boolean_info
.overlapping_slices
Expand Down
24 changes: 12 additions & 12 deletions cavalier_contours/src/polyline/internal/pline_offset.rs
Expand Up @@ -35,14 +35,6 @@ where
P: PlineSource<Num = T> + ?Sized,
T: Real,
{
let mut result = Vec::new();
let seg_count = polyline.segment_count();
if seg_count == 0 {
return result;
}

result.reserve(seg_count);

let process_line_seg = |v1: PlineVertex<T>, v2: PlineVertex<T>| -> RawPlineOffsetSeg<T> {
let line_v = v2.pos() - v1.pos();
let offset_v = line_v.unit_perp().scale(offset);
Expand Down Expand Up @@ -78,13 +70,21 @@ where
}
};

for (v1, v2) in polyline.iter_segments() {
// Note: using with_capacity to ensure exact allocation required for the end result (avoids
// over allocating and resize allocations)
let mut result = Vec::with_capacity(polyline.segment_count());
result.extend(polyline.iter_segments().map(|(v1, v2)| {
if v1.bulge_is_zero() {
result.push(process_line_seg(v1, v2));
process_line_seg(v1, v2)
} else {
result.push(process_arc_seg(v1, v2));
process_arc_seg(v1, v2)
}
}
}));

debug_assert!(
result.capacity() == polyline.segment_count(),
"ensure exact allocation"
);

result
}
Expand Down
26 changes: 15 additions & 11 deletions cavalier_contours/src/polyline/traits.rs
Expand Up @@ -854,32 +854,36 @@ pub trait PlineSource {
.chain(self.iter_vertexes().take(start))
};

let mut result = Self::OutputPolyline::with_capacity(0, true);

let start_v = self.at(start_index);
if start_v.pos().fuzzy_eq_eps(point, pos_equal_eps) {
// Note: using with_capacity to ensure exact allocation required for the end result (avoids
// over allocating and resize allocations)
let result = if start_v.pos().fuzzy_eq_eps(point, pos_equal_eps) {
// point lies on top of start index vertex
result.extend_vertexes(wrapping_vertexes_starting_at(start_index))
let mut r = Self::OutputPolyline::with_capacity(vc, true);
r.extend_vertexes(wrapping_vertexes_starting_at(start_index));
r
} else {
// check if it's at the end of the segment, if it is then use that next index
let next_index = self.next_wrapping_index(start_index);
if point.fuzzy_eq_eps(self.at(next_index).pos(), pos_equal_eps) {
result.reserve(vc);
result.extend_vertexes(wrapping_vertexes_starting_at(next_index));
let mut r = Self::OutputPolyline::with_capacity(vc, true);
r.extend_vertexes(wrapping_vertexes_starting_at(next_index));
r
} else {
// must split at the point
result.reserve(vc + 1);
let mut r = Self::OutputPolyline::with_capacity(vc + 1, true);
let split = seg_split_at_point(
self.at(start_index),
self.at(next_index),
point,
pos_equal_eps,
);
result.add_vertex(split.split_vertex);
result.extend_vertexes(wrapping_vertexes_starting_at(next_index));
result.set_last(split.updated_start);
r.add_vertex(split.split_vertex);
r.extend_vertexes(wrapping_vertexes_starting_at(next_index));
r.set_last(split.updated_start);
r
}
}
};

Some(result)
}
Expand Down

0 comments on commit ad4d663

Please sign in to comment.