Skip to content

Commit 27c525b

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.
1 parent ff9682e commit 27c525b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Sources/SwiftDriver/IncrementalCompilation/SourceFileDependencyGraph.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,13 @@ 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+
}
230233
let node = try Node(key: key,
231234
fingerprint: fingerprint?.intern(in: internedStringTable),
232235
sequenceNumber: nodeSequenceNumber,
233-
defsIDependUpon: defsNodeDependUpon,
236+
defsIDependUpon: defsIDependUpon,
234237
definitionVsUse: definitionVsUse)
235238
self.key = nil
236239
self.defsNodeDependUpon.removeAll(keepingCapacity: true)

0 commit comments

Comments
 (0)