Skip to content

Commit c92cfd1

Browse files
committed
Revert "This is mostly just weird experiments, but sample says it still spends a decent amount of time in swift_getGenericMetadata"
This reverts commit 2e325db26cb33b6c124ed7783fbc7c075e53d0ab.
1 parent c07654a commit c92cfd1

File tree

1 file changed

+66
-96
lines changed

1 file changed

+66
-96
lines changed

stdlib/public/Darwin/Foundation/NSDictionary.swift

Lines changed: 66 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,6 @@ import _SwiftFoundationOverlayShims
2424
) -> Void)
2525
}
2626

27-
extension NSDictionary {
28-
internal func _fastEnumerateKeysAndObjects(block:
29-
(AnyObject, AnyObject)->Bool) -> Bool {
30-
var success = true
31-
let fastSelf = unsafeBitCast(self, to: _NSDictionaryEnumerationHack.self)
32-
fastSelf.enumerateKeysAndObjects(options:0, using:
33-
{ (anyKey, anyValue, stopPtr) in
34-
anyKey._withUnsafeGuaranteedRef { (anyKey) in
35-
anyValue._withUnsafeGuaranteedRef { (anyValue) in
36-
success = block(anyKey, anyValue)
37-
if !success {
38-
stopPtr.pointee = 1
39-
}
40-
}
41-
}
42-
})
43-
return success
44-
}
45-
}
46-
4727
//===----------------------------------------------------------------------===//
4828
// Dictionaries
4929
//===----------------------------------------------------------------------===//
@@ -91,80 +71,6 @@ extension Dictionary : _ObjectiveCBridgeable {
9171
return unsafeBitCast(_bridgeToObjectiveCImpl() as AnyObject,
9272
to: NSDictionary.self)
9373
}
94-
95-
@inline(__always)
96-
internal static func _bridgeElement<T>(_ anyKey:AnyObject, _ force: Bool) -> T? {
97-
if force {
98-
return Swift._forceBridgeFromObjectiveC(anyKey, T.self)
99-
} else {
100-
return Swift._conditionallyBridgeFromObjectiveC(anyKey, T.self)
101-
}
102-
}
103-
104-
internal static func _nonverbatimBridgeFromObjectiveC(
105-
_ x: NSDictionary,
106-
result: inout Dictionary?,
107-
force: Bool
108-
) -> Bool {
109-
let success:Bool
110-
var maybeKey:Key? = nil
111-
var maybeKeyString:String? = nil
112-
var maybeValue:Value? = nil
113-
// `Dictionary<Key, Value>` where either `Key` or `Value` is a value type
114-
// may not be backed by an NSDictionary.
115-
if Key.self == String.self {
116-
var builder = _DictionaryBuilder<String, Value>(count: x.count)
117-
success = x._fastEnumerateKeysAndObjects { (anyKey, anyValue) in
118-
maybeValue = _bridgeElement(anyValue, force)
119-
guard let value:Value = maybeValue else {
120-
return false
121-
}
122-
123-
maybeKeyString = _bridgeElement(anyKey, force)
124-
guard let key:String = maybeKeyString else {
125-
return false
126-
}
127-
// String and NSString have different concepts of equality, so
128-
// string-keyed NSDictionaries may generate key collisions when bridged
129-
// over to Swift. See rdar://problem/35995647
130-
if key._isKnownNFC {
131-
builder.add(
132-
key: key,
133-
value: value)
134-
} else {
135-
// FIXME: Log a warning if `dict` already had a value for `key`
136-
builder.add(
137-
possibleDuplicateKey: key,
138-
value: value)
139-
}
140-
return true
141-
}
142-
if success {
143-
result = unsafeBitCast(builder.take(), to: Dictionary.self)
144-
}
145-
} else {
146-
var builder = _DictionaryBuilder<Key, Value>(count: x.count)
147-
success = x._fastEnumerateKeysAndObjects { (anyKey, anyValue) in
148-
maybeValue = _bridgeElement(anyValue, force)
149-
guard let value:Value = maybeValue else {
150-
return false
151-
}
152-
153-
maybeKey = _bridgeElement(anyKey, force)
154-
guard let key:Key = maybeKey else {
155-
return false
156-
}
157-
builder.add(
158-
key: key,
159-
value: value)
160-
return true
161-
}
162-
if success {
163-
result = builder.take()
164-
}
165-
}
166-
return success
167-
}
16874

16975
public static func _forceBridgeFromObjectiveC(
17076
_ x: NSDictionary,
@@ -182,7 +88,35 @@ extension Dictionary : _ObjectiveCBridgeable {
18288
return
18389
}
18490

185-
_ = _nonverbatimBridgeFromObjectiveC(x, result: &result, force: true)
91+
// `Dictionary<Key, Value>` where either `Key` or `Value` is a value type
92+
// may not be backed by an NSDictionary.
93+
let fastSelf = unsafeBitCast(x, to: _NSDictionaryEnumerationHack.self)
94+
var builder = _DictionaryBuilder<Key, Value>(count: x.count)
95+
fastSelf.enumerateKeysAndObjects(options: 0) { (anyKey, anyValue, stopPtr) in
96+
anyKey._withUnsafeGuaranteedRef { (anyKey) in
97+
anyValue._withUnsafeGuaranteedRef { (anyValue) in
98+
let key = Swift._forceBridgeFromObjectiveC(
99+
anyKey, Key.self)
100+
let value = Swift._forceBridgeFromObjectiveC(
101+
anyValue, Value.self)
102+
// String and NSString have different concepts of equality, so
103+
// string-keyed NSDictionaries may generate key collisions when bridged
104+
// over to Swift. See rdar://problem/35995647
105+
if Key.self == String.self &&
106+
!(unsafeBitCast(key, to: String.self)._isKnownNFC) {
107+
// FIXME: Log a warning if `dict` already had a value for `key`
108+
builder.add(
109+
possibleDuplicateKey: key,
110+
value: value)
111+
} else {
112+
builder.add(
113+
key: key,
114+
value: value)
115+
}
116+
}
117+
}
118+
}
119+
result = builder.take()
186120
}
187121

188122
public static func _conditionallyBridgeFromObjectiveC(
@@ -196,7 +130,43 @@ extension Dictionary : _ObjectiveCBridgeable {
196130
return true
197131
}
198132

199-
return _nonverbatimBridgeFromObjectiveC(x, result: &result, force: false)
133+
var success = true
134+
// `Dictionary<Key, Value>` where either `Key` or `Value` is a value type
135+
// may not be backed by an NSDictionary.
136+
let fastSelf = unsafeBitCast(x, to: _NSDictionaryEnumerationHack.self)
137+
var builder = _DictionaryBuilder<Key, Value>(count: x.count)
138+
fastSelf.enumerateKeysAndObjects(options: 0) { (anyKey, anyValue, stopPtr) in
139+
anyKey._withUnsafeGuaranteedRef { (anyKey) in
140+
anyValue._withUnsafeGuaranteedRef { (anyValue) in
141+
guard let key = Swift._conditionallyBridgeFromObjectiveC(
142+
anyKey, Key.self),
143+
let value = Swift._conditionallyBridgeFromObjectiveC(
144+
anyValue, Value.self) else {
145+
stopPtr.pointee = 1
146+
success = false
147+
return
148+
}
149+
// String and NSString have different concepts of equality, so
150+
// string-keyed NSDictionaries may generate key collisions when bridged
151+
// over to Swift. See rdar://problem/35995647
152+
if Key.self == String.self &&
153+
!(unsafeBitCast(key, to: String.self)._isKnownNFC) {
154+
// FIXME: Log a warning if `dict` already had a value for `key`
155+
builder.add(
156+
possibleDuplicateKey: key,
157+
value: value)
158+
} else {
159+
builder.add(
160+
key: key,
161+
value: value)
162+
}
163+
}
164+
}
165+
}
166+
if success {
167+
result = builder.take()
168+
}
169+
return success
200170
}
201171

202172
@_effects(readonly)

0 commit comments

Comments
 (0)