AudioProcessorGraph reallocating on the audio thread?

Hi all,

I noticed something odd in AudioProcessorGraph.

In GraphRenderSequence::perform(), the output buffer is resized on every call:

currentAudioOutputBuffer.setSize (jmax (1, buffer.getNumChannels()), numSamples);

AudioBuffer::setSize() uses avoidReallocating = false by default, so as far as I can tell, any change in numSamples reallocates the buffer on the audio thread. A sequence of variable-sized blocks like 64 - 4 - 60 - 64 would allocate 3 times.

Variable block sizes are explicitly allowed by the AudioProcessor doc so this seems unexpected…

Is there a reason it doesn’t pass avoidReallocating = true, since prepareToPlay() already allocates for the maximum block size?

Thanks!

I checked this in the current source and you’re right. GraphRenderSequence::perform() calls

currentAudioOutputBuffer.setSize (jmax (1, buffer.getNumChannels()), numSamples);

with the defaults (keepExistingContent = false, avoidReallocating = false). In AudioBuffer::setSize, whenever newNumSamples != size, the non-keep path only reuses the memory if avoidReallocating is true and allocatedBytes >= newTotalBytes. Otherwise it allocates a new HeapBlock. So 64 → 4 → 60 → 64 allocates on every size change. prepareBuffers() already sizes this buffer for the max block size, so passing avoidReallocating = true should be safe:

currentAudioOutputBuffer.setSize (jmax (1, buffer.getNumChannels()), numSamples, false, false, true);

It would also be worth checking whether the channel count can grow past what prepareBuffers() allocated. If it can, it would still allocate, but that’s a separate case.

You can confirm it by running the graph under RTSan or a malloc hook with varying block sizes.