JUCE Alternative for std::function (allocation free)

I’ve been using an array of processing functions contained inside of an Array<std::function(void(float&)> in JUCE. Basically just to cleanly have some simple algorithm to swap through based on current settings.

After some profiling I’m realizing the container itself is having a heavy hit on my processor usage & doing some allocations on a per sample basis.

I stumbled upon this page while looking for a solution: https://cppcon2018.sched.com/event/FnKY/why-and-how-to-roll-your-own-stdfunction-implementation

“This presentation outlines why and how a std::function replacement was added to the JUCE open source, cross platform, software development framework and discusses some differences between our implementation and others.”

Anyone know of this mythical allocation free std::function implementation? : )

Best,

J

Here: inplace_function is a std::function that never allocates. You can specify a compile-time size, and if you capture a lot in a lambda you get a compile-time error and have to increase that size. It never allocates at runtime.

I would also recommend to avoid juce::Array for realtime audio code. It allocates all over the place: Even when calling remove() or clear(), which was surprising for me, coming from std::vector. I always use std::vector instead.

5 Likes

Awesome! thanks for the tips.