general questions on reference types versus value types...

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 1 12:52:15 PST 2014


Suliman:

> Could anybody explain why there is opinion that stack is fast 
> and the heap is slow. All of them are located in the same 
> memory. So the access time should be equal.

Often the access speed is not exactly the same, because the stack 
memory is usually hotter, this means it's more often kept in one 
of the CPU caches. But that's not the speed difference they 
usually talk about. They refer to the time needed to allocate and 
deallocate heap memory, that is often higher (and unpredictable) 
compared to allocating some stack memory. If you have a good GC 
like the OracleVM ones, allocating memory is very fast (sometimes 
little more than a comparison and an increase, thank to the green 
generation that is handled as a stack), but still not as fast as 
allocating and freeing stack memory. The situation is quite more 
complex than what I have explained. There are even CPUs with 
programmer-controlled scratch memory, instead of just an 
automatic managed hierarchy of caches.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list