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

chr() performance #366

Closed
jimhester opened this issue Jan 17, 2018 · 4 comments
Closed

chr() performance #366

jimhester opened this issue Jan 17, 2018 · 4 comments

Comments

@jimhester
Copy link
Member

@jimhester jimhester commented Jan 17, 2018

I conversation on twitter (https://twitter.com/PhilGrosjean/status/953549513054179328) mentioned that rlang::set_names() is much slower than stats::setNames().

It turns out almost all of the performance decrease is due to the call to chr()

https://github.com/tidyverse/rlang/blob/c56b0aecd8efe225b11db1649c3e827674f0b662/R/attr.R#L242

If you omit the call to chr() when there are no dots you rescue the performance degradation almost entirely. You could do this without forcing the promise in R-devel with ...length(), but perhaps you have a rlang function that does similar. Also you may want to think about a fast return for simple single argument vector cases of chr()

diff --git a/R/attr.R b/R/attr.R
index 44e52035..c487af0e 100644
--- a/R/attr.R
+++ b/R/attr.R
@@ -239,7 +239,9 @@ set_names <- function(x, nm = x, ...) {
   } else if (!is_null(nm)) {
     # Make sure `x` is serialised when no arguments is provided.
     nm <- as.character(nm)
-    nm <- chr(nm, ...)
+    if (length(list(...)) > 0) {
+      nm <- chr(nm, ...)
+    }
   }

   if (!is_null(nm) && !is_character(nm, length(x))) {
x <- rep(letters, 1e6)
microbenchmark::microbenchmark(
  set_names(seq_along(x), x),
  set_names_old(seq_along(x), x),
  stats::setNames(seq_along(x), x))
#> Unit: milliseconds
#>                              expr       min        lq     mean    median        uq      max neval
#>        set_names(seq_along(x), x)  49.78397  53.17709 118.6732  83.23718 110.4262 378.1382   100
#>    set_names_old(seq_along(x), x) 249.66423 312.33776 447.6747 511.29348 554.1848 739.7161   100
#>  stats::setNames(seq_along(x), x)  49.66855  53.64586 103.3154  76.05925 107.7731 368.2463   100
@lionel-
Copy link
Member

@lionel- lionel- commented Jan 18, 2018

Was this with the CRAN version? It should be faster with the devel version. It will be rewritten to C eventually along with the rest of rlang.

Also we probably don't need splicing semantics at all here so dots will be captured with c()

@jimhester
Copy link
Member Author

@jimhester jimhester commented Jan 18, 2018

@lionel- lionel- closed this in dc8c250 Jan 18, 2018
@lionel-
Copy link
Member

@lionel- lionel- commented Jan 18, 2018

It seems like c(x, ...) allocates a new vector even when ... is empty, which causes a big performance degradation for large x. This could be optimised in R core.

@jimhester
Copy link
Member Author

@jimhester jimhester commented Jan 18, 2018

Yeah, agreed that would be a nice (and relatively simple) optimization

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