Thank you for the response. I had a feeling that it would come down to re-reading the document, but happily, it doesn't seem to have a significant increase in the amount of time needed to render the document and oddly reduces the memory footprint (example shown on the {dplyr} NEWS file). The only significant cost appears to be the fact that we now need to re-assign the variable.
f <- tempfile()
download.file("https://raw.githubusercontent.com/tidyverse/dplyr/master/NEWS.md", f)
library(xml2)
library(commonmark)
reread <- function(d) {
xml_add_child(d, "code_block", "1 + 1\n")
d2 <- read_xml(as.character(d))
xml_name(xml_find_all(d2, "d1:code_block"))
}
default <- function(d) {
xml_add_child(d, "code_block", "1 + 1\n", xmlns = xml_ns(d)[[1]])
xml_name(xml_find_all(d, "d1:code_block"))
}
dx <- read_xml(markdown_xml(f))
dy <- read_xml(markdown_xml(f))
bench::mark(default(dx), reread(dy))
#> # A tibble: 2 x 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 default(dx) 293µs 1.75ms 548. 164.15KB 97.2
#> 2 reread(dy) 284µs 1.89ms 503. 7.81KB 19.7
Created on 2020-10-19 by the reprex package (v0.3.0)