general questions on reference types versus value types...

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 1 13:48:29 PST 2014


On Monday, 1 December 2014 at 20:23:00 UTC, Suliman 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.

Yes, the problem is that if you load from a memory area (of 64 
bytes) that has not been visisted in a while then the cpu will 
have to wait for approximately the same time it takes to execute 
100 instructions. So if you touch a 64 byte block every 20 
instructions that needs to be loaded from RAM you end up with the 
CPU being 80% underutilized…

The throughput is actually quite nice, but the latency is the 
problem.

It is possible to tell the cpu to prefetch new areas of memory 
ahead of time, but then you have to do it 100 instructions before 
you actually access it… Which is hard to get right and might be 
slower if the memory was already loaded into the caches anyway. 
On some CPUs the prefetch instructions can be counterproductive…


More information about the Digitalmars-d-learn mailing list