I'm using alloca
a lot these days, to allocate temporary buffers. In my application (signal processing) this is a common need.
The question is:
When allocating multiple arrays, is it better (performance-wise) to use alloca just once?
Like this:
float *array1 = (float*)alloca(4096 * 4);
float *array2 = array1 + 1024;
float *array3 = array2 + 1024;
float *array4 = array3 + 1024;
Or use it multiple times like this:
void *array1 = (float*)alloca(4096);
void *array2 = (float*)alloca(4096);
void *array3 = (float*)alloca(4096);
void *array4 = (float*)alloca(4096);
I mean all it probably does is decrease the stack pointer and probably do the "stack probe", which depends on the size, so it perhaps doesn't matter?
alloca
is made to be faster thanmalloc
for reasons related with the way the allocation/deallocation is performed, and the sections of memory used, as I'm sure you know. It's also, as stated in the comments, very easy to get wrong.To the point, my guess would be that the first version, repeating
alloca
, would be faster, in an unoptimized setting, than playing with indirection, and indeed, after some benchmarks this was confirmed:The tests were performed using google benchmark, clang 10.0, C++20 std and no optimization. The tests were repeated with constant results using function runs with code similar to that of the OP:
Whith
O3
otpimization added, as one would expect, the test results will even out, the multiplealloca
still consistently slightly faster but the differences in performance are negligible. As you stated, it's basicaly the same. To use one or the other seems to make little to no difference.Disclaimer:
To best understand the performance of your program, integrated testing would give you a more accurate reading than isolated testing like done here. The buils tools as well as the environment will also affect the end result, to fully and accurately measure the performance of your options you must test them yourself.