Skip to content

speed up tune_tbl() helper #929

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

Merged
merged 1 commit into from
Mar 15, 2023
Merged

speed up tune_tbl() helper #929

merged 1 commit into from
Mar 15, 2023

Conversation

simonpcouch
Copy link
Contributor

tune_tbl() is called each resample fit, and is a light wrapper around tibble::tibble(). The function is only called inside of tune_args(), so we know what kinds of inputs the helper will receive:

parsnip/R/tune_args.R

Lines 21 to 29 in d8f273c

tune_tbl(
name = names(res),
tunable = unname(!is.na(res)),
id = res,
source = "model_spec",
component = model_type,
component_id = NA_character_,
full = full
)

bm <- 
  bench::mark(
    total = fit_resamples(linear_reg(), mpg ~ ., bootstraps(mtcars, 100)),
    tune_tbl = replicate(100, parsnip:::tune_tbl()),
    check = FALSE
  )

bm
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 total         3.25s    3.25s     0.308    47.7MB     8.92
#> 2 tune_tbl    43.77ms  47.91ms    22.6        77KB    11.3

As a percentage of total time, the helper takes:

100 * as.numeric(bm$median[2]) / as.numeric(bm$median[1])
#> [1] 1.474154

Since we can recycle vectors as needed before inputting to the helper, we can use new_tibble(list()) instead of tibble(), which will drastically cut down on the time this helper takes.

Copy link
Member

@EmilHvitfeldt EmilHvitfeldt left a comment

Choose a reason for hiding this comment

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

Looks clean!

@topepo topepo merged commit dff211b into main Mar 15, 2023
@topepo topepo deleted the tune-tbl branch March 15, 2023 11:53
@github-actions
Copy link

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 30, 2023
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.

3 participants