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

Speed of first() evaluated within grouped tibbles #6682

Closed
dkutner opened this issue Feb 2, 2023 · 4 comments · Fixed by #6710
Closed

Speed of first() evaluated within grouped tibbles #6682

dkutner opened this issue Feb 2, 2023 · 4 comments · Fixed by #6710
Milestone

Comments

@dkutner
Copy link

dkutner commented Feb 2, 2023

Using dplyr 1.1.0 and vctrs 0.5.2, I'm noticing speed issues when evaluating first within grouped tibbles.

df <- dplyr::group_by(tibble::tibble(x = 1:10000, grp = rep(1:100, 100)), grp)
bench::mark(
  first = dplyr::summarize(
    df,
    f = dplyr::first(x)
  ),
  indexed = dplyr::summarize(
    df,
    f = x[1]
  )
)
#> # A tibble: 2 x 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 first        7.44ms   7.98ms      119.    1.57MB     19.1
#> 2 indexed      1.01ms   1.06ms      901.   55.62KB     12.7

On dplyr 1.0.10 with vctrs 0.5.0, the first benchmark is about 1.7 ms.

@hadley
Copy link
Member

hadley commented Feb 2, 2023

Why do you think this is related to grouped tibbles?

@dkutner
Copy link
Author

dkutner commented Feb 2, 2023

I'm not sure if it's isolated to grouped tibbles, but the effect seems to be more apparent on them. If we omit the grouping in the above reprex, both benchmarks have similar times.

@dkutner
Copy link
Author

dkutner commented Feb 2, 2023

I'm probably just observing the effect of running first many times. Here's a simpler example without a grouped tibble. On the previous version of dplyr, the first benchmark took 8 microseconds.

x <- 1:10000
bench::mark(
  first = dplyr::first(x),
  indexed = x[1]
)
#> # A tibble: 2 x 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 first        64.7us   69.8us    13674.    10.3MB     18.9
#> 2 indexed       100ns    200ns  4016387.        0B      0

@hadley
Copy link
Member

hadley commented Feb 3, 2023

Thanks for clarifying!

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 a pull request may close this issue.

3 participants