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

Performance issue with chained assignment #5408

Closed
markfairbanks opened this issue Jun 20, 2022 · 2 comments
Closed

Performance issue with chained assignment #5408

markfairbanks opened this issue Jun 20, 2022 · 2 comments

Comments

@markfairbanks
Copy link

After using as.character() in assignment the next chained assignment is unexpectedly slow. I couldn't find any other functions that this occurred with.

library(data.table)

df <- data.table(x = round(runif(1e6), 4))

bench::mark(
  single = copy(df)[, x := as.character(x)][],
  chained = copy(df)[, x := as.character(x)][, x := x][],
  check = FALSE, iterations = 30
)
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 single        750µs   1.33ms    636.      9.48MB   273.  
#> 2 chained       416ms 490.03ms      2.00   22.97MB     2.42
@tlapak
Copy link
Contributor

tlapak commented Jun 20, 2022

String conversions are expensive, therefore R delays them where possible in case the result isn't actually needed. The initial as.character call only produces what's known as an ALTREP object which in this case is essentially just a pointer to the original and a note to do the conversion next time the object is touched. Simply printing it is enough to trigger this conversion and data.table also forces the expansion any time you touch such an ALTREP column. This is what happens in the second example. If you replace that call with copy(df)[, x := as.character(x)][, x] you get the same effect. (The empty angle brackets do nothing btw.) So what you're observing is simply R taking some liberties with when it actually does what you asked.

@markfairbanks
Copy link
Author

Gotcha - thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants