general questions on reference types versus value types...

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 1 13:09:07 PST 2014


On Mon, Dec 01, 2014 at 08:22:59PM +0000, Suliman via Digitalmars-d-learn wrote:
> 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.

That may be true 15 years ago, it's not true today with multilevel CPU
caches. The stack is basically always "hot" in the CPU cache because
it's very frequently accessed (function calls, function returns,
temporaries, local variables, etc.), so accessing stuff on the stack
almost always hits the cache. Accessing stuff on the heap is likely to
incur a cache miss, so the CPU has to go and fetch it from RAM
(sloooow).

Not to mention, allocating stuff on the heap incurs a lot of overhead to
keep track of which parts of memory is in use or free, whereas
allocating stuff on the stack is just bumping a pointer (and compilers
usually combine several stack allocations into a single instruction that
allocates all of them at once -- optimizers will even elide pointer
bumps if the total required size for local variables is already known in
advance and the end of the allocated region doesn't need to be used --
e.g. the function never calls another function). Also, allocating on the
heap generates garbage for the GC to collect.

Also, heap-allocated objects tend to require pointer dereferences to
access, which means higher chance of CPU cache miss (the pointer and the
target data may be in two different RAM pages, so the CPU may have to do
the RAM roundtrip *twice* -- this is esp. true for virtual function
calls).


T

-- 
Живёшь только однажды.


More information about the Digitalmars-d-learn mailing list