Skip to content

Commit 910e238

Browse files
authored
Merge pull request #363 from b-ma/perf/param-array-vec
Perf - use `ArrayVec` instead of `Vec` for internal `AudioParam` buffer
2 parents ed2e871 + 4a5e4dc commit 910e238

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/param.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
use std::any::Any;
33
use std::slice::{Iter, IterMut};
44
use std::sync::atomic::{AtomicBool, Ordering};
5-
use std::sync::Arc;
5+
use std::sync::{Arc, OnceLock};
6+
7+
use arrayvec::ArrayVec;
68

79
use crate::context::AudioContextRegistration;
810
use crate::node::{
@@ -11,8 +13,6 @@ use crate::node::{
1113
use crate::render::{AudioParamValues, AudioProcessor, AudioRenderQuantum, RenderScope};
1214
use crate::{AtomicF32, RENDER_QUANTUM_SIZE};
1315

14-
use std::sync::OnceLock;
15-
1616
/// For SetTargetAtTime event, that theoretically cannot end, if the diff between
1717
/// the current value and the target is below this threshold, the value is set
1818
/// to target value and the event is considered ended.
@@ -620,7 +620,7 @@ pub(crate) struct AudioParamProcessor {
620620
shared_parts: Arc<AudioParamShared>,
621621
event_timeline: AudioParamEventTimeline,
622622
last_event: Option<AudioParamEvent>,
623-
buffer: Vec<f32>,
623+
buffer: ArrayVec<f32, RENDER_QUANTUM_SIZE>,
624624
}
625625

626626
impl AudioProcessor for AudioParamProcessor {
@@ -670,11 +670,11 @@ impl AudioProcessor for AudioParamProcessor {
670670
}
671671

672672
impl AudioParamProcessor {
673-
// warning: tick in called directly in the unit tests so everything important
674-
// for the tests should be done here
673+
// Warning: compute_intrinsic_values in called directly in the unit tests so
674+
// everything important for the tests should be done here
675+
// The returned value is only used in tests
675676
fn compute_intrinsic_values(&mut self, block_time: f64, dt: f64, count: usize) -> &[f32] {
676677
self.compute_buffer(block_time, dt, count);
677-
678678
self.buffer.as_slice()
679679
}
680680

@@ -1088,7 +1088,14 @@ impl AudioParamProcessor {
10881088
match some_event {
10891089
None => {
10901090
if is_a_rate {
1091-
self.buffer.resize(count, self.intrinsic_value);
1091+
// @note - we use `count` rather then `buffer.remaining_capacity`
1092+
// to correctly unit tests where `count` is lower than RENDER_QUANTUM_SIZE
1093+
//
1094+
// @todo(perf) - consider using try_extend_from_slice
1095+
// which internally uses ptr::copy_nonoverlapping
1096+
for _ in self.buffer.len()..count {
1097+
self.buffer.push(self.intrinsic_value);
1098+
}
10921099
}
10931100
break;
10941101
}
@@ -1614,7 +1621,7 @@ pub(crate) fn audio_param_pair(
16141621
shared_parts,
16151622
event_timeline: AudioParamEventTimeline::new(),
16161623
last_event: None,
1617-
buffer: Vec::with_capacity(RENDER_QUANTUM_SIZE),
1624+
buffer: ArrayVec::new(),
16181625
};
16191626

16201627
(param, processor)

0 commit comments

Comments
 (0)