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

Faster alternative to match.arg() in digest() #139

Closed
wlandau opened this issue Dec 4, 2019 · 4 comments
Closed

Faster alternative to match.arg() in digest() #139

wlandau opened this issue Dec 4, 2019 · 4 comments

Comments

@wlandau
Copy link
Contributor

wlandau commented Dec 4, 2019

As with Sys.info() in #137, digest() spends a noticeable amount of time on match.arg() (see the flame graphs in #138). Would it be reasonable to explore cheaper alternatives to partial argument matching for algo and errormode?

@eddelbuettel
Copy link
Owner

eddelbuettel commented Dec 4, 2019

🤷‍♂️

Hard to tell if it matters or if we go off into obscure territory. It is a base R function so there is probably not that much fluff.

Also note that we now have a vectorized caller -- so maybe look into getVDigest()? And or if you're so pressed for time maybe call .Call(digest_impl) directly with properly set up arguments?

(Also: why does the flamegraph not show actual digesting? Am I missing something or misreading?)

@wlandau
Copy link
Contributor Author

wlandau commented Dec 4, 2019

Also note that we now have a vectorized caller -- so maybe look into getVDigest()? And or if you're so pressed for time maybe call .Call(digest_impl) directly with properly set up arguments?

I will look into it, thanks.

@wlandau
Copy link
Contributor Author

wlandau commented Dec 4, 2019

getVDigest() circumvents this exact problem!

library(digest)
library(fs)
library(profile)
library(withr)

profile <- function(plan) {
  local_dir(dir_create(tempfile()))
  f <- getVDigest(algo = "murmur32")
  replicate(1e4, f(letters)) # warmup
  start <- proc.time()
  replicate(1e4, f(letters))
  print(proc.time() - start)
  rprof <- "prof.rprof"
  pprof <- "prof.pprof"
  Rprof(filename = rprof)
  replicate(1e4, f(letters))
  Rprof(NULL)
  data <- read_rprof(rprof)
  write_pprof(data, pprof)
  vis_pprof(pprof)
}

vis_pprof <- function(path, host = "localhost", port = NULL) {
  server <- sprintf("%s:%s", host, port %||% random_port())
  message("local pprof server: http://", server)
  args <- c("-http", server, path)
  if (on_windows()) {
    shell(paste(c("pprof", args), collapse = " "))
  } else {
    system2(jointprof::find_pprof(), args)
  }
}

random_port <- function(from = 49152L, to = 65355L) {
  sample(seq.int(from = from, to = to, by = 1L), size = 1L)
}

on_windows <- function() {
  tolower(Sys.info()["sysname"]) == "windows"
}

`%||%` <- function(x, y) {
  if (is.null(x) || length(x) <= 0) {
    y
  } else {
    x
  }
}

profile()

vec

@wlandau wlandau closed this as completed Dec 4, 2019
@eddelbuettel
Copy link
Owner

Yay!

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

No branches or pull requests

2 participants