truelength: Over-allocation access

Description Usage Arguments Details Value See Also Examples

Description

These functions are experimental and somewhat advanced. By experimental we mean their names might change and perhaps the syntax, argument names and types. So if you write a lot of code using them, you have been warned! They should work and be stable, though, so please report problems with them.

Usage

1
2
3
4
truelength(x)
alloc.col(DT,
    n = getOption("datatable.alloccol"),        # default: quote(max(100L,ncol(DT)+64L))
    verbose = getOption("datatable.verbose"))   # default: FALSE

Arguments

x

Any type of vector, including data.table which is a list vector of column pointers.

DT

A data.table.

n

The number of column pointer slots to reserve in memory, including existing columns. May be a numeric, or a quote()-ed expression (see default). If DT is a 10 column data.table, n=1000 means grow the spare slots from 90 to 990, assuming the default of 100 has not been changed.

verbose

Output status and information.

Details

When adding columns by reference using :=, we could simply create a new column list vector (one longer) and memcpy over the old vector, with no copy of the column vectors themselves. That requires negligibe use of space and time, and is what v1.7.2 did. However, that copy of the list vector of column pointers only (but not the columns themselves), a shallow copy, resulted in inconsistent behaviour in some circumstances. So, as from v1.7.3 data.table over allocates the list vector of column pointers so that columns can be added fully by reference, consistently.

When the allocated column pointer slots are used up, to add a new column data.table must reallocate that vector. If two or more variables are bound to the same data.table this shallow copy may or may not be desirable, but we don't think this will be a problem very often (more discussion may be required on datatable-help). Setting options(datatable.verbose=TRUE) includes messages if and when a shallow copy is taken. To avoid shallow copies there are several options: use copy to make a deep copy first, use alloc.col to reallocate in advance, or, change the default allocation rule (perhaps in your .Rprofile); e.g., options(datatable.alloccol=1000).

Please note : over allocation of the column pointer vector is not for efficiency per se. It's so that := can add columns by reference without a shallow copy.

Value

truelength(x) returns the length of the vector allocated in memory. length(x) of those items are in use. Currently, it's just the list vector of column pointers that is over-allocated (i.e. truelength(DT)), not the column vectors themselves, which would in future allow fast row insert(). For tables loaded from disk however, truelength is 0 in R 2.14.0 and random in R <= 2.13.2; i.e., in both cases perhaps unexpected. data.table detects this state and over-allocates the loaded data.table when the next column addition or deletion occurs. All other operations on data.table (such as fast grouping and joins) do not need truelength.

alloc.col reallocates DT by reference. This may be useful for efficiency if you know you are about to going to add a lot of columns in a loop. It also returns the new DT, for convenience in compound queries.

See Also

copy

Examples

1
2
3
4
5
6
7
8
    DT = data.table(a=1:3,b=4:6)
    length(DT)                 # 2 column pointer slots used
    truelength(DT)             # 100 column pointer slots allocated
    alloc.col(DT,200)
    length(DT)                 # 2 used
    truelength(DT)             # 200 allocated, 198 free
    DT[,c:=7L]                 # add new column by assigning to spare slot
    truelength(DT)-length(DT)  # 197 slots spare

Example output

[1] 2
[1] 1026
   a b
1: 1 4
2: 2 5
3: 3 6
[1] 2
[1] 1026
[1] 1023

data.table documentation built on May 2, 2019, 4:57 p.m.