[Rd] A memory management question

Luke Tierney luke at stat.uiowa.edu
Mon Sep 5 01:26:28 CEST 2005


On Sun, 4 Sep 2005, dhinds at sonic.net wrote:

> Can someone explain the use of SETLENGTH() and SETTRUELENGTH()?
>
> I would like to allocate a vector and reserve some space at the end,
> so that it appears shorter than the allocated size.  So that I can
> more efficiently append to the vector, without requiring a new copy
> every time. So I'd like to use SETLENGTH() with a shorter apparent
> length, and bump this up as needed until I've used the entire space.

>
> There are only a couple users of SETLENGTH() in R, and they all appear
> at first glance to be pointless: a few routines use allocVector() and
> then call SETLENGTH() to set the vector length to the value that was
> just allocated.  What are valid uses for SETLENGTH()?  And what are
> the intended semantics for "truelength" as opposed to the regular
> length?
>

This is not supported by the memory manager.  Using SETLENGTH to
change the length would confuse the garbage collector--we should
probably remove SETLENGTH from the headers.

TRUELENGTH is unused except for something very different in envir.c.
Again we should probably remove or rename this to reflect how it is
currently used.  At one point, well before the current memory manager,
I believe there was thought that we might allow this sort of
over-allocation but I don't believe it was ever implemented.

The memory manager does over-allocate small vectors by rounding up to
convenient sizes, and the real size could be computed, but this is not
true for large allocations--these correspond to malloc calls for the
requested size--and in any case the memory manager relies on LENGTH
giving the correct amount (maybe not heavily but this could change).

> If GC happens and an object is moved, and its apparent LENGTH()
> differs from its allocated length, does GC preserve the allocated
> length, or the updated LENGTH()?  Is there any way to get at the
> original allocated length, given an SEXP?

A GC does not move objects.

Using R level vectors for the purpose you describe is in any case
tricky since it is hard to reliably prevent copying. You are better
off using something like an external pointer into an R-allocated
object that is only accessible through the external pointer.  Then you
can manage the filled length yourself.

luke


-- 
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:      luke at stat.uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu



More information about the R-devel mailing list