rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
213 stars 67 forks source link

Implement `tag()` in C #416

Open mgirlich opened 9 months ago

mgirlich commented 9 months ago

Supersedes #414.

I think it is worth implementing tag() in C to make it fast. Especially when using tag() directly instead of tags$... this is now quite fast.

library(htmltools)

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

f_tags <- function(data) {
  for (i in seq_along(data)) {
    tags$tr(data[[i]])
  }
}
f_tag <- function(data) {
  for (i in seq_along(data)) {
    tag("tr", list(data[[i]]))
  }
}

bench::mark(
  tags = f_tags(data),
  tag = f_tag(data),
)

# before
# # A tibble: 2 × 13
#   expression      min   median `itr/sec` mem_alloc `gc/sec`
#   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
# 1 tags          1.24s    1.24s     0.810    41.2KB     13.0
# 2 tag        637.34ms 637.34ms     1.57     15.2KB     14.1

# after
# # A tibble: 2 × 13
#   expression      min  median `itr/sec` mem_alloc `gc/sec` n_itr
#   <bch:expr> <bch:tm> <bch:t>     <dbl> <bch:byt>    <dbl> <int>
# 1 tags        426.8ms   441ms      2.27    15.7KB     29.5     2
# 2 tag          89.8ms    91ms     10.8     15.2KB     43.3     6