Skip to content
Prev 49896 / 63424 Next

Recycling memory with a small free list

Reusing the LHS storage immediately isn't possible in general, because
evaluation of the RHS might produce an error, in which case the LHS
variable is supposed to be unchanged.  Detecting special cases where
there is guaranteed to be no error, or at least no error after the
first modification to newly allocated memory, might be too
complicated.  

Putting the LHS storage on a small free list for later reuse (only
after the old value of the variable will definitely be replaced) seems
more promising (then one would need only two copies for examples such
as above, with them being used in alternate iterations).  However,
there's a danger of getting carried away and essentially rewriting
malloc.  To avoid this, one might try just calling "free" on the
no-longer-needed object, letting "malloc" then figure out when it can
be re-used.  Unfortunately, that seems not to be safe, because it's
posslble that there is a reference to the no-longer-needed object on
the PROTECT stack, even though no one should actually be looking at
it any more.

In the current version of pqR (see pqR-project.org), modifications are
(often) done in place for statements such as w = w * Q, but not
curretly when the LHS variable does not appear on the RHS.

Regards,

    Radford Neal