Skip to content

Commit bf32e78

Browse files
committed
Make bridged empty NSDictionary and NSSet instances bridge to the empty singletons
1 parent bf09c28 commit bf32e78

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

stdlib/public/core/NativeDictionary.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,17 @@ internal struct _NativeDictionary<Key: Hashable, Value> {
5252

5353
@inlinable
5454
internal init(_ cocoa: __owned __CocoaDictionary, capacity: Int) {
55-
_internalInvariant(cocoa.count <= capacity)
56-
self._storage =
57-
_DictionaryStorage<Key, Value>.convert(cocoa, capacity: capacity)
58-
for (key, value) in cocoa {
59-
insertNew(
60-
key: _forceBridgeFromObjectiveC(key, Key.self),
61-
value: _forceBridgeFromObjectiveC(value, Value.self))
55+
if capacity == 0 {
56+
self._storage = __RawDictionaryStorage.empty
57+
} else {
58+
_internalInvariant(cocoa.count <= capacity)
59+
self._storage =
60+
_DictionaryStorage<Key, Value>.convert(cocoa, capacity: capacity)
61+
for (key, value) in cocoa {
62+
insertNew(
63+
key: _forceBridgeFromObjectiveC(key, Key.self),
64+
value: _forceBridgeFromObjectiveC(value, Value.self))
65+
}
6266
}
6367
}
6468
#endif

stdlib/public/core/NativeSet.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,15 @@ internal struct _NativeSet<Element: Hashable> {
5151

5252
@inlinable
5353
internal init(_ cocoa: __owned __CocoaSet, capacity: Int) {
54-
_internalInvariant(cocoa.count <= capacity)
55-
self._storage = _SetStorage<Element>.convert(cocoa, capacity: capacity)
56-
for element in cocoa {
57-
let nativeElement = _forceBridgeFromObjectiveC(element, Element.self)
58-
insertNew(nativeElement, isUnique: true)
54+
if capacity == 0 {
55+
self._storage = __RawSetStorage.empty
56+
} else {
57+
_internalInvariant(cocoa.count <= capacity)
58+
self._storage = _SetStorage<Element>.convert(cocoa, capacity: capacity)
59+
for element in cocoa {
60+
let nativeElement = _forceBridgeFromObjectiveC(element, Element.self)
61+
insertNew(nativeElement, isUnique: true)
62+
}
5963
}
6064
}
6165
#endif

0 commit comments

Comments
 (0)