AudioBuffer<double/float> performance question (Stack/Heap)

Let’s go back for a moment to AudioBuffer

Recently I had the issue with Hight CPU load in GarageBand (iOS): (SOLVED) iOS GarageBand - Help with High CPU load. Everything is fine with Cubasis - #2 by vtarasuk

As it turned out, the replacement of “AudioBuffer” to “std::array or c array” helped to drop the CPU from 300% cpu to 47-54%.

Today I decided to still experiment a little, since I want my application to use the dynamic buffer length and various channel sizes instead of fixed.

And here is a strange observation. When I allocate AudioBuffer on “static”. The CPU is still up to 300%.

Pseudo code (CPU: 300+ %):

class Voice {

public:
        void prepareToPlay(int channels, int samplesPerBlockExpected) {
            audioBuffer_.setSize(channels, samplesPerBlockExpected, true/false, true/false,...); // <<<-- init/reinit of our buffer is here
        }

        inline void processBlock(AudioBuffer<float> &outputBuffer) {
                auto *p = voiceBuffer_.getArrayOfWritePointers();
                p[0][n] = // some sound for the left channel
                p[1][n] = // some sound for the right channel
        }
private:
    AudioBuffer<float> voiceBuffer_{}; // <<<-- init of our buffer is here

}

And I decided to also try to just initialize the buffer right in the stream. Surprisingly, it worked and I got the same results as with fixed “c array”

Pseudo code (CPU: 47-54%):

class Voice {

public:
        void prepareToPlay(int channels, int samplesPerBlockExpected) {
             channels_ = channels;
             numOfSamples_ = samplesPerBlockExpected;
        }

        inline void processBlock(AudioBuffer<float> &outputBuffer) {
                AudioBuffer<float> voiceBuffer(channels, numOfSamples_); // <<<-- init of our buffer is here
                auto *p = voiceBuffer.getArrayOfWritePointers();
                p[0][n] = // some sound for the left channel
                p[1][n] = // some sound for the right channel
        }
private:
    int channels_;
    int numOfSamples_{};

}

According to this I have a few questions:

  1. Does it mean that the buffer in last example will be initialized on “stack” or it’s still on “heap” ?
  2. Do I need to deallocate it somehow? I don’t see any memory increases even after 10-15 min of work.
  3. Is this even a good practice to initialize AudioBuffer this way on Stack in realtime thread? (not sure how it work under the hood)

p.s. As I mentioned, this is only a problem with GarageBand. In cubase or standalone app, for example, both of realizations give me a low load (47-54%) By low of course I mean when all voices are on, all oscillators, unisons, modulations and oversampling 4x.

Thank you.