Skip to content

Commit aae60ff

Browse files
committed
stdlib: Code size improvements for Dictionary for -Osize
The first change is to remove some @inline(__always) attributes. Those were added before we had the guaranteed-by-default calling convention. They are not necessary anymore. The second change is to not specialize some slow-path functions. This results that no specialization code for these functions is generated at the client side. Instead those functions are directly called in the libSwiftCore. Note that Key-related hash and equality comparisons are still specialized, because otherwise the performance hit for Osize would be too big. Some Dictionary benchmarks regress a bit with -Osize, but the code size wins are big. rdar://problem/46534453
1 parent 5b403ea commit aae60ff

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

stdlib/public/core/Dictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,6 @@ extension Dictionary {
782782
/// otherwise, `nil`.
783783
@inlinable
784784
public subscript(key: Key) -> Value? {
785-
@inline(__always)
786785
get {
787786
return _variant.lookup(key)
788787
}
@@ -819,6 +818,7 @@ extension Dictionary: ExpressibleByDictionaryLiteral {
819818
/// dictionary. Each key in `elements` must be unique.
820819
@inlinable
821820
@_effects(readonly)
821+
@_semantics("optimize.sil.specialize.generic.size.never")
822822
public init(dictionaryLiteral elements: (Key, Value)...) {
823823
let native = _NativeDictionary<Key, Value>(capacity: elements.count)
824824
for (key, value) in elements {

stdlib/public/core/DictionaryVariant.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ extension Dictionary._Variant {
382382
}
383383

384384
@inlinable
385+
@_semantics("optimize.sil.specialize.generic.size.never")
385386
internal mutating func remove(at index: Index) -> Element {
386387
// FIXME(performance): fuse data migration and element deletion into one
387388
// operation.
@@ -412,6 +413,7 @@ extension Dictionary._Variant {
412413
}
413414

414415
@inlinable
416+
@_semantics("optimize.sil.specialize.generic.size.never")
415417
internal mutating func removeAll(keepingCapacity keepCapacity: Bool) {
416418
if !keepCapacity {
417419
self = .init(native: _NativeDictionary())

stdlib/public/core/NativeDictionary.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ extension _NativeDictionary { // ensureUnique
202202
}
203203

204204
@inlinable
205+
@_semantics("optimize.sil.specialize.generic.size.never")
205206
internal mutating func copyAndResize(capacity: Int) {
206207
let capacity = Swift.max(capacity, self.capacity)
207208
let newStorage = _DictionaryStorage<Key, Value>.resize(
@@ -220,6 +221,7 @@ extension _NativeDictionary { // ensureUnique
220221
}
221222

222223
@inlinable
224+
@_semantics("optimize.sil.specialize.generic.size.never")
223225
internal mutating func copy() {
224226
let newStorage = _DictionaryStorage<Key, Value>.copy(original: _storage)
225227
_internalInvariant(newStorage._scale == _storage._scale)
@@ -241,7 +243,7 @@ extension _NativeDictionary { // ensureUnique
241243
/// Ensure storage of self is uniquely held and can hold at least `capacity`
242244
/// elements. Returns true iff contents were rehashed.
243245
@inlinable
244-
@inline(__always)
246+
@_semantics("optimize.sil.specialize.generic.size.never")
245247
internal mutating func ensureUnique(isUnique: Bool, capacity: Int) -> Bool {
246248
if _fastPath(capacity <= self.capacity && isUnique) {
247249
return false
@@ -490,7 +492,6 @@ extension _NativeDictionary { // Insertions
490492
/// held, and with enough capacity for a single insertion (if the key isn't
491493
/// already in the dictionary.)
492494
@inlinable
493-
@inline(__always)
494495
internal mutating func mutatingFind(
495496
_ key: Key,
496497
isUnique: Bool
@@ -629,6 +630,7 @@ extension _NativeDictionary: _HashTableDelegate {
629630
extension _NativeDictionary { // Deletion
630631
@inlinable
631632
@_effects(releasenone)
633+
@_semantics("optimize.sil.specialize.generic.size.never")
632634
internal func _delete(at bucket: Bucket) {
633635
hashTable.delete(at: bucket, with: self)
634636
_storage._count -= 1
@@ -637,7 +639,7 @@ extension _NativeDictionary { // Deletion
637639
}
638640

639641
@inlinable
640-
@inline(__always)
642+
@_semantics("optimize.sil.specialize.generic.size.never")
641643
internal mutating func uncheckedRemove(
642644
at bucket: Bucket,
643645
isUnique: Bool

0 commit comments

Comments
 (0)