From 79fca548b167769782f2a41faa1ef9af24b48719 Mon Sep 17 00:00:00 2001 From: "J.A. de Jong - Redu-Sone B.V., ASCEE V.O.F." Date: Sat, 2 Nov 2024 00:35:07 +0100 Subject: [PATCH] removed one loop vector allocation. Implemented a bit smarter --- src/rt/simpleclip.rs | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/rt/simpleclip.rs b/src/rt/simpleclip.rs index dd8195e..f69b929 100644 --- a/src/rt/simpleclip.rs +++ b/src/rt/simpleclip.rs @@ -44,10 +44,14 @@ impl SimpleClipDetector { rayon::spawn( move || { let mut streammeta: Option> = None; + let mut mins = vec![]; + let mut maxs = vec![]; loop { if let Ok(msg) = rx.recv_timeout(Duration::from_millis(1500)) { match msg { InStreamMsg::StreamStarted(meta) => { + mins.resize(meta.nchannels(), 0.); + maxs.resize(meta.nchannels(), 0.); streammeta = Some(meta); } InStreamMsg::InStreamData(dat) => { @@ -55,37 +59,39 @@ impl SimpleClipDetector { .as_ref() .expect("If we are here, stream metadata should be available"); let flt = dat.getFloatData(); - let maxs = flt - .columns() + // Update channel maximum values + flt.columns() .into_iter() - .map(|col| max(col)) - .collect::>(); - let mins = flt - .columns() + .zip(maxs.iter_mut()) + .for_each(|(coli, maxi)| *maxi = max(coli)); + // Update channel minimum values + flt.columns() .into_iter() - .map(|col| min(col)) - .collect::>(); + .zip(mins.iter_mut()) + .for_each(|(coli, mini)| *mini = min(coli)); - maxs.into_iter().zip(mins).zip(&meta.channelInfo).for_each( - |((max, min), ch)| { + // Compare minima and maxima against clip limits + maxs.iter() + .zip(mins.iter()) + .zip(&meta.channelInfo) + .for_each(|((max, min), ch)| { let min_for_clip = CLIP_REL_LIMIT * ch.range.0; let max_for_clip = CLIP_REL_LIMIT * ch.range.1; - if max >= max_for_clip { + if *max >= max_for_clip { clipstate.store(true, Relaxed); // We do not have to do anything anymore. The signal // has clipped so we do not have to check any new // blocks anymore. return; } - if min <= min_for_clip { + if *min <= min_for_clip { clipstate.store(true, Relaxed); // We do not have to do anything anymore. The signal // has clipped so we do not have to check any new // blocks anymore. return; } - }, - ); + }); } // Ignore other stream messages _ => {}