Weird behavior with data.table and serialize

Viewed 160

Here is a simple, reproducible example:

fobj1 <- function(a, b) {
  list(a)
}

make1 <- function() {
  data <- data.table::data.table(1:1e8)
  a <- 1; b <- 2
  fobj1(a, b)
}

tmp <- make1()
print(object.size(serialize(tmp, connection = NULL)), units = 'Kb')



fobj2 <- function(a, b) {
  f <- function() {NULL}
  list(a, b, 'f' = f)
}

make2 <- function() {
  data <- data.table::data.table(1:1e8)
  a <- 1; b <- 2
  fobj2(a, b)
}

tmp <- make2()
print(object.size(serialize(tmp, connection = NULL)), units = 'Kb')


fobj3 <- function(a, b) {
  f <- function() {NULL}
  list(a, 'f' = f)
}

make3 <- function() {
  data <- data.table::data.table(1:1e8)
  a <- 1; b <- 2
  fobj3(a, b)
}

tmp <- make3()
print(object.size(serialize(tmp, connection = NULL)), units = 'Kb')

If I source this, the outcome is:

0.1 Kb
22.6 Kb
390647.9 Kb

Apparently, data is taken somehow as a reference of the list in the last example. And the reason is that I haven't specified all the function arguments in the list()! Weird?!?

Can someone reproduce and explain?

data.table version: data.table_1.12.4

R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
0 Answers
Related