Skip to content

Commit 0becbbc

Browse files
committed
Avoid an Unnecessary Buffer Copy
1 parent 56a5aef commit 0becbbc

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Sources/SwiftDriver/IncrementalCompilation/SourceFileDependencyGraph.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,14 @@ extension SourceFileDependencyGraph {
251251
}
252252

253253
var visitor = Visitor(extractFromSwiftModule: extractFromSwiftModule)
254-
try Bitcode.read(stream: Data(data.contents), using: &visitor)
254+
try data.contents.withUnsafeBytes { buf in
255+
// SAFETY: The bitcode reader does not mutate the data stream we give it.
256+
// FIXME: Let's avoid this altogether and traffic in ByteString/[UInt8]
257+
// if possible. There's no real reason to use `Data` in this API.
258+
let baseAddr = UnsafeMutableRawPointer(mutating: buf.baseAddress!)
259+
let data = Data(bytesNoCopy: baseAddr, count: buf.count, deallocator: .none)
260+
try Bitcode.read(stream: data, using: &visitor)
261+
}
255262
guard let major = visitor.majorVersion,
256263
let minor = visitor.minorVersion,
257264
let versionString = visitor.compilerVersionString else {

Tests/SwiftDriverTests/ModuleDependencyGraphTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ fileprivate struct SourceFileDependencyGraphMocker {
10341034

10351035
private mutating func mock() -> SourceFileDependencyGraph {
10361036
buildNodes()
1037-
return SourceFileDependencyGraph(nodesForTesting: allNodes )
1037+
return SourceFileDependencyGraph(nodesForTesting: allNodes)
10381038
}
10391039

10401040
private mutating func buildNodes() {

0 commit comments

Comments
 (0)