Skip to content

Commit 075276c

Browse files
committed
Prevent needless clone in offline rendering + in the online happy path
1 parent 7632594 commit 075276c

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/render/graph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ impl Graph {
354354
}
355355

356356
/// Render a single audio quantum by traversing the node list
357-
pub fn render(&mut self, scope: &RenderScope) -> AudioRenderQuantum {
357+
pub fn render(&mut self, scope: &RenderScope) -> &AudioRenderQuantum {
358358
// if the audio graph was changed, determine the new ordering
359359
if self.ordered.is_empty() {
360360
self.order_nodes();
@@ -460,12 +460,12 @@ impl Graph {
460460
}
461461

462462
// Return the output buffer of destination node
463-
self.nodes
463+
&self
464+
.nodes
464465
.get_mut(&AudioNodeId(0))
465466
.unwrap()
466467
.get_mut()
467468
.outputs[0]
468-
.clone()
469469
}
470470
}
471471

src/render/thread.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Communicates with the control thread and ships audio samples to the hardware
22
3+
use std::borrow::Cow;
34
use std::cell::Cell;
45
use std::sync::atomic::{AtomicU64, Ordering};
56
use std::sync::Arc;
@@ -257,12 +258,14 @@ impl RenderThread {
257258
node_id: Cell::new(AudioNodeId(0)), // placeholder value
258259
};
259260

260-
// render audio graph
261-
let mut rendered = self.graph.as_mut().unwrap().render(&scope);
261+
// render audio graph, and use a Cow in case we need to mutate/store the value later
262+
let mut rendered = Cow::Borrowed(self.graph.as_mut().unwrap().render(&scope));
262263

263264
// online AudioContext allows channel count to be less than no of hardware channels
264265
if rendered.number_of_channels() != self.number_of_channels {
265-
rendered.mix(self.number_of_channels, ChannelInterpretation::Discrete);
266+
rendered
267+
.to_mut()
268+
.mix(self.number_of_channels, ChannelInterpretation::Discrete);
266269
}
267270

268271
// copy rendered audio into output slice
@@ -279,7 +282,7 @@ impl RenderThread {
279282
// this is the last chunk, and it contained less than RENDER_QUANTUM_SIZE samples
280283
let channel_offset = data.len() / self.number_of_channels;
281284
debug_assert!(channel_offset < RENDER_QUANTUM_SIZE);
282-
self.buffer_offset = Some((channel_offset, rendered));
285+
self.buffer_offset = Some((channel_offset, rendered.into_owned()));
283286
}
284287

285288
// handle addition/removal of nodes/edges

0 commit comments

Comments
 (0)