From 3a9e39b0da28fd29cf8a7245497741bdbf64d029 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Tue, 7 May 2024 09:02:48 +0900 Subject: [PATCH 1/3] perf(fuzzyhelp): use memoise to improve response --- R/fuzzyhelp.R | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/R/fuzzyhelp.R b/R/fuzzyhelp.R index 82895a8..c962d97 100644 --- a/R/fuzzyhelp.R +++ b/R/fuzzyhelp.R @@ -189,7 +189,7 @@ adist2 <- function(x, y, case_sensitive) { } score_toc_filtered <- list( - fzf = function(toc, queries) { + fzf = function(toc, queries, ..., score_matrix = score_matrix) { query_chars_list <- split_chars(queries) score <- score_matrix(toc$Package, query_chars_list, extra_bonus = FALSE) topic <- score_matrix(toc$Topic, query_chars_list, extra_bonus = FALSE) @@ -197,7 +197,7 @@ score_toc_filtered <- list( score[right] <- topic[right] return(-colSums(score)) }, - lv = function(toc, queries) { + lv = function(toc, queries, ...) { res <- adist2(toc$Package, queries) topic <- adist2(toc$Topic, queries) right <- res > topic @@ -217,7 +217,7 @@ detect <- function(package, topic, query, case_sensitive) { return(d) } -score_toc <- function(toc, queries, method = c("fzf", "lv")) { +score_toc <- function(toc, queries, method = c("fzf", "lv"), ...) { n <- nrow(toc) score <- rep(NA_integer_, n) method <- match.arg(method) @@ -247,7 +247,7 @@ score_toc <- function(toc, queries, method = c("fzf", "lv")) { # Calculate and return score for filtered items score[prefilter] <- score_toc_filtered[[method]]( - toc[prefilter, ], unique_queries + toc[prefilter, ], unique_queries, ... ) return(score) } @@ -304,10 +304,16 @@ create_server <- function( helpPort = NULL, rstudioServer = FALSE) { method <- match.arg(method) + toc <- create_toc() + score_matrix2 <- memoise::memoise(score_matrix) function(input, output) { - toc <- create_toc() reactiveQueries <- shiny::reactive(parse_query(input$query)) - reactiveToc <- shiny::reactive(search_toc(toc, reactiveQueries(), method = method)) + reactiveToc <- shiny::reactive(search_toc( + toc, + reactiveQueries(), + method = method, + score_matrix = score_matrix2 + )) reactiveTocViewer <- shiny::reactive(local({ toc_matched <- dplyr::mutate( reactiveToc(), From 19270c252957b3233624713c4274053b2e195e37 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Tue, 7 May 2024 09:05:26 +0900 Subject: [PATCH 2/3] docs(DESCRIPTION): add memoise to Imports --- DESCRIPTION | 1 + 1 file changed, 1 insertion(+) diff --git a/DESCRIPTION b/DESCRIPTION index 9fa23e1..55e3a89 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -31,6 +31,7 @@ Imports: htmltools, magrittr, matrixStats, + memoise, miniUI, reactable, rstudioapi, From 8218ef4aeac102435bde6e3601c3f166a628af35 Mon Sep 17 00:00:00 2001 From: atusy <30277794+atusy@users.noreply.github.com> Date: Tue, 7 May 2024 09:06:07 +0900 Subject: [PATCH 3/3] docs(NEWS): performance improvement of fuzzyhelp() --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 09e9ed9..62dd1f2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ - resize with CSS instead of JS so that resize works properly on Chrome and Firefox - increase height of help viewer - theming TOC to be striped, be dense, and to highlight hovered-row +- Improved performance of `fuzzyhelp()` (#26) # felp 0.5.0