r-lib / xml2

Bindings to libxml2
https://xml2.r-lib.org/
Other
220 stars 82 forks source link

Replace `structure()` by `class()` #393

Closed mgirlich closed 1 year ago

mgirlich commented 1 year ago

This gives a small performance improvement, e.g. for xml_find_all(flatten = TRUE)

library(xml2)

path <- xml2_example("cd_catalog.xml")
xml <- read_xml(path)
cds <- xml_find_all(xml, "CD")

find <- function() {
  xml_find_all(cds, "TITLE", flatten = FALSE)
}

bench::mark(
  find()
)

# Before
#> # A tibble: 1 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 find()        400µs    428µs     2165.    30.3KB     10.3

# After
#> # A tibble: 1 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 find()        324µs    360µs     2529.    30.3KB     8.20

Created on 2023-06-01 with reprex v2.0.2

This helps towards #394.

hadley commented 1 year ago

Thanks!