Skip to content

Commit 649389a

Browse files
authored
Merge pull request #11 from lukaslihotzki/perf
Fix performance regression introduced by syncable_voice
2 parents c721584 + 24672dc commit 649389a

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/synth.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ pub struct Synth {
2424
pub ext_in: i32,
2525
}
2626

27+
// slice::rotate_left is inefficient for small arrays:
28+
// https://github.com/rust-lang/rust/issues/89714
29+
fn rotate3<T>([a, b, c]: [T; 3], i: usize) -> [T; 3] {
30+
match i {
31+
0 => [a, b, c],
32+
1 => [b, c, a],
33+
2 => [c, a, b],
34+
_ => panic!("index out of bounds"),
35+
}
36+
}
37+
2738
impl Synth {
2839
pub fn new(chip_model: ChipModel) -> Self {
2940
Synth {
@@ -36,9 +47,7 @@ impl Synth {
3647

3748
pub fn syncable_voice(&self, i: usize) -> Syncable<&'_ Voice> {
3849
let [a, b, c] = &self.voices;
39-
let mut voices_ref = [a, b, c];
40-
voices_ref.rotate_left(i);
41-
let [main, sync_dest, sync_source] = voices_ref;
50+
let [main, sync_dest, sync_source] = rotate3([a, b, c], i);
4251
Syncable {
4352
main,
4453
sync_dest,
@@ -48,9 +57,7 @@ impl Synth {
4857

4958
pub fn syncable_voice_mut(&mut self, i: usize) -> Syncable<&'_ mut Voice> {
5059
let [a, b, c] = &mut self.voices;
51-
let mut voices_mut = [a, b, c];
52-
voices_mut.rotate_left(i);
53-
let [main, sync_dest, sync_source] = voices_mut;
60+
let [main, sync_dest, sync_source] = rotate3([a, b, c], i);
5461
Syncable {
5562
main,
5663
sync_dest,

0 commit comments

Comments
 (0)