Skip to content

Commit d285529

Browse files
author
David Ungar
committed
Fix the easy stuff.
1 parent ba887d1 commit d285529

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraph.swift

+23-11
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ extension ModuleDependencyGraph {
762762

763763
func stringIndex(field i: Int) throws -> Int {
764764
let u = record.fields[i]
765-
guard u < UInt64(internedStringTable.endIndex) else {
765+
guard u < UInt64(internedStringTable.count) else {
766766
throw malformedError
767767
}
768768
return Int(u)
@@ -774,18 +774,18 @@ extension ModuleDependencyGraph {
774774
let s = try internedString(field: i)
775775
return s.isEmpty ? nil : s
776776
}
777-
func dependencyKey(fields: (kindCode: Int,
778-
declAspect: Int,
779-
context: Int,
780-
identifier: Int)
777+
func dependencyKey(kindCodeField: Int,
778+
declAspectField: Int,
779+
contextField: Int,
780+
identifierField: Int
781781
) throws -> DependencyKey {
782-
let kindCode = record.fields[fields.kindCode]
783-
guard let declAspect = DependencyKey.DeclAspect(record.fields[fields.declAspect])
782+
let kindCode = record.fields[kindCodeField]
783+
guard let declAspect = DependencyKey.DeclAspect(record.fields[declAspectField])
784784
else {
785785
throw malformedError
786786
}
787-
let context = try internedString(field: fields.context)
788-
let identifier = try internedString(field: fields.identifier)
787+
let context = try internedString(field: contextField)
788+
let identifier = try internedString(field: identifierField)
789789
let designator = try DependencyKey.Designator(
790790
kindCode: kindCode, context: context, name: identifier,
791791
internedStringTable: internedStringTable, fileSystem: fileSystem)
@@ -804,13 +804,18 @@ extension ModuleDependencyGraph {
804804

805805
self.majorVersion = record.fields[0]
806806
self.minorVersion = record.fields[1]
807+
let stringCount = record.fields[2]
808+
internedStringTable.reserveCapacity(Int(stringCount))
807809
self.compilerVersionString = String(decoding: compilerVersionBlob, as: UTF8.self)
808810
case .moduleDepGraphNode:
809811
guard record.fields.count == 6
810812
else {
811813
throw malformedError
812814
}
813-
let key = try dependencyKey(fields: (0, 1, 2, 3))
815+
let key = try dependencyKey(kindCodeField: 0,
816+
declAspectField: 1,
817+
contextField: 2,
818+
identifierField: 3)
814819
let depSourceFileOrNone = try nonemptyInternedString(field: 4)
815820
let depSource = try depSourceFileOrNone.map {
816821
internedFile -> DependencySource in
@@ -832,7 +837,11 @@ extension ModuleDependencyGraph {
832837
else {
833838
throw malformedError
834839
}
835-
self.currentDefKey = try dependencyKey(fields: (0, 1, 2, 3))
840+
self.currentDefKey = try dependencyKey(
841+
kindCodeField: 0,
842+
declAspectField: 1,
843+
contextField: 2,
844+
identifierField: 3)
836845
case .useIDNode:
837846
guard let key = self.currentDefKey,
838847
record.fields.count == 1 else {
@@ -1024,6 +1033,7 @@ extension ModuleDependencyGraph {
10241033
$0.append(RecordID.metadata)
10251034
$0.append(serializedGraphVersion.majorForWriting)
10261035
$0.append(serializedGraphVersion.minorForWriting)
1036+
$0.append(min(UInt(internedStringTable.count), UInt(UInt32.max)))
10271037
},
10281038
blob: self.compilerVersion)
10291039
}
@@ -1067,6 +1077,8 @@ extension ModuleDependencyGraph {
10671077
.fixed(bitWidth: 16),
10681078
// Minor version
10691079
.fixed(bitWidth: 16),
1080+
// Number of strings to be interned
1081+
.fixed(bitWidth: 32),
10701082
// Frontend version
10711083
.blob,
10721084
])

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraphParts/InternedStrings.swift

+10-12
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
import Foundation
14-
1513
public protocol InternedStringTableHolder {
1614
var internedStringTable: InternedStringTable {get}
1715
}
@@ -21,7 +19,7 @@ public struct InternedString: CustomStringConvertible, Equatable, Hashable {
2119
let index: Int
2220

2321
fileprivate init(_ s: String, _ table: InternedStringTable) {
24-
self.init(index: s.isEmpty ? 0 : table.index(s))
22+
self.init(index: s.isEmpty ? 0 : table.intern(s))
2523
}
2624

2725
public var isEmpty: Bool { index == 0 }
@@ -37,7 +35,7 @@ public struct InternedString: CustomStringConvertible, Equatable, Hashable {
3735
return r
3836
}
3937

40-
public func lookup(`in` holder: InternedStringTableHolder) -> String {
38+
public func lookup(in holder: InternedStringTableHolder) -> String {
4139
holder.internedStringTable.strings[index]
4240
}
4341

@@ -56,28 +54,28 @@ public func isInIncreasingOrder(
5654
lhs.lookup(in: holder) < rhs.lookup(in: holder)
5755
}
5856

59-
extension InternedString {
60-
public static func < (lhs: InternedString, rhs: InternedString) -> Bool {
61-
lhs.index < rhs.index
62-
}
63-
}
64-
6557
/// Hardcode empty as 0
6658
public class InternedStringTable {
6759
var strings = [""]
6860
fileprivate var indices = ["": 0]
6961

7062
public init() {}
7163

72-
fileprivate func index(_ s: String) -> Int {
64+
fileprivate func intern(_ s: String) -> Int {
7365
if let i = indices[s] { return i }
7466
let i = strings.count
7567
strings.append(s)
7668
indices[s] = i
7769
return i
7870
}
7971

80-
var endIndex: Int { strings.count }
72+
var count: Int { strings.count }
73+
74+
func reserveCapacity(_ minimumCapacity: Int) {
75+
strings.reserveCapacity(minimumCapacity)
76+
indices.reserveCapacity(minimumCapacity)
77+
}
78+
8179
}
8280

8381
extension InternedStringTable: InternedStringTableHolder {

0 commit comments

Comments
 (0)