Skip to content

Commit

Permalink
Optimize rendering without threadpool (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
MyBlackMIDIScore authored Aug 18, 2024
1 parent 0892efe commit 4124259
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
39 changes: 16 additions & 23 deletions core/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,22 +272,6 @@ impl VoiceChannel {
}

fn push_key_events_and_render(&mut self, out: &mut [f32]) {
fn render_for_key(
key: &mut Key,
len: usize,
control: &VoiceControlData,
params: &VoiceChannelParams,
) {
for e in key.event_cache.drain(..) {
key.data
.send_event(e, control, &params.channel_sf, params.layers);
}

prepapre_cache_vec(&mut key.audio_cache, len, 0.0);

key.data.render_to(&mut key.audio_cache);
}

self.params
.channel_sf
.change_program(self.control_event_data.bank, self.control_event_data.preset);
Expand All @@ -301,7 +285,13 @@ impl VoiceChannel {
let control_data = &self.voice_control_data;
pool.install(|| {
key_voices.par_iter_mut().for_each(move |key| {
render_for_key(key, len, control_data, params);
for e in key.event_cache.drain(..) {
key.data
.send_event(e, control_data, &params.channel_sf, params.layers);
}

prepapre_cache_vec(&mut key.audio_cache, len, 0.0);
key.data.render_to(&mut key.audio_cache);
});
});

Expand All @@ -310,14 +300,17 @@ impl VoiceChannel {
}
}
None => {
let len = out.len();

for key in self.key_voices.iter_mut() {
render_for_key(key, len, &self.voice_control_data, &self.params);
}
for e in key.event_cache.drain(..) {
key.data.send_event(
e,
&self.voice_control_data,
&self.params.channel_sf,
self.params.layers,
);
}

for key in self.key_voices.iter() {
sum_simd(&key.audio_cache, out);
key.data.render_to(out);
}
}
}
Expand Down
13 changes: 6 additions & 7 deletions core/src/channel_group/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use crate::{
channel::{ChannelAudioEvent, ChannelEvent, VoiceChannel},
helpers::sum_simd,
helpers::{prepapre_cache_vec, sum_simd},
AudioPipe, AudioStreamParams,
};

Expand Down Expand Up @@ -36,7 +36,7 @@ impl ChannelGroup {
let mut sample_cache_vecs = Vec::new();

// Thread pool for individual channels to split between keys
let channel_pool = match config.parallelism.channel {
let channel_pool = match config.parallelism.key {
ThreadCount::None => None,
ThreadCount::Auto => Some(Arc::new(rayon::ThreadPoolBuilder::new().build().unwrap())),
ThreadCount::Manual(threads) => Some(Arc::new(
Expand All @@ -48,7 +48,7 @@ impl ChannelGroup {
};

// Thread pool for splitting channels between threads
let group_pool = match config.parallelism.key {
let group_pool = match config.parallelism.channel {
ThreadCount::None => None,
ThreadCount::Auto => Some(rayon::ThreadPoolBuilder::new().build().unwrap()),
ThreadCount::Manual(threads) => Some(
Expand Down Expand Up @@ -149,20 +149,20 @@ impl ChannelGroup {

match self.thread_pool.as_ref() {
Some(pool) => {
let len = buffer.len();
let channels = &mut self.channels;
let sample_cache_vecs = &mut self.sample_cache_vecs;
pool.install(move || {
channels
.par_iter_mut()
.zip(sample_cache_vecs.par_iter_mut())
.for_each(|(channel, samples)| {
samples.resize(buffer.len(), 0.0);
prepapre_cache_vec(samples, len, 0.0);
channel.read_samples(samples.as_mut_slice());
});

for vec in sample_cache_vecs.iter_mut() {
sum_simd(vec, buffer);
vec.clear();
}
});
}
Expand All @@ -174,13 +174,12 @@ impl ChannelGroup {
.iter_mut()
.zip(self.sample_cache_vecs.iter_mut())
{
samples.resize(len, 0.0);
prepapre_cache_vec(samples, len, 0.0);
channel.read_samples(samples.as_mut_slice());
}

for vec in self.sample_cache_vecs.iter_mut() {
sum_simd(vec, buffer);
vec.clear();
}
}
}
Expand Down

0 comments on commit 4124259

Please sign in to comment.