mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Motivation: Right now Heap.heapify incurs quite a lot of ARC traffic when it is used in SelectableEventLoop. Specifically, each of the invocations of self.comparator in heapify incurs three refcount operations. These three are all the result of the fact that the comparator is an arbitrary function, but all three are guaranteed to be unnecessary. The first refcount is for the closure context for self.comparator. Of course, self.comparator has only two possible values, neither of which needs a closure context, but the compiler doesn't know that and the type system doesn't encode it. The other two refcounts are for the ScheduledTask objects themselves. Again, the compiler doesn't know what the comparator function will do, so it must defensively ensure that a retain/release is inserted for the arguments of that function. If we happen to hit both branches of the code, we'll execute 6 retains and 6 releases, and even in fairly average cases we'll execute 3 of each. All of this is unnecessary: the only runtime function ever used here is a trivial comparison operation. We know what it is at compile time, we just have to ask. While it was admirable to have the configurability of the priority queue in place, after 3 years I think we have a pretty good feeling that we're not going to need the ability to have a max-heap instead of a min-heap. We can always get this back if we need it. Modifications: - Removed the configurability of Heap. - Removed HeapType. - Removed arguments from PriorityQueue - Removed all tests testing now-removed max-heap functionality. Result: Less ARC traffic in the core task scheduling loop.
95 lines
2.2 KiB
Swift
95 lines
2.2 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2020 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
@usableFromInline
|
|
internal struct PriorityQueue<Element: Comparable> {
|
|
@usableFromInline
|
|
internal var _heap: Heap<Element>
|
|
|
|
internal init() {
|
|
self._heap = Heap()
|
|
}
|
|
|
|
@inlinable
|
|
internal mutating func remove(_ key: Element) {
|
|
self._heap.remove(value: key)
|
|
}
|
|
|
|
@inlinable
|
|
internal mutating func push(_ key: Element) {
|
|
self._heap.append(key)
|
|
}
|
|
|
|
@inlinable
|
|
internal func peek() -> Element? {
|
|
return self._heap.storage.first
|
|
}
|
|
|
|
@inlinable
|
|
internal var isEmpty: Bool {
|
|
return self._heap.storage.isEmpty
|
|
}
|
|
|
|
@inlinable
|
|
@discardableResult
|
|
internal mutating func pop() -> Element? {
|
|
return self._heap.removeRoot()
|
|
}
|
|
|
|
@inlinable
|
|
internal mutating func clear() {
|
|
self._heap = Heap()
|
|
}
|
|
}
|
|
|
|
extension PriorityQueue: Equatable {
|
|
@usableFromInline
|
|
internal static func ==(lhs: PriorityQueue, rhs: PriorityQueue) -> Bool {
|
|
return lhs.count == rhs.count && lhs.elementsEqual(rhs)
|
|
}
|
|
}
|
|
|
|
extension PriorityQueue: Sequence {
|
|
@usableFromInline
|
|
struct Iterator: IteratorProtocol {
|
|
|
|
private var queue: PriorityQueue<Element>
|
|
fileprivate init(queue: PriorityQueue<Element>) {
|
|
self.queue = queue
|
|
}
|
|
|
|
public mutating func next() -> Element? {
|
|
return self.queue.pop()
|
|
}
|
|
}
|
|
|
|
@usableFromInline
|
|
func makeIterator() -> Iterator {
|
|
return Iterator(queue: self)
|
|
}
|
|
}
|
|
|
|
extension PriorityQueue {
|
|
var count: Int {
|
|
return self._heap.count
|
|
}
|
|
}
|
|
|
|
extension PriorityQueue: CustomStringConvertible {
|
|
@usableFromInline
|
|
var description: String {
|
|
return "PriorityQueue(count: \(self.count)): \(Array(self))"
|
|
}
|
|
}
|