[VirtualPath] Shard the path cache so drivers in one process don't serialize #2220
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
HeEAaD
Fixes #2219.
Motivation
VirtualPath.PathCacheis process-global. Everyinterntook 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/./b) is recorded as an alias in its own shard and points at the canonical path's handle, as before.pathCacheis astatic let. As astatic var, every access retained and released the shared cache object and checked for exclusive access, so concurrent lookups contended on its reference count.temporaryFileStoreis astatic lettoo, since it is never reassigned either.NSLock.withLockhelper, moved fromArgsResolver.swiftintoUtilities/NSLockAdditions.swift.NSLockrather 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.Handleencodes 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-RELEASEand 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, andintern()on aVirtualPathvalue. 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.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