Use buffer pool
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
/// Pool of reusable audio buffers for recursive group rendering
|
||||
///
|
||||
/// This pool allows groups to acquire temporary buffers for submixing
|
||||
@@ -6,6 +8,11 @@ pub struct BufferPool {
|
||||
buffers: Vec<Vec<f32>>,
|
||||
available: Vec<usize>,
|
||||
buffer_size: usize,
|
||||
/// Tracks the number of times a buffer had to be allocated (not reused)
|
||||
/// This should be zero during steady-state playback
|
||||
total_allocations: AtomicUsize,
|
||||
/// Peak number of buffers simultaneously in use
|
||||
peak_usage: AtomicUsize,
|
||||
}
|
||||
|
||||
impl BufferPool {
|
||||
@@ -28,6 +35,8 @@ impl BufferPool {
|
||||
buffers,
|
||||
available,
|
||||
buffer_size,
|
||||
total_allocations: AtomicUsize::new(0),
|
||||
peak_usage: AtomicUsize::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +45,13 @@ impl BufferPool {
|
||||
/// Returns a zeroed buffer ready for use. If no buffers are available,
|
||||
/// allocates a new one (though this should be avoided in the audio thread).
|
||||
pub fn acquire(&mut self) -> Vec<f32> {
|
||||
// Track peak usage
|
||||
let current_in_use = self.buffers.len() - self.available.len();
|
||||
let peak = self.peak_usage.load(Ordering::Relaxed);
|
||||
if current_in_use > peak {
|
||||
self.peak_usage.store(current_in_use, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
if let Some(idx) = self.available.pop() {
|
||||
// Reuse an existing buffer
|
||||
let mut buf = std::mem::take(&mut self.buffers[idx]);
|
||||
@@ -44,6 +60,7 @@ impl BufferPool {
|
||||
} else {
|
||||
// No buffers available, allocate a new one
|
||||
// This should be rare if the pool is sized correctly
|
||||
self.total_allocations.fetch_add(1, Ordering::Relaxed);
|
||||
vec![0.0; self.buffer_size]
|
||||
}
|
||||
}
|
||||
@@ -76,6 +93,52 @@ impl BufferPool {
|
||||
pub fn total_count(&self) -> usize {
|
||||
self.buffers.len()
|
||||
}
|
||||
|
||||
/// Get the total number of allocations that occurred (excluding pre-allocated buffers)
|
||||
///
|
||||
/// This should be zero during steady-state playback. If non-zero, the pool
|
||||
/// should be resized to avoid allocations in the audio thread.
|
||||
pub fn allocation_count(&self) -> usize {
|
||||
self.total_allocations.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Get the peak number of buffers simultaneously in use
|
||||
///
|
||||
/// Use this to determine the optimal initial_capacity for your workload.
|
||||
pub fn peak_usage(&self) -> usize {
|
||||
self.peak_usage.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Reset allocation statistics
|
||||
///
|
||||
/// Useful for benchmarking steady-state performance after warmup.
|
||||
pub fn reset_stats(&mut self) {
|
||||
self.total_allocations.store(0, Ordering::Relaxed);
|
||||
self.peak_usage.store(0, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Get comprehensive pool statistics
|
||||
pub fn stats(&self) -> BufferPoolStats {
|
||||
BufferPoolStats {
|
||||
total_buffers: self.total_count(),
|
||||
available_buffers: self.available_count(),
|
||||
in_use_buffers: self.total_count() - self.available_count(),
|
||||
peak_usage: self.peak_usage(),
|
||||
total_allocations: self.allocation_count(),
|
||||
buffer_size: self.buffer_size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Statistics about buffer pool usage
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct BufferPoolStats {
|
||||
pub total_buffers: usize,
|
||||
pub available_buffers: usize,
|
||||
pub in_use_buffers: usize,
|
||||
pub peak_usage: usize,
|
||||
pub total_allocations: usize,
|
||||
pub buffer_size: usize,
|
||||
}
|
||||
|
||||
impl Default for BufferPool {
|
||||
|
||||
@@ -397,6 +397,11 @@ impl Engine {
|
||||
// Add a pre-loaded MIDI clip to the track
|
||||
let _ = self.project.add_midi_clip(track_id, clip);
|
||||
}
|
||||
Command::RequestBufferPoolStats => {
|
||||
// Send buffer pool statistics back to UI
|
||||
let stats = self.buffer_pool.stats();
|
||||
let _ = self.event_tx.push(AudioEvent::BufferPoolStats(stats));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -549,4 +554,10 @@ impl EngineController {
|
||||
pub fn add_loaded_midi_clip(&mut self, track_id: TrackId, clip: MidiClip) {
|
||||
let _ = self.command_tx.push(Command::AddLoadedMidiClip(track_id, clip));
|
||||
}
|
||||
|
||||
/// Request buffer pool statistics
|
||||
/// The statistics will be sent via an AudioEvent::BufferPoolStats event
|
||||
pub fn request_buffer_pool_stats(&mut self) {
|
||||
let _ = self.command_tx.push(Command::RequestBufferPoolStats);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user