klmr / box

Write reusable, composable and modular R code
https://klmr.me/box/
MIT License
827 stars 47 forks source link

Imported object environment size increases when loading other modules #366

Open mjakubczak opened 3 weeks ago

mjakubczak commented 3 weeks ago

Error description

Object size increases when importing multiple modules. This is really strange, because nothing happens to the object environment. In this example the size increases from 435kB to 26MB (!) just when loading 2nd module. Looks like all imported objects share the same resources, because finally all of them have the same accumulated size.

m1.R

box::use(dplyr[mutate])

#' @export
foo <- function(x){
  x |> mutate(m1 = 1)
}

dummy <- 2

m2.R

box::use(ggplot2[ggplot, aes, geom_point])

#' @export
bar <- function(x){
  ggplot(x, aes(Sepal.Width, Petal.length)) + geom_point()
}

m3.R

box::use(tidyr[drop_na])

#' @export
baz <- function(x){
  drop_na(x)
}

some_var <- "hi"

console output

> box::use(. / m1[foo])
> 
> pryr::object_size(foo)
435.83 kB
> 
> box::use(. / m2[bar])
> 
> pryr::object_size(foo) # the object size increases
25.84 MB
> pryr::object_size(bar)
25.84 MB
> 
> ls(environment(foo)) # environments look as expected
[1] "dummy" "foo"  
> ls(environment(bar))
[1] "bar"
> 
> box::use(. / m3[baz])
> 
> pryr::object_size(foo) # all objects have the same size
25.96 MB
> pryr::object_size(bar)
25.96 MB
> pryr::object_size(baz)
25.96 MB

R version

> R.version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          1.1                         
year           2021                        
month          08                          
day            10                          
svn rev        80725                       
language       R                           
version.string R version 4.1.1 (2021-08-10)
nickname       Kick Things

‘box’ version

1.2.0

klmr commented 3 weeks ago

I cannot reproduce this: when I execute the code, the object sizes stay constant. This is true across multiple systems and versions of R.

Here’s the output I get when running the exact same commands as you:

> box::use(./m1[foo])

> pryr::object_size(foo)
28.62 kB

> box::use(./m2[bar])

> pryr::object_size(foo)
28.62 kB

> pryr::object_size(bar)
50.69 kB

> box::use(./m3[baz])

> pryr::object_size(foo)
28.62 kB

> pryr::object_size(bar)
50.69 kB

> pryr::object_size(baz)
28.19 kB
mjakubczak commented 3 weeks ago

@klmr Thank you for the quick answer. I've determined that renv could be involved in this issue - please check this repo for a minimal reprex: https://github.com/mjakubczak/boxtest/

tl;dr: with renv:

> pryr::object_size(foo)
203.02 kB
> pryr::object_size(bar)
203.02 kB
> pryr::object_size(baz)
203.02 kB

without renv:

> pryr::object_size(foo)
24.75 kB
> pryr::object_size(bar)
22.64 kB
> pryr::object_size(baz)
23.44 kB