perf: replace dplyr/tidyr with vctrs in ard_summary hot paths#2
Open
Melkiades wants to merge 1 commit into
Open
perf: replace dplyr/tidyr with vctrs in ard_summary hot paths#2Melkiades wants to merge 1 commit into
Melkiades wants to merge 1 commit into
Conversation
Two functions optimized: .lst_results_as_df: Replace dplyr::tibble() + dplyr::mutate() + dplyr::rename() with a single vctrs::new_data_frame() call. This function is invoked once per variable x statistic x by-group (~72 times in a typical tbl_summary(by=) call). Each dplyr::tibble() has ~0.5ms NSE overhead that is eliminated. .calculate_stats_as_ard: Replace map()/map2()/dplyr::bind_rows() with for-loops and vctrs::vec_rbind(). Avoids intermediate tibble allocations and per-variable bind_rows overhead. Benchmark: tbl_summary(by=, 3 groups, 4 vars) drops from ~670ms to ~500ms (1.34x). Co-authored-by: Ona <no-reply@ona.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace
dplyr::tibble(),dplyr::mutate(),dplyr::rename(),map()/map2()/dplyr::bind_rows()withvctrs::new_data_frame(),for-loops, andvctrs::vec_rbind()in two internal functions called on every ARD computation.Motivation
tbl_summary(by=)spends ~16% of its total time in.lst_results_as_dfand.calculate_stats_as_ard. These functions are called once per variable × statistic × by-group (~72 times for a typical 4-variable, 3-arm table). Thedplyr::tibble()constructor has ~0.5ms NSE overhead per call that adds up.Changes
R/ard_summary.R— two functions:.lst_results_as_df: Singlevctrs::new_data_frame()call replacesdplyr::tibble()+dplyr::mutate(variable=)+dplyr::rename(stat="result"). Thevariableandstatcolumns are now set directly in the constructor..calculate_stats_as_ard:for-loops +vctrs::vec_rbind()replacemap()/map2()/dplyr::bind_rows(). Note:funandfun_namevariables must remain in scope becausegetOption("cards.calculate_stats_as_ard.eval_fun")(used byard_mvsummary) references them by name.DESCRIPTION: Addedvctrs (>= 0.6.5)to Imports. vctrs is already a transitive dependency via dplyr and tidyr.Benchmark
tbl_summary(data, by = trt, include = c(age, sex, grade))with 500 rows, 3 treatment arms:Testing