Skip to content

Commit 57692b8

Browse files
committed
perf: optimize sample conversion based on decoder output format
Avoid unnecessary sample conversions in MP3 and WAV decoders by using cfg attributes to conditionally perform format conversion only when needed.
1 parent d1dd377 commit 57692b8

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/decoder/flac.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ where
123123
let bits = self.bits_per_sample;
124124
let real_val = match bits {
125125
8 => (raw_val as i8).to_sample(),
126-
16 => (raw_val as i16).to_sample(),
126+
16 => {
127+
let raw_val = raw_val as i16;
128+
#[cfg(not(feature = "integer-decoder"))] // perf
129+
let raw_val = raw_val.to_sample();
130+
raw_val
131+
}
127132
24 => I24::new(raw_val).unwrap_or(Sample::EQUILIBRIUM).to_sample(),
128133
32 => raw_val.to_sample(),
129134
_ => {

src/decoder/mp3.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ where
110110
let v = self.current_span.data[self.current_span_offset];
111111
self.current_span_offset += 1;
112112

113-
Some(v.to_sample())
113+
#[cfg(not(feature = "integer-decoder"))] // perf
114+
let v = v.to_sample();
115+
Some(v)
114116
}
115117
}
116118

src/decoder/wav.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ where
8686
(SampleFormat::Float, bits) => {
8787
if bits == 32 {
8888
let next_f32: Option<Result<f32, _>> = self.reader.samples().next();
89-
next_f32.and_then(|value| value.ok().map(|value| value.to_sample()))
89+
next_f32.and_then(|value| {
90+
value.ok().map(|value| {
91+
#[cfg(feature = "integer-decoder")] // perf
92+
let value = value.to_sample();
93+
value
94+
})
95+
})
9096
} else {
9197
// > 32 bits we cannot handle, so we'll just return equilibrium
9298
// and let the iterator continue
@@ -100,7 +106,13 @@ where
100106
}
101107
(SampleFormat::Int, 16) => {
102108
let next_i16: Option<Result<i16, _>> = self.reader.samples().next();
103-
next_i16.and_then(|value| value.ok().map(|value| value.to_sample()))
109+
next_i16.and_then(|value| {
110+
value.ok().map(|value| {
111+
#[cfg(not(feature = "integer-decoder"))] // perf
112+
let value = value.to_sample();
113+
value
114+
})
115+
})
104116
}
105117
(SampleFormat::Int, 24) => {
106118
let next_i24_in_i32: Option<Result<i32, _>> = self.reader.samples().next();

0 commit comments

Comments
 (0)