From 2486368d08db4cba8c049f7a0a753bfb9c90738d Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 6 Feb 2022 03:38:59 +0100 Subject: [PATCH] Use AtomicF32 for the smoothing This should at least not have a big performance impact since we can use relaxed memory ordering here. AtomicCell always uses acquire/release ordering. --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/param/smoothing.rs | 22 +++++++++++----------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 40afc0f6..8d2374d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,6 +41,12 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55ca83137a482d61d916ceb1eba52a684f98004f18e0cafea230fe5579c178a3" +[[package]] +name = "atomic_float" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62af46d040ba9df09edc6528dae9d8e49f5f3e82f55b7d2ec31a733c38dbc49d" + [[package]] name = "atomic_refcell" version = "0.1.8" @@ -490,6 +496,7 @@ name = "nih_plug" version = "0.0.0" dependencies = [ "assert_no_alloc", + "atomic_float", "cfg-if 1.0.0", "crossbeam", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index 5e76a615..cec83b29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ members = [ [dependencies] nih_plug_derive = { path = "nih_plug_derive" } +atomic_float = "0.1" cfg-if = "1.0" crossbeam = "0.8" lazy_static = "1.4" diff --git a/src/param/smoothing.rs b/src/param/smoothing.rs index 83267252..c885e7fd 100644 --- a/src/param/smoothing.rs +++ b/src/param/smoothing.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crossbeam::atomic::AtomicCell; +use atomic_float::AtomicF32; use std::sync::atomic::{AtomicU32, Ordering}; /// Controls if and how parameters gets smoothed. @@ -47,7 +47,7 @@ pub struct Smoother { /// uniform. step_size: f32, /// The value for the current sample. Always stored as floating point for obvious reasons. - current: AtomicCell, + current: AtomicF32, /// The value we're smoothing towards target: T, } @@ -58,7 +58,7 @@ impl Default for Smoother { style: SmoothingStyle::None, steps_left: AtomicU32::new(0), step_size: Default::default(), - current: AtomicCell::new(0.0), + current: AtomicF32::new(0.0), target: Default::default(), } } @@ -91,7 +91,7 @@ impl Smoother { /// Reset the smoother the specified value. pub fn reset(&mut self, value: f32) { self.target = value; - self.current.store(value); + self.current.store(value, Ordering::Relaxed); self.steps_left.store(0, Ordering::Relaxed); } @@ -107,7 +107,7 @@ impl Smoother { }; self.steps_left.store(steps_left, Ordering::Relaxed); - let current = self.current.load(); + let current = self.current.load(Ordering::Relaxed); self.step_size = match self.style { SmoothingStyle::None => 0.0, SmoothingStyle::Linear(_) => (self.target - current) / steps_left as f32, @@ -124,7 +124,7 @@ impl Smoother { #[allow(clippy::should_implement_trait)] pub fn next(&self) -> f32 { if self.steps_left.load(Ordering::Relaxed) > 1 { - let current = self.current.load(); + let current = self.current.load(Ordering::Relaxed); // The number of steps usually won't fit exactly, so make sure we don't do weird things // with overshoots or undershoots @@ -138,7 +138,7 @@ impl Smoother { SmoothingStyle::Logarithmic(_) => current * self.step_size, } }; - self.current.store(new); + self.current.store(new, Ordering::Relaxed); new } else { @@ -151,7 +151,7 @@ impl Smoother { /// Reset the smoother the specified value. pub fn reset(&mut self, value: i32) { self.target = value; - self.current.store(value as f32); + self.current.store(value as f32, Ordering::Relaxed); self.steps_left.store(0, Ordering::Relaxed); } @@ -166,7 +166,7 @@ impl Smoother { }; self.steps_left.store(steps_left, Ordering::Relaxed); - let current = self.current.load(); + let current = self.current.load(Ordering::Relaxed); self.step_size = match self.style { SmoothingStyle::None => 0.0, SmoothingStyle::Linear(_) => (self.target as f32 - current) / steps_left as f32, @@ -180,7 +180,7 @@ impl Smoother { #[allow(clippy::should_implement_trait)] pub fn next(&mut self) -> i32 { if self.steps_left.load(Ordering::Relaxed) > 1 { - let current = self.current.load(); + let current = self.current.load(Ordering::Relaxed); // The number of steps usually won't fit exactly, so make sure we don't do weird things // with overshoots or undershoots @@ -194,7 +194,7 @@ impl Smoother { SmoothingStyle::Logarithmic(_) => current * self.step_size, } }; - self.current.store(new); + self.current.store(new, Ordering::Relaxed); new.round() as i32 } else {