EVOLUTION-MANAGER
Edit File: truelength.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Over-allocation access</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for truelength {data.table}"><tr><td>truelength {data.table}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Over-allocation access </h2> <h3>Description</h3> <p>These functions are experimental and somewhat advanced. By <em>experimental</em> 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. <code>alloc.col</code> is just an alias to <code>setalloccol</code>. We recommend to use <code>setalloccol</code> (though <code>alloc.col</code> will continue to be supported) because the <code>set*</code> prefix in <code>setalloccol</code> makes it clear that its input argument is modified in-place. </p> <h3>Usage</h3> <pre> truelength(x) setalloccol(DT, n = getOption("datatable.alloccol"), # default: 1024L verbose = getOption("datatable.verbose")) # default: FALSE alloc.col(DT, n = getOption("datatable.alloccol"), # default: 1024L verbose = getOption("datatable.verbose")) # default: FALSE </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p> Any type of vector, including <code>data.table</code> which is a <code>list</code> vector of column pointers. </p> </td></tr> <tr valign="top"><td><code>DT</code></td> <td> <p> A <code>data.table</code>. </p> </td></tr> <tr valign="top"><td><code>n</code></td> <td> <p> The number of spare column pointer slots to ensure are available. If <code>DT</code> is a 1,000 column <code>data.table</code> with 24 spare slots remaining, <code>n=1024L</code> means grow the 24 spare slots to be 1024. <code>truelength(DT)</code> will then be 2024 in this example. </p> </td></tr> <tr valign="top"><td><code>verbose</code></td> <td> <p> Output status and information. </p> </td></tr> </table> <h3>Details</h3> <p>When adding columns by reference using <code>:=</code>, we <em>could</em> 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 negligible 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 <em>shallow copy</em>, 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. </p> <p>When the allocated column pointer slots are used up, to add a new column <code>data.table</code> 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 data.table issue tracker). Setting <code>options(datatable.verbose=TRUE)</code> includes messages if and when a shallow copy is taken. To avoid shallow copies there are several options: use <code><a href="copy.html">copy</a></code> to make a deep copy first, use <code>setalloccol</code> to reallocate in advance, or, change the default allocation rule (perhaps in your .Rprofile); e.g., <code>options(datatable.alloccol=10000L)</code>. </p> <p>Please note : over allocation of the column pointer vector is not for efficiency <em>per se</em>; it is so that <code>:=</code> can add columns by reference without a shallow copy. </p> <h3>Value</h3> <p><code>truelength(x)</code> returns the length of the vector allocated in memory. <code>length(x)</code> of those items are in use. Currently, it is just the list vector of column pointers that is over-allocated (i.e. <code>truelength(DT)</code>), not the column vectors themselves, which would in future allow fast row <code>insert()</code>. For tables loaded from disk however, <code>truelength</code> is 0 in <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> 2.14.0+ (and random in <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> <= 2.13.2), which is perhaps unexpected. <code>data.table</code> detects this state and over-allocates the loaded <code>data.table</code> when the next column addition occurs. All other operations on <code>data.table</code> (such as fast grouping and joins) do not need <code>truelength</code>. </p> <p><code>setalloccol</code> <em>reallocates</em> <code>DT</code> 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 <code>DT</code>, for convenience in compound queries. </p> <h3>See Also</h3> <p><code><a href="copy.html">copy</a></code> </p> <h3>Examples</h3> <pre> DT = data.table(a=1:3,b=4:6) length(DT) # 2 column pointer slots used truelength(DT) # 1026 column pointer slots allocated setalloccol(DT, 2048) length(DT) # 2 used truelength(DT) # 2050 allocated, 2048 free DT[,c:=7L] # add new column by assigning to spare slot truelength(DT)-length(DT) # 2047 slots spare </pre> <hr /><div style="text-align: center;">[Package <em>data.table</em> version 1.14.4 <a href="00Index.html">Index</a>]</div> </body></html>