Skip to content
Prev 11183 / 12125 Next

[R-pkg-devel] [External] Re: Replacement for SETLENGTH

Thanks Luke !   I had seen the usage and discussion of growable vectors, as well as using SET_TRUELENGTH with SETLENGTH and didn't necessarily want to get even more out of API compliance :-). but if that looks like it will allowed (subject to perhaps changes) that seems like the better way forward to handle the different SEXPs.    And switching to smaller vectors and enlarging would be much more efficient in terms of memory.    I'll need to play around with how much to expand by as the enlargement would need to be in the loop with a final resizing before returning.

So if I understand the suggestion the use of xlengthgets basically handles the body of the code in EnlargeVector function  for allocation and copying (but now smaller vectors) with then the extra step to SETLENGTH and SET_TRUELENGTH
If done within a loop over MCMC iterations, then I would need to use SET_GROWABLE_BIT before the loop or when I encounter the need to enlarge.   (so basically a local implementation of EnlargeVector)

For my non-VECSXP objects (REALSXP, INTSXP)  it might be more efficient to use Realloc on a working array within loops and only allocate and assign after determining the final length (nUnique), and freeing the memory myself...  That way I avoid SETLENGTH altogether for those types.

best,
Merlise



Merlise Clyde (she/her/hers)
Professor of Statistical Science and Director of Graduate Studies
Duke University


________________________________________
From:?luke-tierney at uiowa.edu <luke-tierney at uiowa.edu>
Sent:?Wednesday, January 15, 2025 12:34 PM
To:?Iris Simmons <ikwsimmo at gmail.com>
Cc:?Merlise Clyde, Ph.D. <clyde at duke.edu>; List r-package-devel <r-package-devel at r-project.org>
Subject:?Re: [External] Re: [R-pkg-devel] Replacement for SETLENGTH
?
On Wed, 15 Jan 2025, Iris Simmons wrote:

            
You do not want to use memcpy or inanyother way try to write to the
locations in a VECSXP. It is not jus the reference counts but also the
integrity of the GC write barrier that you would be damaging.
These are not part of the API.

Support for growable vectors maybe added to the API in the future, but
probably with a more robust interface.

In any case, this mechanism is intended for growing, not shrinking,
vectors.

Initially over-allocating and returning a smaller result is a
reasonable strategy, but the right way to do it is to allocate a new
shorter result. xlengthgets is a convenient way to do this. Tholonger
vector will be subject to garbage collecion once there are no
remaining references to it.

Attempting to keep alive a longer allocation but pretending it is
shorter is mis-guided: it would keep alive a larger object than is
needed and so waste memory.

Best,

luke
--
Luke Tierney
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-tierney at uiowa.edu
Iowa City, IA 52242???????????????? WWW:? https://urldefense.com/v3/__http://www.stat.uiowa.edu/__;!!OToaGQ!uACtQIEun1eC8hwn-FzFogXQoPl1wETg9EUSV1NzAif9u15KlRTctzEq1RSA5rcbeVGv0n3geb8UexFnFDCXgK0$