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

cache the 'are we on Windows' test (closes #137) #138

Merged
merged 1 commit into from
Dec 4, 2019

Conversation

eddelbuettel
Copy link
Owner

Poking @wlandau re #137

@wlandau
Copy link
Contributor

wlandau commented Dec 4, 2019

Thanks for the quick patch. The Sys.info() bottleneck is gone, and replicate(1e4, digest(letters, algo = "murmur32")) dropped from 1.17 seconds down to 0.41 seconds on my machine (elapsed).

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

profile <- function(plan) {
  local_dir(dir_create(tempfile()))
  replicate(1e4, digest(letters, algo = "murmur32")) # warmup
  start <- proc.time()
  replicate(1e4, digest(letters, algo = "murmur32"))
  print(proc.time() - start)
  rprof <- "prof.rprof"
  pprof <- "prof.pprof"
  Rprof(filename = rprof)
  replicate(1e4, digest(letters, algo = "murmur32"))
  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()

With fe990a3 (master):

before

With 0c9a3e3 (PR):

after

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

Successfully merging this pull request may close these issues.

2 participants