[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:
Karoy Lorentey committed 2026-04-22 16:23:15 -07:00
1 parent 6ed215dcc2
commit 5bd8ab095d
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,