Last data update: 2014.03.03

R: Object compaction
compactR Documentation

Object compaction

Description

Compacting an object is modifying its internal representation in order to reduce its size in memory.

Usage

compact(x, check=TRUE, ...)

## Internal compact() support function. Not intended to be called
## directly:
xvcopy(x, start=NA, end=NA, width=NA, lkup=NULL, reverse=FALSE)

Arguments

x

For compact, the object to be compacted. compact accepts any R object. However, on most of them, compact won't do anything and will just return an object identical to x. See the Details section below.

For xvcopy, a SharedVector, XVector, SharedVector_Pool, or XRawList vector.

check

After compacting the individual slots of an S4 object, this argument is passed to `slot<-` when replacing the original value of a slot with the compacted value.

...

Arguments to be passed to or from other methods.

start, end, width, lkup, reverse

For internal use.

Details

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.

Value

An object equivalent to x but eventually smaller in memory.

Author(s)

H. Pag<c3><83><c2><a8>s

See Also

XVector-class, XVectorList-class, subseq, object.size, save

Examples

## 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

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(XVector)
Loading required package: BiocGenerics
Loading required package: parallel

Attaching package: 'BiocGenerics'

The following objects are masked from 'package:parallel':

    clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
    clusterExport, clusterMap, parApply, parCapply, parLapply,
    parLapplyLB, parRapply, parSapply, parSapplyLB

The following objects are masked from 'package:stats':

    IQR, mad, xtabs

The following objects are masked from 'package:base':

    Filter, Find, Map, Position, Reduce, anyDuplicated, append,
    as.data.frame, cbind, colnames, do.call, duplicated, eval, evalq,
    get, grep, grepl, intersect, is.unsorted, lapply, lengths, mapply,
    match, mget, order, paste, pmax, pmax.int, pmin, pmin.int, rank,
    rbind, rownames, sapply, setdiff, sort, table, tapply, union,
    unique, unsplit

Loading required package: S4Vectors
Loading required package: stats4

Attaching package: 'S4Vectors'

The following objects are masked from 'package:base':

    colMeans, colSums, expand.grid, rowMeans, rowSums

Loading required package: IRanges
> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/XVector/compact-methods.Rd_%03d_medium.png", width=480, height=480)
> ### Name: compact
> ### Title: Object compaction
> ### Aliases: xvcopy xvcopy,SharedVector-method xvcopy,XVector-method
> ###   xvcopy,SharedVector_Pool-method xvcopy,XRawList-method compact
> ###   compact,ANY-method compact,XVector-method compact,XVectorList-method
> ### Keywords: methods
> 
> ### ** Examples
> 
> ## 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
SharedInteger of length 500000 (data starting at address 0x72738d8)
> y@shared  # same address
SharedInteger of length 500000 (data starting at address 0x72738d8)
> object.size(x)
2002096 bytes
> object.size(y)  # same size
2002096 bytes
> 
> ## compact() copies the data, but only the data actually "used" by 'y':
> y0 <- compact(y)
> y0@shared  # new address
SharedInteger of length 20 (data starting at address 0x6009e98)
> object.size(y0)  # much smaller now!
2224 bytes
> 
> ## 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
[1] 1514424
> 
> y0file <- file.path(tempdir(), "y0.rda")
> save(y0, file=y0file)
> file.info(y0file)$size
[1] 3992
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>