[Rd] Possibility for memory improvement: x <- as.vector(x) always(?) duplicates

From: Henrik Bengtsson <hb_at_biostat.ucsf.edu>
Date: Tue, 23 Nov 2010 06:12:18 -0800


Hi,

I've noticed that as.vector() always allocates a new object, e.g.

> x <- 1:10;
> x <- as.vector(x);
> tracemem(x);

[1] "<0x0000000005622db8"
> x <- as.vector(x);

tracemem[0x0000000005622db8 -> 0x0000000005622ec0]: as.vector
> x <- as.vector(x);

tracemem[0x0000000005622ec0 -> 0x0000000005622f18]: as.vector
> x <- as.vector(x);

tracemem[0x0000000005622f18 -> 0x000000000561c388]: as.vector
> x <- as.vector(x);

tracemem[0x000000000561c388 -> 0x000000000561c3e0]: as.vector
>

and so on.

This also seems to be the reason for an extra copy being created in turning a vector into a matrix or an array, e.g.

> x <- 1:10;
> tracemem(x);

[1] "<0x000000000561c750"
> x <- matrix(x, nrow=5, ncol=2);

tracemem[0x000000000561c750 -> 0x000000000561c7a8]: as.vector matrix
>

Example of how it could work (not sure if the test with is.vector() is enough):

as.vector <- function(x, mode="any") {
  if (is.vector(x)) return(x);
  base::as.vector(x);
} # as.vector()

matrix <- base::matrix;
environment(matrix) <- globalenv();

> x <- 1:10;
> tracemem(x);

[1] "<0x0000000003965488"
> x <- matrix(x, nrow=5, ncol=2);
>

Could this be generic improvement? Some years ago there similar improvements where done for as.integer(), as.numeric() etc.

This is on R v2.12.0 patched (2010-11-09 r53543) and R v2.13.0 devel (2010-11-20 r53645) on Windows 7 Ultimate.

/Henrik



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 23 Nov 2010 - 14:23:38 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 30 Nov 2010 - 06:50:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive