From 3e8b2b37597257dff57243f62c7949c93b44b20a Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Tue, 1 Mar 2022 19:52:56 +0100 Subject: [PATCH] Use AtomicRef instead of Mutexes for block smooth --- Cargo.lock | 1 + Cargo.toml | 1 + src/param/smoothing.rs | 26 +++++++++++++------------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5616d1e9..8bcaa308 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -534,6 +534,7 @@ version = "0.0.0" dependencies = [ "assert_no_alloc", "atomic_float", + "atomic_refcell", "cfg-if", "clap-sys", "crossbeam", diff --git a/Cargo.toml b/Cargo.toml index 9e59f8fb..d6819837 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ assert_process_allocs = ["assert_no_alloc"] nih_plug_derive = { path = "nih_plug_derive" } atomic_float = "0.1" +atomic_refcell = "0.1" cfg-if = "1.0" # For CLAP 0.18 clap-sys = { git = "https://github.com/glowcoil/clap-sys", rev = "3ef7048e1d3b426a7c6b02b5d3ae18f14874d4e5" } diff --git a/src/param/smoothing.rs b/src/param/smoothing.rs index 9b0938b9..9982d55d 100644 --- a/src/param/smoothing.rs +++ b/src/param/smoothing.rs @@ -1,5 +1,5 @@ use atomic_float::AtomicF32; -use parking_lot::{MappedMutexGuard, Mutex, MutexGuard}; +use atomic_refcell::{AtomicRef, AtomicRefCell, AtomicRefMut}; use std::sync::atomic::{AtomicI32, Ordering}; use crate::buffer::Block; @@ -43,7 +43,7 @@ pub struct Smoother { /// A dense buffer containing smoothed values for an entire block of audio. Useful when using /// [crate::Buffer::iter_blocks()] to process small blocks of audio multiple times. - block_values: Mutex>, + block_values: AtomicRefCell>, } impl Default for Smoother { @@ -55,7 +55,7 @@ impl Default for Smoother { current: AtomicF32::new(0.0), target: Default::default(), - block_values: Mutex::new(Vec::new()), + block_values: AtomicRefCell::new(Vec::new()), } } } @@ -85,7 +85,7 @@ impl Smoother { /// [crate::Buffer::iter_blocks()]. pub fn initialize_block_smoother(&mut self, max_block_size: usize) { self.block_values - .lock() + .borrow_mut() .resize_with(max_block_size, || T::default()); } } @@ -142,8 +142,8 @@ impl Smoother { /// /// Returns a `None` value if the block length exceed's the allocated capacity. #[inline] - pub fn next_block(&self, block: &Block) -> Option> { - let mut block_values = self.block_values.lock(); + pub fn next_block(&self, block: &Block) -> Option> { + let mut block_values = self.block_values.borrow_mut(); if block_values.len() < block.len() { return None; } @@ -154,7 +154,7 @@ impl Smoother { // In that case we wouldn't need to do anything ehre. (&mut block_values[..block.len()]).fill_with(|| self.next()); - Some(MutexGuard::map(block_values, |values| { + Some(AtomicRefMut::map(block_values, |values| { &mut values[..block.len()] })) } @@ -239,16 +239,16 @@ impl Smoother { /// /// Returns a `None` value if the block length exceed's the allocated capacity. #[inline] - pub fn next_block(&self, block_len: usize) -> Option> { - let mut block_values = self.block_values.lock(); - if block_values.len() < block_len { + pub fn next_block(&self, block: &Block) -> Option> { + let mut block_values = self.block_values.borrow_mut(); + if block_values.len() < block.len() { return None; } - (&mut block_values[..block_len]).fill_with(|| self.next()); + (&mut block_values[..block.len()]).fill_with(|| self.next()); - Some(MutexGuard::map(block_values, |values| { - &mut values[..block_len] + Some(AtomicRefMut::map(block_values, |values| { + &mut values[..block.len()] })) }