← Back to Pull requests

[VirtualPath] Shard the path cache so drivers in one process don't serialize #2220

Closed
unknown closed this pull request without merging on Oct 13, 2026 08:24 UTC
View on GitHub
unknown unknown commented on Oct 05, 2026 08:13 UTC
Author

Fixes #2219.

Motivation

VirtualPath.PathCache is process-global. Every intern took a barrier on its concurrent queue, including when the path is already cached, and validated new paths while holding it. When many drivers run in one process, as when Swift Build plans every module in-process, they intern through this one barrier and planning runs almost one module at a time. In a 375-module iOS app, planners spent 84–86% of their time waiting on this queue after a change to a low-level module. The issue has the measurements and stack traces, including a reproduction on Xcode 27.1 beta.

Change

The cache is split into 32 shards, each with its own NSLock:

  • A path lives in the shard of its canonical cache key, and a handle encodes the shard and the slot within it.

  • A non-canonical spelling (for example /a/./b) is recorded as an alias in its own shard and points at the canonical path's handle, as before.

  • Paths are validated without holding any lock. An insert re-checks the shard under its lock, so concurrent interning of the same new path always returns one handle.

  • Resolving a cached path takes one short lock instead of a dispatch round trip; interning a new path from a string takes two (three for a new alias spelling). Different paths no longer contend.

  • pathCache is a static let. As a static var, every access retained and released the shared cache object and checked for exclusive access, so concurrent lookups contended on its reference count. temporaryFileStore is a static let too, since it is never reassigned either.

  • The shard locks use the NSLock.withLock helper, moved from ArgsResolver.swift into Utilities/NSLockAdditions.swift.

  • Why 32 shards: in a sweep from 1 to 128 shards (run with an earlier, shorter benchmark path root), more shards sped up concurrent inserts of new paths (0.40 s with a single shard, which is one NSLock rather than the old barrier queue, and 0.13 s with 32), but beyond 8 shards they slowed down concurrent lookups that hit one hot path. In the iOS app's Xcode build, planners waited on the shard locks for 12% of their time with 8 shards and 5% with 32 (sampled, one run each), so the real workload favors more shards.
  • Behavior is unchanged: the same strings map to the same VirtualPath, and a handle keeps resolving to its path. Handle values now depend on the shard, but they are only meaningful within a process: VirtualPath.Handle encodes as the path it names, never as its number.

    Performance

    Release build on top of main, median of three interleaved runs of 10 iterations each (VirtualPathPerformanceTests, added here):

    | Benchmark | Before | After |
    | --- | --- | --- |
    | 1 thread interning 100,000 new paths | 0.58 s | 0.40 s |
    | 16 threads interning 160,000 new, distinct paths | 1.45 s | 0.17 s |
    | 16 threads interning cached paths, a quarter of them one hot path | 2.36 s | 0.21 s |
    | 16 threads resolving handles, a quarter of them one hot path | 0.97 s | 0.10 s |

    In the iOS app, with this change (before the review follow-ups) applied to swift-6.3.3-RELEASE and the resulting library loaded into Xcode 26.6's build service, the median "Planning Swift module" step after the low-level change went from 11.6–18.5 s (two stock runs) to 2.2–3.1 s (two runs). Wall time went from 390 s (one stock run) to 290–331 s (two runs); single runs vary by around 40 s.

    Xcode's build timeline for the same change, with the "Planning Swift module" steps highlighted. With the stock driver (390 s build), Planning steps fill every lane for minutes:

    <img width="1799" height="487" alt="stock_driver" src="https://github.com/user-attachments/assets/47d93fc5-4ab0-41a8-a9e8-0a4400b14b0c" />

    With this change (290 s build), Planning is a thin sliver and the lanes are full of compile jobs:

    <img width="1800" height="496" alt="patched_driver" src="https://github.com/user-attachments/assets/2bf72063-d49e-4cca-94ee-f59869b5a8d9" />

    Testing

  • VirtualPathTests.concurrentInterningVendsOneHandlePerPath: 16 threads intern the same new paths, in opposite orders, through both interning entry points: VirtualPath.intern(path:) with a canonical and a non-canonical spelling, and intern() on a VirtualPath value. On Windows the non-canonical spelling is skipped, because path validation there may not collapse . components (not verified on a Windows machine). It checks that all threads get identical handles, that aliases resolve to the canonical handle, and that distinct paths never share one. It fails if the re-check under the shard lock is removed, and it passes under Thread Sanitizer.

  • The full test suite passes on macOS with Xcode 26.6's toolchain.

  • The benchmarks are opt-in, because their timings are noisy on shared CI machines and in parallel test runs. Run them with swift test -c release -Xswiftc -DSWIFT_DRIVER_ENABLE_BENCHMARKS --filter VirtualPathPerformanceTests. With the condition set, debug builds run a small size just to check that they work.
  • Assisted-by: Claude

    Diff: 627 lines (+470 / -157) across 7 files Reviewer: HeEAaD Review duration: 11530m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 627 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.385
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #2220"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.3% (MEDIUM) · Higher = larger blast radius
    50.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.9% · Safe to merge
    19.9%