From h.wickham at gmail.com  Mon Nov 20 19:50:24 2017
From: h.wickham at gmail.com (Hadley Wickham)
Date: Mon, 20 Nov 2017 12:50:24 -0600
Subject: [Rd] Small performance bug in [.Date
Message-ID: <CABdHhvH6iWobpPDdpOWc0oqDASNJbTwgRQp9RaS-Jk_JNDE4dg@mail.gmail.com>

Hi all,

I think there's an unnecessary line in [.Date which has a considerable
impact on performance when subsetting large dates:

x <- Sys.Date() + 1:1e6

microbenchmark::microbenchmark(x[1])
#> Unit: microseconds
#>  expr     min       lq     mean   median       uq      max neval
#>  x[1] 920.651 1039.346 3624.833 2294.404 3786.881 41176.38   100

`[.Date` <- function(x, ..., drop = TRUE) {
    cl <- oldClass(x)
    # class(x) <- NULL
    val <- NextMethod("[")
    class(val) <- cl
    val
}
microbenchmark::microbenchmark(x[1])
#> Unit: microseconds
#>  expr   min     lq     mean median    uq      max neval
#>  x[1] 2.738 3.0225 28.40893  3.269 3.513 2470.068   100

Setting the class of x to NULL is problematic because it forces a
copy, and I'm pretty sure it's unnecessary as NextMethod() does not
consult the class of x, but instead uses .Class.

Hadley

-- 
http://hadley.nz



[Rd] Small performance bug in [.Date
Hadley Wickham