@@ -3394,21 +3394,27 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
3394
3394
}
3395
3395
}
3396
3396
3397
- internal mutating func nativeKeepCapacityRemoveAll() {
3397
+ internal mutating func nativeRemoveAll() {
3398
+ var nativeStorage = native
3399
+
3400
+ // FIXME(performance): if the storage is non-uniquely referenced, we
3401
+ // shouldn’t be copying the elements into new storage and then immediately
3402
+ // deleting the elements. We should detect that the storage is not uniquely
3403
+ // referenced and allocate new empty storage of appropriate capacity.
3398
3404
3399
3405
// We have already checked for the empty dictionary case, so we will always
3400
3406
// mutating the dictionary storage. Request unique storage.
3407
+ let (reallocated, _) = ensureUniqueNativeStorage(nativeStorage.capacity)
3408
+ if reallocated {
3409
+ nativeStorage = native
3410
+ }
3401
3411
3402
- if !isUniquelyReferenced() {
3403
- self = .Native(NativeStorageOwner(minimumCapacity: native.capacity))
3404
- } else {
3405
- for b in 0..<native.capacity {
3406
- if native.isInitializedEntry(b) {
3407
- native.destroyEntryAt(b)
3408
- }
3412
+ for var b = 0; b != nativeStorage.capacity; ++b {
3413
+ if nativeStorage.isInitializedEntry(b) {
3414
+ nativeStorage.destroyEntryAt(b)
3409
3415
}
3410
3416
}
3411
- native .count = 0
3417
+ nativeStorage .count = 0
3412
3418
}
3413
3419
3414
3420
internal mutating func removeAll(keepCapacity keepCapacity: Bool) {
@@ -3422,13 +3428,13 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
3422
3428
}
3423
3429
3424
3430
if _fastPath(guaranteedNative) {
3425
- nativeKeepCapacityRemoveAll ()
3431
+ nativeRemoveAll ()
3426
3432
return
3427
3433
}
3428
3434
3429
3435
switch self {
3430
3436
case .Native:
3431
- nativeKeepCapacityRemoveAll ()
3437
+ nativeRemoveAll ()
3432
3438
case .Cocoa(let cocoaStorage):
3433
3439
#if _runtime(_ObjC)
3434
3440
self = .Native(NativeStorage.Owner(minimumCapacity: cocoaStorage.count))
0 commit comments