Skip to content

Commit ce82e80

Browse files
authored
Merge pull request #20911 from Catfish-Man/dictionary-cast-builder
Use _DictionaryBuilder instead of Dictionary for casts
2 parents cf1be5e + 17e7c5f commit ce82e80

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

stdlib/public/core/DictionaryCasting.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
public func _dictionaryUpCast<DerivedKey, DerivedValue, BaseKey, BaseValue>(
2222
_ source: Dictionary<DerivedKey, DerivedValue>
2323
) -> Dictionary<BaseKey, BaseValue> {
24-
var result = Dictionary<BaseKey, BaseValue>(minimumCapacity: source.count)
24+
var builder = _DictionaryBuilder<BaseKey, BaseValue>(count: source.count)
2525

2626
for (k, v) in source {
27-
result[k as! BaseKey] = (v as! BaseValue)
27+
builder.add(key:k as! BaseKey, value: v as! BaseValue)
2828
}
29-
return result
29+
return builder.take()
3030
}
3131

3232
/// Called by the casting machinery.
@@ -98,11 +98,11 @@ public func _dictionaryDownCastConditional<
9898
_ source: Dictionary<BaseKey, BaseValue>
9999
) -> Dictionary<DerivedKey, DerivedValue>? {
100100

101-
var result = Dictionary<DerivedKey, DerivedValue>()
101+
var builder = _DictionaryBuilder<DerivedKey, DerivedValue>(count: source.count)
102102
for (k, v) in source {
103103
guard let k1 = k as? DerivedKey, let v1 = v as? DerivedValue
104104
else { return nil }
105-
result[k1] = v1
105+
builder.add(key: k1, value: v1)
106106
}
107-
return result
107+
return builder.take()
108108
}

0 commit comments

Comments
 (0)