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

Add a .method argument to the data frame method of arrange() #5090

Closed
wants to merge 3 commits into from

Conversation

DavisVaughan
Copy link
Member

Closes #4962

This PR adds a .method argument to the data frame method of arrange(). It defaults to "dplyr_auto", but can also be any valid order() method.

"dplyr_auto" is more aggressive than "auto" in two ways:

  • In addition to integer, numeric and logical vectors, it also uses radix ordering for character vectors. This forces the use of a C-locale, but is very fast. This is a breaking change for the defaults of arrange().

  • It uses typeof() rather than is.*() functions when determining if radix ordering can be used to operate at a level lower than S3 dispatch. This probably doesn't matter too much, because it is called on the comparison proxies of the columns (which generally returns the underlying raw data), but it still felt more correct.

library(dplyr, warn.conflicts = FALSE)

set.seed(123)

size <- 1000000

df <- tibble(
  a = sample(c("a", "b", "c", "d"), size, replace = TRUE),
  b = sample(1:20, size, replace = TRUE)
)

# Master
bench::mark(arrange(df, a, b), iterations = 20)
#> # A tibble: 1 x 6
#>   expression             min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>        <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 arrange(df, a, b)    1.99s    2.22s     0.449    41.1MB     1.80

# This PR
bench::mark(arrange(df, a, b), iterations = 20)
#> # A tibble: 1 x 6
#>   expression             min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>        <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 arrange(df, a, b)   34.3ms   37.8ms      24.8    48.8MB     105.

@DavisVaughan
Copy link
Member Author

DavisVaughan commented Apr 6, 2020

Problems:

  1. R 3.2 did not have a method argument at all, so it was forcibly chosen for you. Radix sorting still seems to have been done, but in a very small set of cases ("Method "radix" is only implemented for integer x with a range of less than 100,000")

  2. R 3.3 does not have a method = "auto" option, and seems to default to "shell" unless explicitly specified as "radix".

Copy link
Member

@romainfrancois romainfrancois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but that can equally wait for 1.1.0 ?

@romainfrancois romainfrancois requested a review from hadley May 20, 2020 09:21
@romainfrancois
Copy link
Member

Is the first problem enough justification to start depending on R > 3.3 ?

@DavisVaughan
Copy link
Member Author

DavisVaughan commented May 20, 2020

My current impression is that we will wait on this and maybe eventually add a custom ordering function to vctrs that works on R 3.2 and uses radix sorting, along with the ability to specify ascending/descending per column, but I might be wrong. It seems like that will be a tough project though

@DavisVaughan
Copy link
Member Author

Superseded by #6263

@DavisVaughan DavisVaughan deleted the arrange-method branch May 24, 2022 18:26
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

Successfully merging this pull request may close these issues.

Performance drop-off for arrange()
2 participants