Repository navigation
Description
Problem
When a user has 1000+ R packages installed on a network filesystem (NFS, CIFS, etc.), RStudio session startup is significantly slower than expected. Part of the problem is that three independent code paths each enumerate the full package library directory at startup, with no data sharing or caching between them. This produces tens of thousands of redundant filesystem operations, each subject to network round-trip latency.
Reproduction
- Mount a network filesystem (NFS or similar) as an R library path
- Install 1000+ R packages into that library
- Start an RStudio session
- Observe startup takes 25+ seconds, with most time spent in filesystem operations
Code paths that scan the library
1. updatePackageEvents() — hook registration scan
File: src/cpp/session/modules/SessionPackages.R:1091
pkgNames <- base::list.dirs(.libPaths(), full.names = FALSE, recursive = FALSE)
sapply(pkgNames, function(packageName) {
# registers attach/load/unload hooks per package ...
})This enumerates every directory in every library path to register package event hooks. With 1000+ packages, it registers 3000+ hooks (attach + onLoad + onUnload per package).
2. listInstalledPackages() — packages pane population
File: src/cpp/session/modules/SessionPackages.R:1434
pkgPaths <- list.files(.rs.uniqueLibraryPaths(), full.names = TRUE)
hasMeta <- file.exists(file.path(pkgPaths, "Meta"))
pkgPaths <- pkgPaths[hasMeta]This re-enumerates the same directories, then does a file.exists() check and reads DESCRIPTION metadata for every package. Each file.exists() is an additional stat syscall over the network.
3. PackageProvidedExtension::Indexer::beginIndexing() — extension indexing
File: src/cpp/session/modules/SessionPackageProvidedExtension.cpp:131
std::vector<core::FilePath> libPaths = module_context::getLibPaths();
for (const core::FilePath& libPath : libPaths) {
std::vector<core::FilePath> pkgPaths;
core::Error error = libPath.getChildren(pkgPaths);
// ...
}A third independent enumeration of the same library paths from C++, to index package-provided extensions (e.g., RStudio addins, connection types).
4. availablePackagesUpdated() — update check scan (bonus)
File: src/cpp/session/modules/SessionPackages.R:176
installedPkgsList <- lapply(.libPaths(), function(libPath) {
pkgPaths <- list.files(libPath, full.names = TRUE)
# reads DESCRIPTION per package ...
})Yet another enumeration for checking available updates.
Impact analysis
Each library scan on a network filesystem produces:
- 1
readdirper library path (returns 1000+ entries) - N
statcalls (one per package directory, for metadata) - Additional
open/read/closeper DESCRIPTION orMeta/package.rdsfile
With 3-4 scans and no caching, this is O(n × passes × network_latency). On a network FS with 2ms round-trip, 1000 packages across 4 passes can easily exceed 25 seconds of wall-clock time just for directory enumeration.
Proposed improvements
Short-term: cache the directory listing
Introduce a session-scoped cache that stores the result of the first library path enumeration and reuses it for subsequent consumers within the same startup sequence. This would reduce 3-4 full directory scans to 1.
Medium-term: consolidate scans into a single pass
Refactor startup so that a single enumeration populates a shared data structure consumed by hook registration, package listing, and extension indexing.
Medium-term: defer or lazy-load non-critical scans
- Hook registration (
updatePackageEvents): Consider registering hooks lazily (on first attach/load) rather than pre-registering for every installed package - Extension indexing: Already uses
scheduleIncrementalWorkwith 300ms intervals, butbeginIndexing()still does the full directory scan synchronously — consider deferring the directory enumeration as well
Medium-term: make packages pane conditional
If the Packages pane is not visible at startup (e.g., user has a layout without it), skip listInstalledPackages() entirely until the pane is first shown.
Current workarounds
Users can mitigate with:
packages_pane_enabled: falsein the session config to skip pane populationRSTUDIO_DISABLE_PACKAGES=1environment variable (disables packages module entirely)
These are heavy-handed — they disable useful functionality rather than making it performant.
Activity
zachhannum commentedon May 5, 2026
While planning a fix for this issue, continued to dig into the support ticket this originated from (~1300 packages on NFS, EasyBuild + Slurm). The strace evidence reframed a lot of what I wrote in the original issue.
Most of what is called "redundant scans" isn't actually redundant or isn't actually a startup scan:
availablePackagesUpdated) isn't a startup scan at all. It's the handler for the "Check for Updates" button.updatePackageEvents) is mostly cheap. It does alist.dirsplussetHookcalls; the filesystem hit is small, and the setHooks are in-memory.What does show up clearly in the strace is something I hadn't called out: Walk 2 fires twice at startup, about two seconds apart, doing the same per-package DESCRIPTION reads both times. On the customer's slow run that's roughly 5,500
Meta/package.rdsopens in the second pass alone, all redoing the first pass.The second fire is intentional.
DeferredInitCompletedEventruns after.Rprofile,kSessionInitHookcallbacks, andafterSessionInitHook, and any of those can mutate package state. There's a 2-second timer inPackages.java:197-203from 2014 that was meant to dedupe on first load, and it does work on fast filesystems:DeferredInitCompletedfires before the handler is registered, so the second call just doesn't happen. On slow filesystems the timing inverts, the handler is in place when the event fires, and you get two scans.Two ways to fix it:
The GWT side: replace the 2-second timer with a proper "skip if the constructor's call already completed" check. Small diff, no R-side state. The trade-off is you lose the post-
.Rprofilerefresh path entirely. Packages loaded during.Rprofilewon't show as loaded until the nextenquePackageStateChangedtrigger, which fires on the first console prompt anyway.The R side: cache the parsed result of
listInstalledPackagesfor one hit, then drop it. Recomputeloaded/attachedfromloadedNamespaces()andsearch()on the cache hit so post-.Rprofilestate is reflected. About 30 lines of R, no behavior change on fast filesystems. The narrow correctness gap isinstall.packages()during.Rprofile.Either is a small, targeted fix similar to #17457. The wall-clock saving scales with package count and filesystem latency, so it's meaningful on NFS and negligible locally. I'd want to actually measure before quoting a number.
One caveat worth flagging: this won't move the customer's headline complaint. Their ~40s startup is dominated by NFS module load, and EasyBuild
LD_LIBRARY_PATHwalking on subprocess startup.