Skip to content

Commit 125b7d9

Browse files
authored
Merge pull request #462 from CodaFi/a-serial-case
Serialize Fingerprint Blobs For External Dependencies
2 parents b5eba93 + e10d9b9 commit 125b7d9

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

Sources/SwiftDriver/IncrementalCompilation/DependencyKey.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import Foundation
44
/// A filename from another module
55
/*@_spi(Testing)*/ public struct ExternalDependency: Hashable, Comparable, CustomStringConvertible {
66
let fileName: String
7+
let fingerprint: String?
8+
9+
/*@_spi(Testing)*/ public init(_ fileName: String, fingerprint: String? = nil) {
10+
self.fileName = fileName
11+
self.fingerprint = fingerprint
12+
}
713

814
var file: VirtualPath? {
915
try? VirtualPath(path: fileName)
1016
}
11-
/*@_spi(Testing)*/ public init(_ path: String) {
12-
self.fileName = path
13-
}
17+
1418
public var description: String {
1519
fileName.description
1620
}

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraph.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,17 @@ extension ModuleDependencyGraph {
561561
}
562562
self.nodeUses[key, default: []].append(Int(record.fields[0]))
563563
case .externalDepNode:
564-
guard record.fields.count == 1,
565-
record.fields[0] < identifiers.count
564+
guard record.fields.count == 2,
565+
record.fields[0] < identifiers.count,
566+
case .blob(let fingerprintBlob) = record.payload,
567+
let fingerprintStr = String(data: fingerprintBlob, encoding: .utf8)
566568
else {
567569
throw ReadError.malformedExternalDepNodeRecord
568570
}
569571
let path = identifiers[Int(record.fields[0])]
570-
self.graph.externalDependencies.insert(ExternalDependency(path))
572+
let hasFingerprint = Int(record.fields[1]) != 0
573+
let fingerprint = hasFingerprint ? fingerprintStr : nil
574+
self.graph.externalDependencies.insert(ExternalDependency(path, fingerprint: fingerprint))
571575
case .identifierNode:
572576
guard record.fields.count == 0,
573577
case .blob(let identifierBlob) = record.payload,
@@ -804,6 +808,10 @@ extension ModuleDependencyGraph {
804808
.literal(RecordID.externalDepNode.rawValue),
805809
// path ID
806810
.vbr(chunkBitWidth: 13),
811+
// fingerprint?
812+
.fixed(bitWidth: 1),
813+
// fingerprint bytes
814+
.blob
807815
])
808816
self.abbreviate(.identifierNode, [
809817
.literal(RecordID.identifierNode.rawValue),
@@ -874,10 +882,11 @@ extension ModuleDependencyGraph {
874882
}
875883

876884
for dep in graph.externalDependencies {
877-
serializer.stream.writeRecord(serializer.abbreviations[.externalDepNode]!) {
885+
serializer.stream.writeRecord(serializer.abbreviations[.externalDepNode]!, {
878886
$0.append(RecordID.externalDepNode)
879887
$0.append(serializer.lookupIdentifierCode(for: dep.fileName))
880-
}
888+
$0.append((dep.fingerprint != nil) ? UInt32(1) : UInt32(0))
889+
}, blob: dep.fingerprint ?? "")
881890
}
882891
}
883892
return ByteString(serializer.stream.data)

Tests/SwiftDriverTests/DependencyGraphSerializationTests.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class DependencyGraphSerializationTests: XCTestCase {
4949
var commands: [LoadCommand]
5050

5151
enum LoadCommand {
52-
case load(index: Int, nodes: [MockDependencyKind: [String]])
52+
case load(index: Int, nodes: [MockDependencyKind: [String]], fingerprint: String? = nil)
5353
case reload(index: Int, nodes: [MockDependencyKind: [String]], fingerprint: String? = nil)
5454
}
5555
}
@@ -212,15 +212,19 @@ class DependencyGraphSerializationTests: XCTestCase {
212212
.load(index: 6, nodes: [.nominal: ["C2->"]]),
213213
.reload(index: 0, nodes: [.nominal: ["A1@11", "A2@2"]])
214214
]),
215+
GraphFixture(commands: [
216+
.load(index: 0, nodes: [.externalDepend: ["/foo->", "/bar->"]], fingerprint: "ABCDEFG"),
217+
.reload(index: 0, nodes: [.externalDepend: ["/foo->", "/bar->"]], fingerprint: "HIJKLMNOP"),
218+
]),
215219
]
216220

217221
for fixture in fixtures {
218222
let de = DiagnosticsEngine()
219223
let graph = ModuleDependencyGraph(mock: de)
220224
for loadCommand in fixture.commands {
221225
switch loadCommand {
222-
case .load(index: let index, nodes: let nodes):
223-
graph.simulateLoad(index, nodes, nil)
226+
case .load(index: let index, nodes: let nodes, fingerprint: let fingerprint):
227+
graph.simulateLoad(index, nodes, fingerprint)
224228
case .reload(index: let index, nodes: let nodes, fingerprint: let fingerprint):
225229
_ = graph.simulateReload(index, nodes, fingerprint)
226230
}

0 commit comments

Comments
 (0)