Question about obj_size in lobstr


#1

Can someone explain why obj_size behaves differently than base object.size?

library(lobstr)

x <- runif(1e6)
y <- list(c(x), x) 
z <- list(x, x)

all.equal(y, z)
#> [1] TRUE

object.size(y)
#> 16000160 bytes
object.size(z)
#> 16000160 bytes
obj_size(y)
#> 16,000,160 B
obj_size(z)
#> 8,000,112 B

Created on 2018-11-08 by the reprex package (v0.2.1)

In the docs I see:

Compared to object.size() , obj_size() :

  • Accounts for all types of shared values, not just strings in the global string pool.
  • Includes the size of environments (up to env )
  • Accurately measures the size of ALTREP objects.

But I'm not sure which one of those points might be highlighting the difference between the two.


#2

Just from your example I get the impression that you are reading Advanced R:

But doesn't Hadley explain exactly this in examples about y <- c(x, x, x)? So I would say that in this case difference is because lobstr correctly "[a]ccounts for all types of shared values, not just strings in the global string pool".


#3

Thanks for the link, I've read bits and pieces but not that specific page. I started playing with lobstr after watching this video. The only example I see that discusses the global string pool is:

banana <- "bananas bananas bananas"
obj_size(banana)
obj_size(rep(banana, 100))

Which produces the same results when using object.size. So I'm still confused why c() makes lobstr behave the way it does.

Edit: I think ref makes it a little clearer to me or at least visually explains why y is larger than z:

library(lobstr)

x <- runif(1e6)
y <- list(c(x), x) 
z <- list(x, x)

ref(y)
#> █ [1:0x7ff5715eb708] <list> 
#> ├─[2:0x1089d1000] <dbl> 
#> └─[3:0x106327000] <dbl>
ref(z)
#> █ [1:0x7ff5714ba248] <list> 
#> ├─[2:0x106327000] <dbl> 
#> └─[2:0x106327000]