Description Usage Arguments Details Value Author(s) See Also Examples
Compacting an object is modifying its internal representation in order to reduce its size in memory.
1 2 3 4 5 |
x |
For For |
check |
After compacting the individual slots of an S4 object, this argument
is passed to |
... |
Arguments to be passed to or from other methods. |
start, end, width, lkup, reverse |
For internal use. |
The internal reorganization of the object should be transparent to the
user i.e. compact(x)
should "look" the same as x
, or,
more precisely, x
and compact(x)
should be interchangeable
anywhere in the user's code. However, because they have different internal
representations, we generally don't expect identical(x, compact(x))
to be TRUE, even though most of the times they will, because there are
only very few types of objects that compact
actually knows how to
reorganize internally.
compact
is a generic function.
Here is how the default method works. By default compact(x)
is
obtained by compacting all the "components" in x
. Only 2 kinds
of objects are considered to have "components": lists (the components
are the list elements), and S4 objects (the components are the slots).
The other objects are not considered to have components, so, by default,
compact
does nothing on them. In particular, it does nothing on
environments. Also the attributes of an object (other than the slots of
an S4 object) are not considered to be "components" and therefore are
not compacted.
Note that, in the absence of specialized compact
methods that
actually know how to reorganize an object internally, the default method
would visit the tree of all the components, sub-components,
sub-sub-components etc of object x
without actually modifying
anything in x
. So of course, specialized compact
methods
need to be defined for the objects that can *effectively* be compacted.
Otherwise the compact
function would be equivalent to the
identity
function!
At the moment, 2 specialized compact
methods are defined (in
addition to the default method): one for XVector objects, and
one for XVectorList objects.
An object equivalent to x
but eventually smaller in memory.
H. Pagès
XVector-class,
XVectorList-class,
subseq
,
object.size
,
save
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ## We illustrate the use of compact() on an XInteger vector (XInteger
## is one of the 3 concrete subclasses of the XVector virtual class):
x <- XInteger(500000, sample(500000))
## subseq() does NOT copy the data stored in an XVector object:
y <- subseq(x, start=41, end=60)
x@shared
y@shared # same address
object.size(x)
object.size(y) # same size
## compact() copies the data, but only the data actually "used" by 'y':
y0 <- compact(y)
y0@shared # new address
object.size(y0) # much smaller now!
## Compaction is particularly relevant when saving an object with
## external references like 'y':
yfile <- file.path(tempdir(), "y.rda")
save(y, file=yfile)
file.info(yfile)$size
y0file <- file.path(tempdir(), "y0.rda")
save(y0, file=y0file)
file.info(y0file)$size
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.