From eb97b628a9c0c5c63de5a3d00aa75607c136c8cf Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Sat, 13 May 2023 11:03:07 -0500 Subject: [PATCH] fix performance issue in `PatternView`: Previously converting from a `PatternView` to an array reference required an intermediary step, and the creation of a new array on the stack. The new implementation can pass a reference to the underyling data directly, without that intermediary allocation. --- src/common/cpp_essentials/pattern.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/common/cpp_essentials/pattern.rs b/src/common/cpp_essentials/pattern.rs index d222b2c..2ec70bc 100644 --- a/src/common/cpp_essentials/pattern.rs +++ b/src/common/cpp_essentials/pattern.rs @@ -348,6 +348,16 @@ impl<'a, const LEN: usize> From<&PatternView<'a>> for [PatternType; LEN] { } } +impl<'a> From<&PatternView<'a>> for &'a [PatternType] { + fn from(value: &PatternView<'a>) -> Self { + if value.data.0.len() == value.count { + &value.data.0 + } else { + &value.data.0[value.current + value.start..=value.current + value.start + value.count] + } + } +} + /** * @brief The BarAndSpace struct is a simple 2 element data structure to hold information about bar(s) and space(s). * @@ -476,8 +486,8 @@ pub fn IsPattern(&v_src); + // let v_src: [PatternType; LEN] = view.into(); + let widths = BarAndSpaceSum::(view.into()); let sums = pattern.sums(); let modSize: BarAndSpace = BarAndSpace { bar: widths[0] / sums[0] as f64,