Difference between Heap and Stack
9.5K followers
174,492 views
14 Comments
Comments from the original post on X (Twitter)

@SebAaltonen
Allocating/deallocating from stack is fast because those are just add/sub to a pointer. Stack is also linear and the most recent data is in top, so it's very likely in L1$, which is just 5 cycles away from registers. Heap is scattered around the whole address space. Very likely cache miss and very expensive alloc/dealloc, since there's a complex data structure for the allocator and it must be protected from races (mutex) in multithreaded systems. Stack is per-thread, so there's never races there. No protection needed. Just a trivial non-atomic add/sub to manipulate it. Push/pop are not that important instructions nowadays. You simply fuse two fast instructions together shaving one cycle (not even that nowadays due to super wide execution). But spilling to stack is common, so might as well make that common operation slightly faster.
97 likes

@TommyYesItsMe
If you look at the GCC and TCC source code, they use heap very sparingly and stack only for local variables. But they use extensively static memory. The kind of C memory nobody talks about. Maybe C and C++ compiler devs know something about these languages we missed all along.
8 likes

@thestanduppod
Full Ep:
4 likes

@danielmvega
How come that the return address is not stored in RAM?
3 likes

@Matt_barlowe
A heap is where is just put things a stack is where i put things neatly
2 likes

@deadalnix
Another major difference is that the optimiser know the stack doesn't alias, so it can optimise much more. At every single level, from hardware to higher level languages, and everything in between, the staok is treated differently.
2 likes

@yawaramin
OCaml is this simple language that does that very direct translation btw let n = 10 (* int, on stack *) let s = "hello" (* string, on heap *)
2 likes

@HedebyIversen
This is dumb, how would this ever make my JavaScript go faster?
1 like

@RealOtisCrune
Odin would make a great learning language—very explicit about important lowish-level concepts while remaining super legible.
1 like

@AIexandruBogdan
Some of my favourite people on this podcast. Top Top!
1 like

@0x44_
Never heard of a stack stored on a special core. Maybe a modern convention tied to branch prediction, but traditionally there's no separate stack, and at least with C, it's up to the compiler designer to decide where things go based on the ISA, i.e. registers vs stack.

@kimiyoribaka
I think this is particularly interesting to look at for Wasm, where you have the Wasm stack that code can't access ever and then to compensate you have the stack that compilers like to make so function calls are easier to deal with, all of which are on JS heap.

@fctsMttrLsShttr
If you do not use recursion and limit the use of function pointers you can calculate the stack usage at compile time. But if you use dynamic allocation it's much harder to predict total heap usage and the chance of heap fragmentation. I avoid using the heap in my firmware.

@atorstling
There's also a fundamental difference in allocation strategy. Things allocated on the stack can't grow once more stack frames are added on top. This means that you need "side channels" for growing.