Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as_mapper.default() is relatively slow #1088

Open
mgirlich opened this issue Jul 5, 2023 · 1 comment
Open

as_mapper.default() is relatively slow #1088

mgirlich opened this issue Jul 5, 2023 · 1 comment

Comments

@mgirlich
Copy link
Contributor

mgirlich commented Jul 5, 2023

Nearly half the time in this example is spend in as_mapper.default(). The issue i

x <- as.list(1:20)
x[21] <- list(NULL)

for (i in 1:10e3) {
  purrr::detect_index(x, is.null)
}

image

This leads to the curious situation that it is faster to manually wrap is.null() yourself in a function:

x <- as.list(1:20)
x[21] <- list(NULL)

bench::mark(
  wrapped = purrr::detect_index(x, function(x) is.null(x)),
  prim = purrr::detect_index(x, is.null)
)
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 wrapped        61µs   66.1µs    13236.     446KB     16.6
#> 2 prim          129µs  145.4µs     6387.     232KB     14.6

Created on 2023-07-05 with reprex v2.0.2

@hadley
Copy link
Member

hadley commented Jul 26, 2023

Hard to see how to fix without breaking this test:

test_that("primitive functions are wrapped", {
  expect_identical(as_mapper(`-`)(.y = 10, .x = 5), -5)
  expect_identical(as_mapper(`c`)(1, 3, 5), c(1, 3, 5))
})

But maybe that's ok since we don't generally expect - to respect named arguments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants