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 up new_tibble() #901

Merged
merged 3 commits into from
Jul 5, 2021
Merged

Conversation

mgirlich
Copy link
Contributor

@mgirlich mgirlich commented Jul 5, 2021

Relates to #726.

Some minor code changes give a nice speed up for new_tibble()

library(tibble)

# empty
y <- list()
y_nrow <- 0L

bench::mark(
  tbl = new_tibble(y, nrow = y_nrow),
  iterations = 100000
)
# master
#> # 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 tbl          8.66µs   10.5µs    88253.    35.4KB     19.4

# replace `setdiff()`
#> # 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 tbl          5.92µs    7.7µs   115497.      36KB     17.3

# this PR
#> # 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 tbl          5.29µs   6.56µs   133027.    32.3KB     18.6

x <- unclass(mtcars)
nrow <- 32L

bench::mark(
  tbl = new_tibble(x, nrow = nrow),
  iterations = 100000
)
# master
#> # 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 tbl           8.1µs   9.95µs    93560.        0B     22.5

# replace `setdiff()`
#> # 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 tbl          5.48µs   7.07µs   128840.        0B     20.6

# this PR
#> # 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 tbl             5µs   6.01µs   143254.        0B     20.1


bench::mark(
  tbl = new_tibble(x, nrow = nrow, foo = "bar", foo2 = 1, foo3 = "x"),
  iterations = 100000
)
# master
#> # 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 tbl          10.3µs   12.1µs    77771.        0B     24.1

# replace `setdiff()`
#> # 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 tbl          7.21µs   9.05µs   103539.        0B     23.8

# this PR
#> # 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 tbl           6.3µs   8.06µs   112465.        0B     22.5

Created on 2021-07-05 by the reprex package (v2.0.0)

@krlmlr krlmlr merged commit 516449b into tidyverse:master Jul 5, 2021
@krlmlr
Copy link
Member

krlmlr commented Jul 5, 2021

Thanks!

@mgirlich mgirlich deleted the speed-up-new_tibble() branch July 6, 2021 11:34
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants