Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix performance regression introduced by syncable_voice #11

Merged
merged 1 commit into from
Aug 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/synth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ pub struct Synth {
pub ext_in: i32,
}

// slice::rotate_left is inefficient for small arrays:
// https://github.com/rust-lang/rust/issues/89714
fn rotate3<T>([a, b, c]: [T; 3], i: usize) -> [T; 3] {
match i {
0 => [a, b, c],
1 => [b, c, a],
2 => [c, a, b],
_ => panic!("index out of bounds"),
}
}

impl Synth {
pub fn new(chip_model: ChipModel) -> Self {
Synth {
Expand All @@ -36,9 +47,7 @@ impl Synth {

pub fn syncable_voice(&self, i: usize) -> Syncable<&'_ Voice> {
let [a, b, c] = &self.voices;
let mut voices_ref = [a, b, c];
voices_ref.rotate_left(i);
let [main, sync_dest, sync_source] = voices_ref;
let [main, sync_dest, sync_source] = rotate3([a, b, c], i);
Syncable {
main,
sync_dest,
Expand All @@ -48,9 +57,7 @@ impl Synth {

pub fn syncable_voice_mut(&mut self, i: usize) -> Syncable<&'_ mut Voice> {
let [a, b, c] = &mut self.voices;
let mut voices_mut = [a, b, c];
voices_mut.rotate_left(i);
let [main, sync_dest, sync_source] = voices_mut;
let [main, sync_dest, sync_source] = rotate3([a, b, c], i);
Syncable {
main,
sync_dest,
Expand Down