POV-Ray : Newsgroups : povray.off-topic : Question about garbage collection (not a flame) : Re: Question about garbage collection (not a flame) Server Time
3 Nov 2024 01:12:04 EDT (-0400)
  Re: Question about garbage collection (not a flame)  
From: Fredrik Eriksson
Date: 1 Apr 2008 12:05:45
Message: <op.t8xy3ubv7bxctx@e6600.bredbandsbolaget.se>
On Tue, 01 Apr 2008 11:08:34 +0200, Warp <war### [at] tagpovrayorg> wrote:
> Fredrik Eriksson <fe79}--at--{yahoo}--dot--{com> wrote:
>> That stack allocation is fast should come as no surprise since it
>> typically requires a grand total of zero additional CPU instructions. In
>> the worst case it requires a single one-cycle instruction.
>
>   But so does using a pre-allocated array. The array is *already*  
> allocated.
> The function doesn't need to allocate it each time. Yet, in some cases,
> the function becomes faster by using the stack-allocated array than the
> pre-allocated array in the heap.

Two points to consider:

- Data locality
Allocating the array from the stack keeps it close to the local variables,  
thus confining memory accesses to a smaller area. This plays better with  
memory caches.

- Register contention
Accessing a heap-allocated array requires loading its address into a CPU  
register. Accessing a stack-allocated array can be done with the stack  
register, freeing other registers for use in calculations.


-- 
FE


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.