From f657561d6122c3f24c4c1a95473e88080e49cfc2 Mon Sep 17 00:00:00 2001 From: eitsupi Date: Sat, 31 May 2025 06:36:33 +0000 Subject: [PATCH] Speed up `is_installed()` - shortcut when package is already loaded --- R/session.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/R/session.R b/R/session.R index 62c822c31..2a29550e8 100644 --- a/R/session.R +++ b/R/session.R @@ -63,9 +63,21 @@ is_installed <- function(pkg, ..., version = NULL, compare = NULL) { check_dots_empty0(...) + # Fast path. Note that if `version` is supplied, this always falls through to the + # more expensive check. + if (is.null(version)) { + loaded <- lapply(pkg, function(x) { + is.character(x) && nzchar(x) && is.environment(.getNamespace(x)) + }) + if (all(as.logical(loaded))) { + return(TRUE) + } + } + info <- pkg_version_info(pkg, version = version, compare = compare) all(detect_installed(info)) } + detect_installed <- function(info) { # Internal mechanism for unit tests hook <- peek_option("rlang:::is_installed_hook")