[stdlib][performance] skip copying old values during removeAll(keepCapacity: true) #616
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
(This is a re-submission of #477 with clearer patch and description.)
For
.removeAll(keepCapacity: true)
on a non-uniquely referencedDictionary
orSet
, the existing code implements COW by callingensureUniqueNativeStorage()
, which actually copies the content of its storage. Only then the elements get deleted one by one.A FIXME(performance) comment stated this wastefulness (benchmark result posted in #477 agrees). A better solution is directly replace the native storage with equal capacity.
A note on Set: As @dabrahams pointed out,
aSet.removeAll()
is the equivalent ofaSet.intersectInPlace([])
. Ideally,intersectInPlace()
, being the more general case, should be the target for optimization. In successfully doing so, the equivalence in math can be directly realized in code.In that light, this patch can be treated as solely an optimization for
Dictionary
. The speedup for Set is a side-effect (courtesy of GYP and architect of this file).Tests in both
swift/validation-test/stdlib/Set.swift
and./lit swift/validation-test/stdlib/Dictionary.swift
are passing.