-
Notifications
You must be signed in to change notification settings - Fork 1k
R C API tips
This wiki page is meant to collect information that are useful for efficient R C api use, which itself is not very well documented.
pryr
package is doing that very well.
#install.packages("pryr")
print(sum) # take the body and paste into pryr::show_c_source
pryr::show_c_source(.Primitive("sum"))
truelength
is the allocated length. length
is amount used. truelength
was an unused field in R until recently. Now, finally, truelength
is used as it was intended by Ross originally (allocated length).
You can't set a new truelength. That's the actual allocation on R's heap / or allocated using malloc by R (R can do both depending on the size of the vector and how it has been configured/compiled). If a length is set smaller than truelength, though, which we do in data.table (e.g. at the end of fread) then the memory leak can be solved. I was told by an R core member there is a new 'growable' bit that can be set. When growable is set, gc() releases truelength rather than length, so the workarounds at the top of assign.c can be removed. It should have been like that in the first place in R, but for whatever reason they didn't use truelength at all
Very good example is the code contributed in fcaseR
by @2005m, related lines are https://github.com/Rdatatable/data.table/pull/4021/files#diff-25cd0b0c089d5976de15097388ff5683R153-R162
We should not use restrict when two threads update a shared variable, for example from within an atomic or critical, iiuc. I even found something online somewhere that even const together with restrict is beneficial too.