If you can guarantee the conditions you specified, then yes: it would be possible to efficiently completely statically distribute the stack. Each function will have a stack memory block.
However, will real compilers do this? Not.
It means absolutely nothing. In fact, he can get less than nothing. Often, most of the working stack is in the cache, so changing it is pretty cheap. If the stack was in static memory, then the only case when any particular function the memory “stack” function will be cached will be if you recently called this function. Using a real stack, you are likely to work in the cache.
In addition, by providing each function with a stack memory block, you can easily make the use of your static memory in your program much more than necessary. The stack is a fixed-size structure; no matter how many functions you have, the stack takes a certain size. If you have 100,000 functions, and each function occupies 64 bytes of space, then your static "stack" should occupy ~ 6.4 MB of space.
Why? You will never use most of this memory at any given time. The program will work perfectly in a stack of 1 MB or even 512 KB; Why take this memory 6 times for nothing?
Therefore, this is not a performance optimization and can inflate your program memory.
Nicol bolas
source share