perf: replace dplyr/tidyr internals with base R + vctrs in hot paths#1
Open
Melkiades wants to merge 4 commits into
Open
perf: replace dplyr/tidyr internals with base R + vctrs in hot paths#1Melkiades wants to merge 4 commits into
Melkiades wants to merge 4 commits into
Conversation
Replace dplyr::tibble with vctrs::new_data_frame in .lst_results_as_df, dplyr::bind_rows with vctrs::vec_rbind in .calculate_stats_as_ard, dplyr::rowwise + mutate with base R lapply in replace_null_statistic, and per-variable dplyr::mutate + tidyr::pivot_longer + dplyr::filter with base R reshape in .calculate_tabulation_statistics. Measured speedups (median, 200-row trial dataset, by = trt): ard_continuous: 2.6x faster ard_tabulate: 1.9x faster ard_missing: 1.8x faster replace_null_statistic: 5.4x faster tbl_summary (gtsummary): 1.24x faster
…olumns - apply_fmt_fun: for-loop instead of dplyr::mutate(pmap(...)) - nest_for_ard: base R split() instead of per-row dplyr::filter() - .nesting_rename_ard_columns: direct column assignment instead of dplyr::mutate + dplyr::rename
vctrs::new_data_frame() creates a plain data.frame, but downstream consumers (e.g. cardx::ard_categorical_ci) expect tibble class propagation through as_card(). Convert to tibble before returning. Co-authored-by: Ona <no-reply@ona.com>
…stics rep(keep_stats, nr) preserved names from stat_col_map subsetting, causing stat_name column to have spurious names that broke equality checks in downstream packages (e.g. crane::tbl_survfit_quantiles). 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.
What changes are proposed in this pull request?
Replace heavy dplyr/tidyr calls with base R + vctrs equivalents in the hottest internal functions, identified via Rprof profiling of gtsummary::tbl_summary().
Changes
.lst_results_as_dfdplyr::tibble()→vctrs::new_data_frame().calculate_stats_as_arddplyr::bind_rows()→vctrs::vec_rbind(), for-loop instead of map2.calculate_tabulation_statisticsreplace_null_statisticdplyr::rowwise()+dplyr::mutate()apply_fmt_fundplyr::mutate(pmap(...))nest_for_ardsplit()instead of per-rowdplyr::filter().nesting_rename_ard_columnsdplyr::mutate+dplyr::renameMeasured speedups (200-row trial dataset, by = trt)
Combined with gtsummary bridge optimizations (separate PR on Melkiades/gtsummary):
Test results
Demo