Rust Brain Teasers: "Stacking Boxes" heap vs. stack performance explanation

Yay - glad to help! While I’m here, I think the previous paragraph is also confusing:

“Reading data from the heap also requires a little more work: to read data, your program first needs to read the pointer to determine where the heap data is stored. Once it knows the location, the program can read the data from there.”

It’s confusing (to me) - accessing the stack is also via a pointer, and usually accessing the stack involves computing an offset from the stack pointer first, so…it’s not cut-and-dried easier.

One aspect of the difference between them that your discussion doesn’t yet capture is that allocating from the stack is super-cheap (moving the SP), where it’s almost rocket science what goes on in modern heap allocators.

I’m really enjoying the book so far - I’ve learned a lot already!

1 Like