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
This commit is contained in:
Franz Busch authored and GitHub committed 2021-12-14 17:40:40 +00:00
1 parent 213eb6887e
commit f228e264fc
10 files changed
+103 -72

No files matched your search

+12 -6
View File
@@ -16,12 +16,15 @@ import Dispatch
import _NIODataStructures
import NIOCore
private final class EmbeddedScheduledTask {
private struct EmbeddedScheduledTask {
let id: UInt64
let task: () -> Void
let readyTime: NIODeadline
let insertOrder: UInt64
init(readyTime: NIODeadline, insertOrder: UInt64, task: @escaping () -> Void) {
init(id: UInt64, readyTime: NIODeadline, insertOrder: UInt64, task: @escaping () -> Void) {
self.id = id
self.readyTime = readyTime
self.insertOrder = insertOrder
self.task = task
@@ -38,7 +41,7 @@ extension EmbeddedScheduledTask: Comparable {
}
static func == (lhs: EmbeddedScheduledTask, rhs: EmbeddedScheduledTask) -> Bool {
return lhs === rhs
return lhs.id == rhs.id
}
}
@@ -63,6 +66,7 @@ public final class EmbeddedEventLoop: EventLoop {
/// The current "time" for this event loop. This is an amount in nanoseconds.
/* private but tests */ internal var _now: NIODeadline = .uptimeNanoseconds(0)
private var scheduledTaskCounter: UInt64 = 0
private var scheduledTasks = PriorityQueue<EmbeddedScheduledTask>()
/// Keep track of where promises are allocated to ensure we can identify their source if they leak.
@@ -92,16 +96,18 @@ public final class EmbeddedEventLoop: EventLoop {
@discardableResult
public func scheduleTask<T>(deadline: NIODeadline, _ task: @escaping () throws -> T) -> Scheduled<T> {
let promise: EventLoopPromise<T> = makePromise()
let task = EmbeddedScheduledTask(readyTime: deadline, insertOrder: self.nextTaskNumber()) {
self.scheduledTaskCounter += 1
let task = EmbeddedScheduledTask(id: self.scheduledTaskCounter, readyTime: deadline, insertOrder: self.nextTaskNumber(), task: {
do {
promise.succeed(try task())
} catch let err {
promise.fail(err)
}
}
})
let taskId = task.id
let scheduled = Scheduled(promise: promise, cancellationTask: {
self.scheduledTasks.remove(task)
self.scheduledTasks.removeFirst { $0.id == taskId }
})
scheduledTasks.push(task)
return scheduled