@@ -3394,27 +3394,21 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
3394
3394
}
3395
3395
}
3396
3396
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.
3397
+ internal mutating func nativeKeepCapacityRemoveAll() {
3404
3398
3405
3399
// We have already checked for the empty dictionary case, so we will always
3406
3400
// mutating the dictionary storage. Request unique storage.
3407
- let (reallocated, _) = ensureUniqueNativeStorage(nativeStorage.capacity)
3408
- if reallocated {
3409
- nativeStorage = native
3410
- }
3411
3401
3412
- for var b = 0; b != nativeStorage.capacity; ++b {
3413
- if nativeStorage.isInitializedEntry(b) {
3414
- nativeStorage.destroyEntryAt(b)
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
+ }
3415
3409
}
3416
3410
}
3417
- nativeStorage .count = 0
3411
+ native .count = 0
3418
3412
}
3419
3413
3420
3414
internal mutating func removeAll(keepCapacity keepCapacity: Bool) {
@@ -3428,13 +3422,13 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
3428
3422
}
3429
3423
3430
3424
if _fastPath(guaranteedNative) {
3431
- nativeRemoveAll ()
3425
+ nativeKeepCapacityRemoveAll ()
3432
3426
return
3433
3427
}
3434
3428
3435
3429
switch self {
3436
3430
case .Native:
3437
- nativeRemoveAll ()
3431
+ nativeKeepCapacityRemoveAll ()
3438
3432
case .Cocoa(let cocoaStorage):
3439
3433
#if _runtime(_ObjC)
3440
3434
self = .Native(NativeStorage.Owner(minimumCapacity: cocoaStorage.count))
0 commit comments