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

Speed up check_installed() - shortcut when package is already loaded #1780

Merged
merged 4 commits into from
Feb 25, 2025

Conversation

shikokuchuo
Copy link
Member

Can safely shortcut when package versions are not specified.

This prevents the full code path being run every time the function is called within a session, and is equivalent to the user memoizing the result.

Takes 3 microseconds, which is not much to give back on the initial run when we've saved some hundreds of microseconds in #1777.

library(rlang)
bench::mark(check_installed("later"))
#> # A tibble: 1 × 6
#>   expression                        min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                   <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 "check_installed(\"later\")"   2.47µs   3.01µs   305568.    5.61MB     30.6

Created on 2025-02-20 with reprex v2.1.1

This should be applied with #1777 but is independent of that.
Closes #1776.

@@ -201,6 +201,16 @@ check_installed <- function(pkg,
action = NULL,
call = caller_env()) {
check_dots_empty0(...)

if (is.null(version)) {
loaded <- lapply(pkg, function(x) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a comment about what happens when a version is supplied?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in c65352f.

@lionel- lionel- merged commit ff850cc into r-lib:main Feb 25, 2025
10 of 11 checks passed
@shikokuchuo shikokuchuo deleted the shortcut branch February 25, 2025 12:13
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.

Propose a faster version of check_installed()
2 participants