Skip to content

Commit 4165aca

Browse files
committed
Merge pull request #699 from apple/revert-616-removeAll_speedup
Revert "[stdlib][performance] skip copying old values during removeAll(keepCapacity: true)"
2 parents 83018a1 + 2ed81bd commit 4165aca

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3394,21 +3394,27 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
33943394
}
33953395
}
33963396

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.
33983404

33993405
// We have already checked for the empty dictionary case, so we will always
34003406
// mutating the dictionary storage. Request unique storage.
3407+
let (reallocated, _) = ensureUniqueNativeStorage(nativeStorage.capacity)
3408+
if reallocated {
3409+
nativeStorage = native
3410+
}
34013411

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)
34093415
}
34103416
}
3411-
native.count = 0
3417+
nativeStorage.count = 0
34123418
}
34133419

34143420
internal mutating func removeAll(keepCapacity keepCapacity: Bool) {
@@ -3422,13 +3428,13 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorageType {
34223428
}
34233429

34243430
if _fastPath(guaranteedNative) {
3425-
nativeKeepCapacityRemoveAll()
3431+
nativeRemoveAll()
34263432
return
34273433
}
34283434

34293435
switch self {
34303436
case .Native:
3431-
nativeKeepCapacityRemoveAll()
3437+
nativeRemoveAll()
34323438
case .Cocoa(let cocoaStorage):
34333439
#if _runtime(_ObjC)
34343440
self = .Native(NativeStorage.Owner(minimumCapacity: cocoaStorage.count))

0 commit comments

Comments
 (0)