mirror of
https://github.com/apple/swift-collections
synced 2026-11-28 05:04:21 +00:00
[BasicContainers] UniqueArray.replace(removing:copying): Fix overallocation issue
`UniqueArray.replace(removing:copying:)` overloads overestimated the amount of memory they need, sometimes leading to unnecessary reallocations. Fix this.
This commit is contained in:
2 files changed
+48
-3
No files matched your search
@@ -441,7 +441,7 @@ extension UniqueArray {
|
||||
copying newElements: UnsafeBufferPointer<Element>
|
||||
) {
|
||||
// FIXME: Avoid moving the subsequent elements twice.
|
||||
_ensureFreeCapacity(newElements.count)
|
||||
_ensureFreeCapacity(newElements.count - subrange.count)
|
||||
unsafe _storage.replace(removing: subrange, copying: newElements)
|
||||
}
|
||||
|
||||
@@ -518,7 +518,7 @@ extension UniqueArray {
|
||||
copying newElements: Span<Element>
|
||||
) {
|
||||
// FIXME: Avoid moving the subsequent elements twice.
|
||||
_ensureFreeCapacity(newElements.count)
|
||||
_ensureFreeCapacity(newElements.count - subrange.count)
|
||||
_storage.replace(removing: subrange, copying: newElements)
|
||||
}
|
||||
|
||||
@@ -605,7 +605,7 @@ extension UniqueArray {
|
||||
) {
|
||||
// FIXME: Avoid moving the subsequent elements twice.
|
||||
let c = newElements.count
|
||||
_ensureFreeCapacity(c)
|
||||
_ensureFreeCapacity(c - subrange.count)
|
||||
_storage._replace(
|
||||
removing: subrange,
|
||||
copyingCollection: newElements,
|
||||
|
||||
Reference in new issue
Block a user