Skip to content

Commit 3d5f585

Browse files
committed
Remove redundant zeroing
std::vector::resize already zeroes the new samples, so we don't need to use std::fill. See https://stackoverflow.com/a/73333009/10477326 However, we pass a second parameter to explicitly copy-init with zero and to not default-init. Benchmarks showed the performance difference between the two to be statistically insignificant, despite default-init having memset.
1 parent 5201136 commit 3d5f585

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

AudioFile.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -437,15 +437,9 @@ void AudioFile<T>::setAudioBufferSize (int numChannels, int numSamples)
437437
template <class T>
438438
void AudioFile<T>::setNumSamplesPerChannel (int numSamples)
439439
{
440-
int originalSize = getNumSamplesPerChannel();
441-
442440
for (int i = 0; i < getNumChannels();i++)
443441
{
444-
samples[i].resize (numSamples);
445-
446-
// set any new samples to zero
447-
if (numSamples > originalSize)
448-
std::fill (samples[i].begin() + originalSize, samples[i].end(), (T)0.);
442+
samples[i].resize (numSamples, static_cast<T>(0));
449443
}
450444
}
451445

@@ -460,13 +454,9 @@ void AudioFile<T>::setNumChannels (int numChannels)
460454

461455
// make sure any new channels are set to the right size
462456
// and filled with zeros
463-
if (numChannels > originalNumChannels)
457+
for (int i = originalNumChannels; i < numChannels; i++)
464458
{
465-
for (int i = originalNumChannels; i < numChannels; i++)
466-
{
467-
samples[i].resize (originalNumSamplesPerChannel);
468-
std::fill (samples[i].begin(), samples[i].end(), (T)0.);
469-
}
459+
samples[i].resize (originalNumSamplesPerChannel, static_cast<T>(0));
470460
}
471461
}
472462

0 commit comments

Comments
 (0)