POV-Ray : Newsgroups : povray.off-topic : Question about garbage collection (not a flame) : Re: Question about garbage collection (not a flame) Server Time
1 Oct 2024 15:23:30 EDT (-0400)
  Re: Question about garbage collection (not a flame)  
From: Warp
Date: 31 Mar 2008 15:58:31
Message: <47f15076@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> http://www.iecc.com/gclist/GC-faq.html

  "Most allocated objects have short lifetimes."

  Hmm, that may be so with languages which force you to allocate everything
on the heap (ie. things which potentially have longer lifetimes than
stack-allocated local objects).

  Personally I seldom find myself allocating memory dynamically for short
periods of time in time-critical parts of the program, when coding in C++.
Most short-lived objects and variables can usually be handled on the stack.

  Stack allocation is actually pretty fast (at least with gcc in linux).
I have made some tests which sometimes even gave surprising results:
Having a single heap-allocated array which is used by a function each
time this function is called can sometimes be even slower than having
the function allocate the array from the stack each time it's called.
(In other words, having allocated an array just once with 'new' and
having the function, which is being called repeatedly, just use that
array, may in some cases be slower than if the function allocated the
array for itself from the stack each time the function is called.)

  That's a bit surprising of a result, but might be explainable by
cache behavior or something similar.

  (The reason why I tested this was because having the function use
the externally-allocated array is not thread-safe, while having it
allocate the array locally is.)

-- 
                                                          - Warp


Post a reply to this message

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