4 ms·
C and C++ compilers can sometimes lower a small heap allocation into a stack allocation, at least in simpler cases. Here's some links about it * https://www.y
by nextaccountic 5y ago
C and C++ compilers can sometimes lower a small heap allocation into a stack allocation, at least in simpler cases.
Here's some links about it
* https://www.youtube.com/watch?v=lTgERgPNTF8 https://www.youtube.com/watch?v=lTgERgPNTF8 a 5 minutes talk about llvm doing this
* https://speice.io/2019/02/compiler-optimizations.html https://speice.io/2019/02/compiler-optimizations.html discusses this happening in Rust (the Rust compiler uses llvm)
* http://www.cs.cmu.edu/afs/cs.cmu.edu/user/jatina/www/CS_15_745_Final_Report.pdf http://www.cs.cmu.edu/afs/cs.cmu.edu/user/jatina/www/CS_15_7... a 12 page paper, also on llvm (ps: gcc can do it too, it was just easier to find llvm sources)
* https://en.wikipedia.org/wiki/Escape_analysis#Optimizations https://en.wikipedia.org/wiki/Escape_analysis#Optimizations (the optimization that enables this is called escape analysis: it tells whether the heap allocation "escapes" the current function, or if it's local enough that stack allocation is a good fit. inlining increases the likelihood that heap allocation won't escape)
* https://stackoverflow.com/questions/47075871/can-the-compiler-optimize-from-heap-to-stack-allocation https://stackoverflow.com/questions/47075871/can-the-compile... asking if compilers are actually allowed to do that (they sometimes are, but not always)
- Thorrez 5y agoGo doesn't even have a language-level distinction between heap and stack, everything is on the heap from the language standpoint. However, values that don't escape and that aren't too big are put on the stack by the compiler.