From 069dd4e5df442e813eb30e3216aafa1cc1696a5f Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Mon, 13 Nov 2023 21:28:24 -0800 Subject: [PATCH] =?UTF-8?q?[Heap]=20insert(contentsOf:)=20Switch=20to=20Fl?= =?UTF-8?q?oyd=E2=80=99s=20if=20we=E2=80=99re=20inserting=20too=20many=20i?= =?UTF-8?q?tems?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/HeapModule/Heap.swift | 41 ++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/Sources/HeapModule/Heap.swift b/Sources/HeapModule/Heap.swift index 4e7e60e9..54d2ac80 100644 --- a/Sources/HeapModule/Heap.swift +++ b/Sources/HeapModule/Heap.swift @@ -253,18 +253,49 @@ extension Heap { /// /// - Parameter newElements: The new elements to insert into the heap. /// - /// - Complexity: O(*n* * log(`count`)), where *n* is the length of `newElements`. + /// - Complexity: O(`count` + *k*), where *k* is the length of `newElements`. @inlinable public mutating func insert( contentsOf newElements: S ) where S.Element == Element { - if count == 0 { + let origCount = self.count + if origCount == 0 { self = Self(newElements) return } - _storage.reserveCapacity(count + newElements.underestimatedCount) - for element in newElements { - insert(element) + defer { _checkInvariants() } + _storage.append(contentsOf: newElements) + let newCount = self.count + + guard newCount > origCount, newCount > 1 else { + // If we didn't append, or the result is too small to violate heapness, + // then we have nothing else to dp. + return + } + + // Otherwise we can either insert items one by one, or we can run Floyd's + // algorithm to re-heapify our entire storage from scratch. + // + // If n is the original count, and k is the number of items we need to + // append, then Floyd's costs O(n + k) comparisons/swaps, while + // the naive loop costs k * log(n + k) -- so we expect that Floyd will + // be cheaper whenever k is "large enough" relative to n. + // + // Floyd's algorithm has a worst-case upper complexity bound of 2 * (n + k), + // so one simple heuristic is to use it whenever k * log(n + k) exceeds + // that. + // + // FIXME: Write a benchmark to verify this heuristic. + let heuristicLimit = 2 * newCount / newCount._binaryLogarithm() + let useFloyd = (newCount - origCount) < heuristicLimit + _update { handle in + if useFloyd { + handle.heapify() + } else { + for offset in origCount ..< handle.count { + handle.bubbleUp(_HeapNode(offset: offset)) + } + } } } }