Files
Franz Busch f228e264fc Convert ScheduledTask to a struct (#2010)
### Motivation:

In my previous PR https://github.com/apple/swift-nio/pull/2009, I added baseline performance and allocation tests around `scheduleTask` and `execute`. After analysing, the various allocations that happen when scheduling a task there were only a few that could be optimized away potentially.

### Modifications:

This PR converts the `ScheduledTask` class to a struct which will reduce the number of allocations for scheduling tasks by 1. The only thing that needs to be worked around when converting to a struct is giving it an identity so that we can implement `Equatable` conformance properly. I explored two options. First, using an `ObjectIdentifier` passed to the init. Second, using an atomic counter per EventLoop. I went with the latter since the former requires an additional allocation in the case of calling `execute`

### Result:

`scheduleTask` and `execute` require one less allocation
2021-12-14 17:40:40 +00:00

104 lines
2.5 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 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
//
//===----------------------------------------------------------------------===//
public struct PriorityQueue<Element: Comparable> {
@usableFromInline
internal var _heap: Heap<Element>
@inlinable
public init() {
self._heap = Heap()
}
@inlinable
public mutating func remove(_ key: Element) {
self._heap.remove(value: key)
}
@inlinable
public mutating func removeFirst(where shouldBeRemoved: (Element) throws -> Bool) rethrows {
try self._heap.removeFirst(where: shouldBeRemoved)
}
@inlinable
public mutating func push(_ key: Element) {
self._heap.append(key)
}
@inlinable
public func peek() -> Element? {
return self._heap.storage.first
}
@inlinable
public var isEmpty: Bool {
return self._heap.storage.isEmpty
}
@inlinable
@discardableResult
public mutating func pop() -> Element? {
return self._heap.removeRoot()
}
@inlinable
public mutating func clear() {
self._heap = Heap()
}
}
extension PriorityQueue: Equatable {
@inlinable
public static func ==(lhs: PriorityQueue, rhs: PriorityQueue) -> Bool {
return lhs.count == rhs.count && lhs.elementsEqual(rhs)
}
}
extension PriorityQueue: Sequence {
public struct Iterator: IteratorProtocol {
/* private but */ @usableFromInline
var _queue: PriorityQueue<Element>
/* fileprivate but */ @inlinable
public init(queue: PriorityQueue<Element>) {
self._queue = queue
}
@inlinable
public mutating func next() -> Element? {
return self._queue.pop()
}
}
@inlinable
public func makeIterator() -> Iterator {
return Iterator(queue: self)
}
}
extension PriorityQueue {
@inlinable
public var count: Int {
return self._heap.count
}
}
extension PriorityQueue: CustomStringConvertible {
@inlinable
public var description: String {
return "PriorityQueue(count: \(self.count)): \(Array(self))"
}
}