Skip to content

Session startup slow with many installed packages on network filesystems — redundant package library scans #17392

Closed

Description

@zachhannum

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

  1. Mount a network filesystem (NFS or similar) as an R library path
  2. Install 1000+ R packages into that library
  3. Start an RStudio session
  4. 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 readdir per library path (returns 1000+ entries)
  • N stat calls (one per package directory, for metadata)
  • Additional open/read/close per DESCRIPTION or Meta/package.rds file

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 scheduleIncrementalWork with 300ms intervals, but beginIndexing() 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: false in the session config to skip pane population
  • RSTUDIO_DISABLE_PACKAGES=1 environment variable (disables packages module entirely)

These are heavy-handed — they disable useful functionality rather than making it performant.

Activity

  1. added
    new
    on Apr 10, 2026
  2. added theissue type on Apr 10, 2026
  3. removed
    new
    on Apr 15, 2026
  4. added this to the Golden Wattle milestone on Apr 22, 2026
  5. zachhannum commented on May 5, 2026

    @zachhannum
    ContributorAuthor

    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:

    • "Scan 4" (availablePackagesUpdated) isn't a startup scan at all. It's the handler for the "Check for Updates" button.
    • Walk 1 (updatePackageEvents) is mostly cheap. It does a list.dirs plus setHook calls; the filesystem hit is small, and the setHooks are in-memory.
    • Walk 3 (PPE indexer) is expensive, but it's been out of scope for this issue from the start, and there's no shared data shape with Walk 2 that would let one feed the other.

    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.rds opens in the second pass alone, all redoing the first pass.

    The second fire is intentional. DeferredInitCompletedEvent runs after .Rprofile, kSessionInitHook callbacks, and afterSessionInitHook, and any of those can mutate package state. There's a 2-second timer in Packages.java:197-203 from 2014 that was meant to dedupe on first load, and it does work on fast filesystems: DeferredInitCompleted fires 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-.Rprofile refresh path entirely. Packages loaded during .Rprofile won't show as loaded until the next enquePackageStateChanged trigger, which fires on the first console prompt anyway.

    The R side: cache the parsed result of listInstalledPackages for one hit, then drop it. Recompute loaded/attached from loadedNamespaces() and search() on the cache hit so post-.Rprofile state is reflected. About 30 lines of R, no behavior change on fast filesystems. The narrow correctness gap is install.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_PATH walking on subprocess startup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions