diff --git a/Sources/HeapModule/Heap.swift b/Sources/HeapModule/Heap.swift index fbb6e40e..a4304c8d 100644 --- a/Sources/HeapModule/Heap.swift +++ b/Sources/HeapModule/Heap.swift @@ -280,13 +280,15 @@ extension Heap { public mutating func removeAll( where shouldBeRemoved: (Element) throws -> Bool ) rethrows { - defer { _checkInvariants() } - try _storage.removeAll(where: shouldBeRemoved) - if _storage.count > 1 { - _update { handle in - handle.heapify() + defer { + if _storage.count > 1 { + _update { handle in + handle.heapify() + } } + _checkInvariants() } + try _storage.removeAll(where: shouldBeRemoved) } /// Replaces the maximum value in the heap with the given replacement,