Depending on the supplied sequence, `Deque.append(contentsOf:)` currently reserves just enough capacity as it needs to perform the append. This isn't right -- it can result in O(n) reallocs instead of O(log(n)) when items are incrementally appended in constant-sized `append(contents:)` batches.
`withHiddenCopies(if:of:)` is supposed to check for value semantic violations where mutations of one copy affect another -- however, it currently only checks that iteration returns the same items before/after the change, not that the old copy is still in a fully consistent state.
Add an extra parameter that can be set to a closure that performs stricter checks.
Use it to call `_checkInvariants()` in ordered collections tests, uncovering a missing uniqueness check in `OrderedSet._insertNew(_:at:in:)`.
These two do *very* non-trivial processing, so they don't at all work like views in the Standard Library.
They also don't carry their weight -- they are simply lightweight wrappers around `popMin` and `popMax`, respectively.
Let's get rid of them before we etch them in stone as public API.
In debug builds, the `OrderedSet.init(uncheckedUniqueElements:)` initializer always created a set with no hash table, even if it had an element count >15. This is technically an invariant violation (caught by `_checkInvariants()`), although it generally only triggers (very) slow performance. (The set reverts to linear searching.)
It seems useful to be able to manually call `_checkInvariants` while debugging a package issue. This is public as in `public`, not public as in Public API -- it's underscored.
- Use a persistent DOI-based link rather than linking to ephemeral course material.
- Replace the example heap (borrowed from Wikipedia) with a better one.
* [Heap] Don't always inline large functions
* [docs] Use a stable link to the Atkinson article
(The original link was pointing to course materials at a random university.)
* Remove stray import
* Revert "[Heap] Don't always inline large functions"
This reverts commit 07ffd6591f.
* [Heap] Enable code coverage collection in Xcode scheme
* [Heap] Speed up invariant checking
* [Heap] Precalculate levels for each offset
`_Node` is a new struct that consists of a storage offset (the old index) along with its level in the tree. The level can be incrementally calculated, saving some time vs counting bits whenever it's needed.
* [Heap] Switch to using unsafe buffer pointers
Introduce `Heap._UnsafeHandle` (a thin wrapper around an unsafe buffer pointer) and rebase most heap algorithms on top of that instead of array operations.
This simplifies things by reducing (hopefully) unnecessary index validation, resulting in some measurable performance improvements.
* [Heap] Stop force-inlining bubbleUp; mark it releasenone
Not inlining such a large function speeds things up by leaving some headroom for the optimizer to make better inlining decisions elsewhere. (Force inlining this resulted in the compiler not inlining the closure passed to `_update` instead, which isn't a great tradeoff.
To speed things up, mark `bubbleUp` with `@_effects(releasenone)`. This may be questionable (because it calls `Element.<`), but it results in better codegen, making `insert` match the performance of `std::priority_queue`.
* [Heap] Rework removals
* [Heap] Remove dead code
* [Heap] Switch to using _ContiguousArray as storage
* [Heap] insert<S>(contentsOf:): add fast path for count == 0 case
* [Heap] Perf pass on trickleDown code paths
This improves popMin/popMax (and the sequence initializer) by reviewing trickleDown and optimizing things:
- Replace swapAt with a scheme where we keep a hold in the storage buffer
- Slightly shorten min/max dependency chain in primary sink loop
In exchange, we get even less readable code.
* [Heap] bubbleUp: remove @_effects attribute
This reintroduces retain/release operations for the empty array until https://github.com/apple/swift/pull/38898 lands.
Mitigate the performance costs of this by refactoring code to reduce the size of the inlined bubbleUpMin/Max invocations.
* [Heap] Finetune Heap.insert