randy3k / collections

High-performance container datatypes for R
https://randy3k.github.io/collections
Other
103 stars 3 forks source link

Add `mpush()` and `mpop()` #21

Open AshesITR opened 1 year ago

AshesITR commented 1 year ago

Having performant implementations of mpush() and mpop() as similar to what fastmap::faststack() offers would be very nice.

Here are slow "reference implementations"

tmp1 <- collections::stack()
tmp2 <- fastmap::faststack()
tmp1$mpush <- function(items) {
  for (item in items) {
    self$push(item)
  }
}

tmp1$mpop <- function(n) {
  replicate(n, self$pop(), simplify = FALSE)
}
environment(tmp1$mpop) <- tmp1
environment(tmp1$mpush) <- tmp1

bench::mark(
  collections = {
    tmp1$mpush(seq_len(1000))
    tmp1$mpop(500)
  },
  fastmap = {
    tmp2$mpush(.list = seq_len(1000))
    tmp2$mpop(500)
  }
)
#> # A tibble: 2 × 6
#>   expression       min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>  <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 collections  834.6µs  943.7µs     1036.    10.2KB     35.9
#> 2 fastmap       33.2µs   37.1µs     5446.    33.9KB     14.1

Created on 2022-12-12 with reprex v2.0.2