Skip to content

Commit 37423fb

Browse files
committed
use .selfref.ok()
1 parent fd6550d commit 37423fb

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

inst/tests/froll.Rraw

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,25 +1816,25 @@ if (use.fork) {
18161816
test(6010.764, frollapply(c(1, 5, 9), N=1L, FUN=identity), c(5,5,9)) ## unexpected again
18171817
ans = frollapply(1:2, 2, data.table)
18181818
is.ok = function(x) {stopifnot(is.data.table(x)); out=capture.output(print(attr(x, ".internal.selfref", TRUE))); out!="<pointer: (nil)>" && out!="<pointer: 0x0>"}
1819-
test(6010.769, is.ok(ans)) ## frollapply will fix DT in most cases
1819+
test(6010.769, .selfref.ok(ans)) ## frollapply will fix DT in most cases
18201820
ans = frollapply(1:2, 2, data.table, simplify=FALSE) ## default: fill=NA
1821-
test(6010.770, is.ok(ans[[2L]])) ## frollapply detected DT and fixed
1821+
test(6010.770, .selfref.ok(ans[[2L]])) ## frollapply detected DT and fixed
18221822
ans = frollapply(1:2, 2, data.table, fill=data.table(c(NA,NA))) ## fill size match
1823-
test(6010.771, is.ok(ans)) ## simplify=TRUE did run rbindlist, but frollapply fixed anyway
1823+
test(6010.771, .selfref.ok(ans)) ## simplify=TRUE did run rbindlist, but frollapply fixed anyway
18241824
ans = frollapply(1:2, 2, data.table, fill=data.table(NA)) ## fill size not match, no rbindlist, but frollapply fixed anyway
1825-
test(6010.772, is.ok(ans[[2L]]))
1825+
test(6010.772, .selfref.ok(ans[[2L]]))
18261826
ans = frollapply(1:2, 2, function(x) list(data.table(x)), fill=list(data.table(NA)), simplify=FALSE)
1827-
test(6010.773, !is.ok(ans[[2L]][[1L]]))
1827+
test(6010.773, !.selfref.ok(ans[[2L]][[1L]]))
18281828
test(6010.7731, set(ans[[2L]][[1L]],, "newcol", 1L), error="data.table has either been loaded from disk")
18291829
ans = lapply(ans, lapply, setDT)
1830-
test(6010.774, is.ok(ans[[2L]][[1L]])) ## fix after
1830+
test(6010.774, .selfref.ok(ans[[2L]][[1L]])) ## fix after
18311831
ans = frollapply(1:2, 2, function(x) list(data.table(x)), fill=list(data.table(NA)), simplify=function(x) lapply(x, lapply, setDT))
1832-
test(6010.775, is.ok(ans[[2L]][[1L]])) ## fix inside frollapply via simplify
1832+
test(6010.775, .selfref.ok(ans[[2L]][[1L]])) ## fix inside frollapply via simplify
18331833
f = function(x) (if (x[1L]==1L) data.frame else data.table)(x) ## automatic fix may not work for a non-type stable function
18341834
ans = frollapply(1:3, 2, f, fill=data.table(NA), simplify=FALSE)
1835-
test(6010.776, !is.ok(ans[[3L]]))
1835+
test(6010.776, !.selfref.ok(ans[[3L]]))
18361836
ans = frollapply(1:3, 2, f, fill=data.table(NA), simplify=function(x) lapply(x, function(y) if (is.data.table(y)) setDT(y) else y))
1837-
test(6010.777, is.ok(ans[[3L]])) ## fix inside frollapply via simplify
1837+
test(6010.777, .selfref.ok(ans[[3L]])) ## fix inside frollapply via simplify
18381838
setDTthreads(old, throttle=1024) ## re-enable throttle
18391839
}
18401840

man/frollapply.Rd

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,49 +85,43 @@ setDTthreads(old)
8585
}
8686
\item Objects returned from forked processes, \code{FUN}, are serialized. This may cause problems for objects that are meant not to be serialized, like data.table. We are handling that for data.table class internally in \code{frollapply} whenever \code{FUN} is returning data.table (which is checked on the results of the first \code{FUN} call so it assumes function is type stable). If data.table is nested in another object returned from \code{FUN} then the problem may still manifest, in such case one has to call \code{setDT} on objects returned from \code{FUN}. This can be also nicely handled via \code{simplify} argument when passing a function that calls \code{setDT} on nested data.table objects returned from \code{FUN}. Anyway, returning data.table from \code{FUN} should, in majority of cases, be avoided from the performance reasons, see \emph{UDF optimization} section for details.
8787
\preformatted{
88-
is.ok = function(x) {
89-
stopifnot(is.data.table(x))
90-
out = capture.output(print(attr(x, ".internal.selfref", TRUE)))
91-
out!="<pointer: (nil)>" && out!="<pointer: 0x0>"
92-
}
93-
9488
setDTthreads(2, throttle=1) ## disable throttle
9589
## frollapply will fix DT in most cases
9690
ans = frollapply(1:2, 2, data.table)
97-
is.ok(ans)
91+
.selfref.ok(ans)
9892
#[1] TRUE
9993
ans = frollapply(1:2, 2, data.table, simplify=FALSE)
100-
is.ok(ans[[2L]])
94+
.selfref.ok(ans[[2L]])
10195
#[1] TRUE
10296

10397
## nested DT not fixed
10498
ans = frollapply(1:2, 2, function(x) list(data.table(x)), fill=list(data.table(NA)), simplify=FALSE)
105-
is.ok(ans[[2L]][[1L]])
99+
.selfref.ok(ans[[2L]][[1L]])
106100
#[1] FALSE
107101
#### now if we want to use it
108102
set(ans[[2L]][[1L]],, "newcol", 1L)
109103
#Error in set(ans[[2L]][[1L]], , "newcol", 1L) :
110104
# This data.table has either been loaded from disk (e.g. using readRDS()/load()) or constructed manually (e.g. using structure()). Please run setDT() or setalloccol() on it first (to pre-allocate space for new columns) before assigning by reference to it.
111105
#### fix as explained in error message
112106
ans = lapply(ans, lapply, setDT)
113-
is.ok(ans[[2L]][[1L]])
107+
.selfref.ok(ans[[2L]][[1L]])
114108
#[1] TRUE
115109

116110
## fix inside frollapply via simplify
117111
simplifix = function(x) lapply(x, lapply, setDT)
118112
ans = frollapply(1:2, 2, function(x) list(data.table(x)), fill=list(data.table(NA)), simplify=simplifix)
119-
is.ok(ans[[2L]][[1L]])
113+
.selfref.ok(ans[[2L]][[1L]])
120114
#[1] TRUE
121115

122116
## automatic fix may not work for a non-type stable function
123117
f = function(x) (if (x[1L]==1L) data.frame else data.table)(x)
124118
ans = frollapply(1:3, 2, f, fill=data.table(NA), simplify=FALSE)
125-
is.ok(ans[[3L]])
119+
.selfref.ok(ans[[3L]])
126120
#[1] FALSE
127121
#### fix inside frollapply via simplify
128122
simplifix = function(x) lapply(x, function(y) if (is.data.table(y)) setDT(y) else y)
129123
ans = frollapply(1:3, 2, f, fill=data.table(NA), simplify=simplifix)
130-
is.ok(ans[[3L]])
124+
.selfref.ok(ans[[3L]])
131125
#[1] TRUE
132126

133127
setDTthreads(2, throttle=1024) ## enable throttle

0 commit comments

Comments
 (0)