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

perf(fuzzyhelp): use memoise to improve response #26

Merged
merged 3 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Imports:
htmltools,
magrittr,
matrixStats,
memoise,
miniUI,
reactable,
rstudioapi,
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 12 additions & 6 deletions R/fuzzyhelp.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,15 @@ 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)
right <- score < topic
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
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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(),
Expand Down
Loading