Skip to content

Commit 1fd4250

Browse files
committed
Explicitly copy defsIDependUpon into an appropriately sized array when initializing SourceFileDependencyGraph.Node
Previously, the removeAll call below the initializer would trigger CoW. If the scratch buffer was larger than the count, this would result in an unnecesarily large allocation. In a very large file, the source-file-provides-implementation node may have multiple orders of magnitude more edges compared to any other node in the graph, which could lead to excessive memory usage and a large slowdown deallocating the array storage when tearing down the graph. rdar://95648282
1 parent ff9682e commit 1fd4250

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Sources/SwiftDriver/IncrementalCompilation/SourceFileDependencyGraph.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,14 @@ extension SourceFileDependencyGraph {
227227
private mutating func finalizeNode() throws {
228228
guard let key = key else {return}
229229

230+
var defsIDependUpon = Array(unsafeUninitializedCapacity: defsNodeDependUpon.count) { destinationBuffer, initializedCount in
231+
_ = destinationBuffer.initialize(from: defsNodeDependUpon)
232+
initializedCount = defsNodeDependUpon.count
233+
}
230234
let node = try Node(key: key,
231235
fingerprint: fingerprint?.intern(in: internedStringTable),
232236
sequenceNumber: nodeSequenceNumber,
233-
defsIDependUpon: defsNodeDependUpon,
237+
defsIDependUpon: defsIDependUpon,
234238
definitionVsUse: definitionVsUse)
235239
self.key = nil
236240
self.defsNodeDependUpon.removeAll(keepingCapacity: true)

0 commit comments

Comments
 (0)