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:
Priyanshu 2025-05-11 20:51:41 +05:30 committed by GitHub
parent c9984a8f1a
commit 0022680336
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 6 deletions

View File

@ -150,19 +150,24 @@ enum BezPathTValue {
/// 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].
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();
assert!(segment_count >= 1);
match t {
BezPathTValue::GlobalEuclidean(t) => {
let lengths = segments_length
.map(|segments_length| segments_length.to_vec())
.unwrap_or(bezpath.segments().map(|segment| segment.perimeter(PERIMETER_ACCURACY)).collect());
let computed_segments_length;
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) => {
assert!((0.0..=1.).contains(&global_t));