Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

obj_size kills R session if asked to measure size of an object with linear models #35

Closed
romatik opened this issue Jan 4, 2019 · 6 comments

Comments

@romatik
Copy link

@romatik romatik commented Jan 4, 2019

While preparing reprex for someone else, I've created an object with some linear models in a list-column. My goal was to show the size of the object, so I've used lobstr::obj_size(), but using it kills the R session. pryr::object_size() works without an issue.

Minimal reprex with session info:

library(magrittr)

models <- tibble::as_tibble(mtcars) %>%
  dplyr::group_by(carb) %>%
  tidyr::nest() %>%
  dplyr::mutate(model = purrr::map(data, ~lm(disp ~ ., data = .x))) %>%
  tidyr::unnest(data, .drop = FALSE)

pryr::object_size(models)
#> 90 kB

## this would kill R session
#lobstr::obj_size(models)

Created on 2019-01-04 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.5.2 (2018-12-20)
#>  os       Ubuntu 18.04.1 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language en_US                       
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Europe/Berlin               
#>  date     2019-01-04                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version     date       lib source                         
#>  assertthat    0.2.0       2017-04-11 [1] CRAN (R 3.5.0)                 
#>  backports     1.1.3       2018-12-14 [1] CRAN (R 3.5.1)                 
#>  callr         3.1.1       2018-12-21 [1] CRAN (R 3.5.2)                 
#>  cli           1.0.1       2018-09-25 [1] CRAN (R 3.5.1)                 
#>  codetools     0.2-16      2018-12-24 [4] CRAN (R 3.5.2)                 
#>  crayon        1.3.4       2017-09-16 [1] CRAN (R 3.5.1)                 
#>  desc          1.2.0       2018-05-01 [1] CRAN (R 3.5.0)                 
#>  devtools      2.0.1       2018-10-26 [1] CRAN (R 3.5.1)                 
#>  digest        0.6.18      2018-10-10 [1] CRAN (R 3.5.1)                 
#>  dplyr         0.7.99.9000 2018-11-25 [1] local                          
#>  evaluate      0.12        2018-10-09 [1] CRAN (R 3.5.1)                 
#>  fs            1.2.6       2018-08-23 [1] CRAN (R 3.5.1)                 
#>  glue          1.3.0       2018-10-14 [1] Github (tidyverse/glue@4e74901)
#>  highr         0.7         2018-06-09 [1] CRAN (R 3.5.0)                 
#>  htmltools     0.3.6       2017-04-28 [1] CRAN (R 3.5.0)                 
#>  knitr         1.21        2018-12-10 [1] CRAN (R 3.5.1)                 
#>  magrittr    * 1.5         2014-11-22 [1] CRAN (R 3.5.0)                 
#>  memoise       1.1.0       2017-04-21 [1] CRAN (R 3.5.0)                 
#>  pillar        1.3.1       2018-12-15 [1] CRAN (R 3.5.2)                 
#>  pkgbuild      1.0.2       2018-10-16 [1] CRAN (R 3.5.1)                 
#>  pkgconfig     2.0.2       2018-08-16 [1] CRAN (R 3.5.1)                 
#>  pkgload       1.0.2       2018-10-29 [1] CRAN (R 3.5.1)                 
#>  prettyunits   1.0.2       2015-07-13 [1] CRAN (R 3.5.0)                 
#>  processx      3.2.1       2018-12-05 [1] CRAN (R 3.5.1)                 
#>  pryr          0.1.4       2018-02-18 [1] CRAN (R 3.5.2)                 
#>  ps            1.3.0       2018-12-21 [1] CRAN (R 3.5.2)                 
#>  purrr         0.2.5       2018-05-29 [1] CRAN (R 3.5.0)                 
#>  R6            2.3.0       2018-10-04 [1] CRAN (R 3.5.1)                 
#>  Rcpp          1.0.0       2018-11-07 [1] CRAN (R 3.5.1)                 
#>  remotes       2.0.2       2018-10-30 [1] CRAN (R 3.5.1)                 
#>  rlang         0.3.0.1     2018-10-25 [1] CRAN (R 3.5.1)                 
#>  rmarkdown     1.11        2018-12-08 [1] CRAN (R 3.5.1)                 
#>  rprojroot     1.3-2       2018-01-03 [1] CRAN (R 3.5.0)                 
#>  sessioninfo   1.1.1       2018-11-05 [1] CRAN (R 3.5.1)                 
#>  stringi       1.2.4       2018-07-20 [1] CRAN (R 3.5.1)                 
#>  stringr       1.3.1       2018-05-10 [1] CRAN (R 3.5.0)                 
#>  testthat      2.0.1       2018-10-13 [1] CRAN (R 3.5.1)                 
#>  tibble        1.4.2       2018-01-22 [1] CRAN (R 3.5.0)                 
#>  tidyr         0.8.2       2018-10-28 [1] CRAN (R 3.5.1)                 
#>  tidyselect    0.2.5       2018-10-11 [1] CRAN (R 3.5.1)                 
#>  usethis       1.4.0       2018-08-14 [1] CRAN (R 3.5.1)                 
#>  withr         2.1.2       2018-03-15 [1] CRAN (R 3.5.0)                 
#>  xfun          0.4         2018-10-23 [1] CRAN (R 3.5.1)                 
#>  yaml          2.2.0       2018-07-25 [1] CRAN (R 3.5.0)                 
#> 
#> [1] /home/misha/R/x86_64-pc-linux-gnu-library/3.5
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library

What I noticed is that if I omit last line in the pipeline tidyr::unnest(data, .drop = FALSE) same problem happens, but it takes ~10 seconds for R session to die. With this line present R session dies immediately.

RStudio info:
Version 1.2.1114
Build 1104 (2e0f7658)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.10.1 Chrome/61.0.3163.140 Safari/537.36

@hadley

This comment has been minimized.

Copy link
Member

@hadley hadley commented Jan 4, 2019

Also crashes for me

@hadley

This comment has been minimized.

Copy link
Member

@hadley hadley commented Jun 7, 2019

Simpler reprex:

df <- data.frame(g = 1:2, y = runif(10), x = runif(10))
mod <- lm(y ~ x, data = df)

lobstr::obj_size(mod$residuals)
@hadley

This comment has been minimized.

Copy link
Member

@hadley hadley commented Jun 7, 2019

Even simpler:

lobstr::obj_size(names(mod$residuals))

I think this is something to do with:

> .Internal(inspect(names(resid)))
@7f98e5ebef60 16 STRSXP g1c0 [MARK,NAM(3)]   <expanded string conversion>
  @7f98db7dab98 16 STRSXP g1c5 [MARK] (len=10, tl=0)
    @7f98d8846fe8 09 CHARSXP g1c1 [MARK,gp=0x61] [ASCII] [cached] "1"
    @7f98d91b37a8 09 CHARSXP g1c1 [MARK,gp=0x60] [ASCII] [cached] "2"
    @7f98dba4a570 09 CHARSXP g1c1 [MARK,gp=0x61] [ASCII] [cached] "3"
    @7f98dce61030 09 CHARSXP g1c1 [MARK,gp=0x61] [ASCII] [cached] "4"
    @7f98dbcb5f08 09 CHARSXP g1c1 [MARK,gp=0x61] [ASCII] [cached] "5"
    ...
@hadley

This comment has been minimized.

@hadley

This comment has been minimized.

Copy link
Member

@hadley hadley commented Jun 7, 2019

Root cause is probably because it doesn't contain a valid pairlist: https://github.com/wch/r-source/blob/1570b0e3b56e7d51ca8e18fcd7b8b7fab7aff109/src/main/altclasses.c#L598

@hadley

This comment has been minimized.

Copy link
Member

@hadley hadley commented Jun 11, 2019

Fixed in c34bd45

@hadley hadley closed this Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.