perf(audio): reclaim callback owners through bounded return packets
This commit is contained in:
@@ -203,7 +203,8 @@ impl AudioBridge for Audio {
|
||||
fn begin_frame(&mut self) { /* Staging may already contain debugger steps. */
|
||||
}
|
||||
fn finish_frame(&mut self) {
|
||||
self.transport.publish(std::mem::take(&mut self.commands), false);
|
||||
let commands = std::mem::replace(&mut self.commands, self.transport.take_buffer());
|
||||
self.transport.publish(commands, false);
|
||||
self.frame_commands_admitted = 0;
|
||||
}
|
||||
fn abort_frame(&mut self) {
|
||||
@@ -221,7 +222,7 @@ impl AudioBridge for Audio {
|
||||
if success {
|
||||
// Publish the earlier frame prefix first so a callback stop cannot
|
||||
// be undone by an old play on resume. Do not replenish frame quota.
|
||||
let mut commands = std::mem::take(&mut self.commands);
|
||||
let mut commands = std::mem::replace(&mut self.commands, self.transport.take_buffer());
|
||||
commands.append(&mut self.lifecycle_commands);
|
||||
self.transport.publish(commands, true);
|
||||
} else {
|
||||
|
||||
@@ -14,6 +14,7 @@ struct Mailbox {
|
||||
paused: bool,
|
||||
frame: Option<Vec<AudioCommand>>,
|
||||
lifecycle: Option<Vec<AudioCommand>>,
|
||||
recycled: [Option<Vec<AudioCommand>>; 4],
|
||||
}
|
||||
|
||||
pub struct AudioDelivery {
|
||||
@@ -36,7 +37,31 @@ impl AudioTransport {
|
||||
}
|
||||
|
||||
pub fn consume_silently(&self) {
|
||||
let _ = self.try_take();
|
||||
if let Some(delivery) = self.try_take() {
|
||||
for batch in [delivery.frame, delivery.lifecycle].into_iter().flatten() {
|
||||
self.recycle_commands(batch);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Main/service lane only: drop command owners and retain bounded empty storage.
|
||||
pub fn recycle_commands(&self, mut commands: Vec<AudioCommand>) {
|
||||
commands.clear();
|
||||
let mut state = self.0.lock().expect("audio mailbox");
|
||||
if let Some(slot) = state.recycled.iter_mut().find(|slot| slot.is_none()) {
|
||||
*slot = Some(commands);
|
||||
}
|
||||
// Excess empty buffers are destroyed here, never in the data callback.
|
||||
}
|
||||
|
||||
pub(super) fn take_buffer(&self) -> Vec<AudioCommand> {
|
||||
self.0
|
||||
.lock()
|
||||
.expect("audio mailbox")
|
||||
.recycled
|
||||
.iter_mut()
|
||||
.find_map(Option::take)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn ready(&self) -> bool {
|
||||
@@ -84,3 +109,36 @@ mod tests {
|
||||
assert!(transport.try_take().is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod reuse_tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn service_recycles_storage_and_releases_sample_owners() {
|
||||
use prometeu_hal::{LoopMode, sample::Sample};
|
||||
let transport = AudioTransport::default();
|
||||
let sample = Arc::new(Sample::new(48000, vec![1]));
|
||||
let weak = Arc::downgrade(&sample);
|
||||
let mut batch = Vec::with_capacity(1024);
|
||||
let allocation = batch.as_ptr();
|
||||
batch.push(AudioCommand::Play {
|
||||
sample,
|
||||
voice_id: 0,
|
||||
volume: 255,
|
||||
pan: 0,
|
||||
pitch: 1.0,
|
||||
priority: 0,
|
||||
loop_mode: LoopMode::Off,
|
||||
});
|
||||
transport.recycle_commands(batch);
|
||||
assert!(weak.upgrade().is_none());
|
||||
let reused = transport.take_buffer();
|
||||
assert_eq!(reused.as_ptr(), allocation);
|
||||
assert_eq!(reused.capacity(), 1024);
|
||||
assert!(reused.is_empty());
|
||||
for _ in 0..8 {
|
||||
transport.recycle_commands(Vec::with_capacity(16));
|
||||
}
|
||||
assert_eq!(transport.0.lock().unwrap().recycled.iter().flatten().count(), 4);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user