Browse Source

Clean up some sub-optimal slice handling

Clippy noticed that update_from looked like a manual copy_from_slice,
which alerted me to sum_into and mul_into also being suboptimal. With
the new changes, the generated code is much cleaner: it lacks an extra
"unnecessary" branch and is about 30% shorter (for those methods).

I also think the code is a bit easier to read :)
Corey Richardson 8 years ago
parent
commit
5f1e73ab5b
1 changed files with 24 additions and 21 deletions
  1. 24 21
      src/synth/mod.rs

+ 24 - 21
src/synth/mod.rs

@@ -80,9 +80,8 @@ impl SampleBuffer {
         self.rate = other.rate;
         match self.rate {
             Rate::Sample => {
-                for i in 0..cmp::min(self.len(), other.len()) {
-                    self.samples[i] = other.samples[i];
-                }
+                let len = cmp::min(self.len(), other.len());
+                self.samples[..len].clone_from_slice(&other.samples[..len]);
             },
             Rate::Control => {
                 self.samples[0] = other.samples[0];
@@ -93,16 +92,18 @@ impl SampleBuffer {
     pub fn sum_into(&mut self, other: &SampleBuffer) {
         match self.rate {
             Rate::Sample => {
-                let bound = match other.rate {
-                    Rate::Sample => cmp::min(self.len(), other.len()),
-                    Rate::Control => self.len(),
+                match other.rate {
+                    Rate::Sample => {
+                        for (elt, oelt) in self.samples.iter_mut().zip(other.samples.iter()) {
+                            *elt += *oelt;
+                        }
+                    }
+                    Rate::Control => {
+                        for elt in &mut self.samples {
+                            *elt += other.samples[0];
+                        }
+                    }
                 };
-                for i in 0..bound {
-                    self.samples[i] += match other.rate {
-                        Rate::Sample => other.samples[i],
-                        Rate::Control => other.samples[0],
-                    };
-                }
             },
             Rate::Control => {
                 self.samples[0] += other.samples[0];
@@ -113,16 +114,18 @@ impl SampleBuffer {
     pub fn mul_into(&mut self, other: &SampleBuffer) {
         match self.rate {
             Rate::Sample => {
-                let bound = match other.rate {
-                    Rate::Sample => cmp::min(self.len(), other.len()),
-                    Rate::Control => self.len(),
+                match other.rate {
+                    Rate::Sample => {
+                        for (elt, oelt) in self.samples.iter_mut().zip(other.samples.iter()) {
+                            *elt *= *oelt;
+                        }
+                    }
+                    Rate::Control => {
+                        for elt in &mut self.samples {
+                            *elt *= other.samples[0];
+                        }
+                    }
                 };
-                for i in 0..bound {
-                    self.samples[i] *= match other.rate {
-                        Rate::Sample => other.samples[i],
-                        Rate::Control => other.samples[0],
-                    };
-                }
             },
             Rate::Control => {
                 self.samples[0] *= other.samples[0];