Skip to content

Commit

Permalink
Cache default_ua on package load
Browse files Browse the repository at this point in the history
The default_ua function is called on every request and involves
calls to the slow base function packageDescription.  Depending on the
use, this can account for up to 40% of the client-side CPU time which
is a lot when using over a loopback connection (e.g. etcd, RNeo4j).

Because this information is unlikely to change during a session (and is
infomative for the server and likely ignored anyway) computing it once
and reusing it seems safe).

By @richfitz, closes #322.
  • Loading branch information
hadley committed May 24, 2016
1 parent a1a7651 commit 3e3e3ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# httr 1.1.0.9000

* The default user agent string is now computed once and cached. This
is a small performance improvement, but important for local connections
(#322, @richfitz).

* New `encode = "raw"` allows you to do your own encoding for requests with
bodies.

Expand Down
17 changes: 11 additions & 6 deletions R/config.r
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,18 @@ curl_docs <- function(x) {
BROWSE(url)
}

cache <- new.env(parent = emptyenv())
cache$default_ua <- NULL

default_ua <- function() {
versions <- c(
libcurl = curl::curl_version()$version,
`r-curl` = as.character(utils::packageVersion("curl")),
httr = as.character(utils::packageVersion("httr"))
)
paste0(names(versions), "/", versions, collapse = " ")
if (is.null(cache$default_ua)) {
versions <- c(
libcurl = curl::curl_version()$version,
`r-curl` = as.character(utils::packageVersion("curl")),
httr = as.character(utils::packageVersion("httr")))
cache$default_ua <- paste0(names(versions), "/", versions, collapse = " ")
}
cache$default_ua
}

#' Set (and reset) global httr configuration.
Expand Down

0 comments on commit 3e3e3ae

Please sign in to comment.