Skip to content

Commit e4047fb

Browse files
author
David Ungar
committed
Fix the easy stuff.
1 parent 9e3e6b2 commit e4047fb

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
@@ -763,7 +763,7 @@ extension ModuleDependencyGraph {
763763

764764
func stringIndex(field i: Int) throws -> Int {
765765
let u = record.fields[i]
766-
guard u < UInt64(internedStringTable.endIndex) else {
766+
guard u < UInt64(internedStringTable.count) else {
767767
throw malformedError
768768
}
769769
return Int(u)
@@ -775,18 +775,18 @@ extension ModuleDependencyGraph {
775775
let s = try internedString(field: i)
776776
return s.isEmpty ? nil : s
777777
}
778-
func dependencyKey(fields: (kindCode: Int,
779-
declAspect: Int,
780-
context: Int,
781-
identifier: Int)
778+
func dependencyKey(kindCodeField: Int,
779+
declAspectField: Int,
780+
contextField: Int,
781+
identifierField: Int
782782
) throws -> DependencyKey {
783-
let kindCode = record.fields[fields.kindCode]
784-
guard let declAspect = DependencyKey.DeclAspect(record.fields[fields.declAspect])
783+
let kindCode = record.fields[kindCodeField]
784+
guard let declAspect = DependencyKey.DeclAspect(record.fields[declAspectField])
785785
else {
786786
throw malformedError
787787
}
788-
let context = try internedString(field: fields.context)
789-
let identifier = try internedString(field: fields.identifier)
788+
let context = try internedString(field: contextField)
789+
let identifier = try internedString(field: identifierField)
790790
let designator = try DependencyKey.Designator(
791791
kindCode: kindCode, context: context, name: identifier,
792792
internedStringTable: internedStringTable, fileSystem: fileSystem)
@@ -805,13 +805,18 @@ extension ModuleDependencyGraph {
805805

806806
self.majorVersion = record.fields[0]
807807
self.minorVersion = record.fields[1]
808+
let stringCount = record.fields[2]
809+
internedStringTable.reserveCapacity(Int(stringCount))
808810
self.compilerVersionString = String(decoding: compilerVersionBlob, as: UTF8.self)
809811
case .moduleDepGraphNode:
810812
guard record.fields.count == 6
811813
else {
812814
throw malformedError
813815
}
814-
let key = try dependencyKey(fields: (0, 1, 2, 3))
816+
let key = try dependencyKey(kindCodeField: 0,
817+
declAspectField: 1,
818+
contextField: 2,
819+
identifierField: 3)
815820
let depSourceFileOrNone = try nonemptyInternedString(field: 4)
816821
let depSource = try depSourceFileOrNone.map {
817822
internedFile -> DependencySource in
@@ -833,7 +838,11 @@ extension ModuleDependencyGraph {
833838
else {
834839
throw malformedError
835840
}
836-
self.currentDefKey = try dependencyKey(fields: (0, 1, 2, 3))
841+
self.currentDefKey = try dependencyKey(
842+
kindCodeField: 0,
843+
declAspectField: 1,
844+
contextField: 2,
845+
identifierField: 3)
837846
case .useIDNode:
838847
guard let key = self.currentDefKey,
839848
record.fields.count == 1 else {
@@ -1025,6 +1034,7 @@ extension ModuleDependencyGraph {
10251034
$0.append(RecordID.metadata)
10261035
$0.append(serializedGraphVersion.majorForWriting)
10271036
$0.append(serializedGraphVersion.minorForWriting)
1037+
$0.append(min(UInt(internedStringTable.count), UInt(UInt32.max)))
10281038
},
10291039
blob: self.compilerVersion)
10301040
}
@@ -1068,6 +1078,8 @@ extension ModuleDependencyGraph {
10681078
.fixed(bitWidth: 16),
10691079
// Minor version
10701080
.fixed(bitWidth: 16),
1081+
// Number of strings to be interned
1082+
.fixed(bitWidth: 32),
10711083
// Frontend version
10721084
.blob,
10731085
])

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)