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

Improve performance of tags functions #414

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mgirlich
Copy link

Functions like tags$tr() can easily be a little faster.

library(htmltools)

set.seed(1)
n <- 50e3
data <- vroom::gen_character(n)

f <- function(data) {
  for (i in seq_along(data)) {
    tags$tr(data[[i]])
  }
}

bench::mark(f(data))

# before
# # A tibble: 1 × 13
#   expression      min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time
#   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm>
# 1 f(data)       1.37s    1.37s     0.730    41.2KB     27.8     1    38      1.37s

# after
# # A tibble: 1 × 13
#   expression      min   median `itr/sec` mem_alloc `gc/sec` n_itr  n_gc total_time
#   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl> <int> <dbl>   <bch:tm>
# 1 f(data)       830ms    830ms      1.20    30.1KB     30.1     1    25      830ms

@@ -170,7 +170,12 @@ nullOrEmpty <- function(x) {

# Given a vector or list, drop all the NULL or length-0 items in it
dropNullsOrEmpty <- function(x) {
x[!vapply(x, nullOrEmpty, FUN.VALUE=logical(1))]
ns <- lengths(x) == 0
Copy link
Collaborator

@cpsievert cpsievert Oct 16, 2023

Choose a reason for hiding this comment

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

From a quick look, it seems lengths() was added in R 3.2.0, so we'll want to put Depends: R (>= 3.2.0) in the DESCRIPTION (or whatever the appropriate version is)

Also, this change would mean nullOrEmpty no longer needs to exist (so please remove it)

@mgirlich mgirlich mentioned this pull request Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants