Avoid memory allocation when precomputed segments lengths is provided. (#2637)
* Avoid allocating a Vec when precomputed segments lengths is provided. * improved using option type. * we don't even need option type as Vec::new() doesn't allocate. * clean up
This commit is contained in:
parent
c9984a8f1a
commit
0022680336
|
|
@ -150,19 +150,24 @@ enum BezPathTValue {
|
||||||
|
|
||||||
/// Convert a [BezPathTValue] to a parametric `(segment_index, t)` tuple.
|
/// Convert a [BezPathTValue] to a parametric `(segment_index, t)` tuple.
|
||||||
/// - Asserts that `t` values contained within the `SubpathTValue` argument lie in the range [0, 1].
|
/// - Asserts that `t` values contained within the `SubpathTValue` argument lie in the range [0, 1].
|
||||||
fn bezpath_t_value_to_parametric(bezpath: &kurbo::BezPath, t: BezPathTValue, segments_length: Option<&[f64]>) -> (usize, f64) {
|
fn bezpath_t_value_to_parametric(bezpath: &kurbo::BezPath, t: BezPathTValue, precomputed_segments_length: Option<&[f64]>) -> (usize, f64) {
|
||||||
let segment_count = bezpath.segments().count();
|
let segment_count = bezpath.segments().count();
|
||||||
assert!(segment_count >= 1);
|
assert!(segment_count >= 1);
|
||||||
|
|
||||||
match t {
|
match t {
|
||||||
BezPathTValue::GlobalEuclidean(t) => {
|
BezPathTValue::GlobalEuclidean(t) => {
|
||||||
let lengths = segments_length
|
let computed_segments_length;
|
||||||
.map(|segments_length| segments_length.to_vec())
|
|
||||||
.unwrap_or(bezpath.segments().map(|segment| segment.perimeter(PERIMETER_ACCURACY)).collect());
|
|
||||||
|
|
||||||
let total_length = lengths.iter().sum();
|
let segments_length = if let Some(segments_length) = precomputed_segments_length {
|
||||||
|
segments_length
|
||||||
|
} else {
|
||||||
|
computed_segments_length = bezpath.segments().map(|segment| segment.perimeter(PERIMETER_ACCURACY)).collect::<Vec<f64>>();
|
||||||
|
computed_segments_length.as_slice()
|
||||||
|
};
|
||||||
|
|
||||||
global_euclidean_to_local_euclidean(bezpath, t, lengths.as_slice(), total_length)
|
let total_length = segments_length.iter().sum();
|
||||||
|
|
||||||
|
global_euclidean_to_local_euclidean(bezpath, t, segments_length, total_length)
|
||||||
}
|
}
|
||||||
BezPathTValue::GlobalParametric(global_t) => {
|
BezPathTValue::GlobalParametric(global_t) => {
|
||||||
assert!((0.0..=1.).contains(&global_t));
|
assert!((0.0..=1.).contains(&global_t));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue