From 5771d508657c74975342962a66fd689926f09243 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Tue, 21 Jan 2025 16:04:37 +0000 Subject: [PATCH] Enable optimized Isolated view path for AsyncTestingEL (#3074) Motivation: NIOAsyncTestingEL is another place where we can reduce the overhead of the isolated EL views. It's nice for the tests to run faster. Modifications: - Implement the new protocol witnesses for AsyncTestingEL - Refactor common code for scheduleTask out Result: AsyncTestingEL is faster. --- .../NIOEmbedded/AsyncTestingEventLoop.swift | 143 ++++++++++++++---- 1 file changed, 112 insertions(+), 31 deletions(-) diff --git a/Sources/NIOEmbedded/AsyncTestingEventLoop.swift b/Sources/NIOEmbedded/AsyncTestingEventLoop.swift index b0ee04036..ef0070b1c 100644 --- a/Sources/NIOEmbedded/AsyncTestingEventLoop.swift +++ b/Sources/NIOEmbedded/AsyncTestingEventLoop.swift @@ -134,7 +134,7 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { private func insertTask( taskID: UInt64, deadline: NIODeadline, - promise: EventLoopPromise, + promise: EventLoopPromise?, task: @escaping () throws -> ReturnType ) { dispatchPrecondition(condition: .onQueue(self.queue)) @@ -146,12 +146,12 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { task: { do { // UnsafeUnchecked is acceptable because we know we're in the loop here. - promise.assumeIsolatedUnsafeUnchecked().succeed(try task()) + promise?.assumeIsolatedUnsafeUnchecked().succeed(try task()) } catch let err { - promise.fail(err) + promise?.fail(err) } }, - promise.fail + { promise?.fail($0) } ) self.scheduledTasks.push(task) @@ -164,38 +164,24 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { deadline: NIODeadline, _ task: @escaping @Sendable () throws -> T ) -> Scheduled { - let promise: EventLoopPromise = self.makePromise() - let taskID = self.scheduledTaskCounter.loadThenWrappingIncrement(ordering: .relaxed) + let scheduled: Scheduled + switch self._prepareToSchedule(returnType: T.self) { + case .doSchedule(let taskID, let promise, let returned): + scheduled = returned - switch self.state.load(ordering: .acquiring) { - case .open: - break - case .closing, .closed: - // If the event loop is shut down, or shutting down, immediately cancel the task. - promise.fail(EventLoopError.cancelled) - return Scheduled(promise: promise, cancellationTask: {}) - } - - let scheduled = Scheduled( - promise: promise, - cancellationTask: { - if self.inEventLoop { - self.removeTask(taskID: taskID) - } else { - self.queue.async { - self.removeTask(taskID: taskID) - } + // Ok, actually do it. + if self.inEventLoop { + self.insertTask(taskID: taskID, deadline: deadline, promise: promise, task: task) + } else { + self.queue.async { + self.insertTask(taskID: taskID, deadline: deadline, promise: promise, task: task) } } - ) - if self.inEventLoop { - self.insertTask(taskID: taskID, deadline: deadline, promise: promise, task: task) - } else { - self.queue.async { - self.insertTask(taskID: taskID, deadline: deadline, promise: promise, task: task) - } + case .returnImmediately(let returned): + scheduled = returned } + return scheduled } @@ -411,6 +397,62 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { dispatchPrecondition(condition: .notOnQueue(self.queue)) } + public func _executeIsolatedUnsafeUnchecked(_ task: @escaping () -> Void) { + // Call directly to insertTask. That function has a thread-safety check, and + // the rest of this method is using thread-safe operations so we don't + // need any extra debug-mode checking here. + let taskID = self.scheduledTaskCounter.loadThenWrappingIncrement(ordering: .relaxed) + self.insertTask( + taskID: taskID, + deadline: self.now, + promise: nil, + task: task + ) + } + + public func _submitIsolatedUnsafeUnchecked(_ task: @escaping () throws -> T) -> EventLoopFuture { + // Call directly to insertTask. That function has a thread-safety check, and + // the rest of this method is using thread-safe operations so we don't + // need any extra debug-mode checking here. + let promise = self.makePromise(of: T.self) + let taskID = self.scheduledTaskCounter.loadThenWrappingIncrement(ordering: .relaxed) + self.insertTask( + taskID: taskID, + deadline: self.now, + promise: promise, + task: task + ) + return promise.futureResult + } + + @discardableResult + public func _scheduleTaskIsolatedUnsafeUnchecked( + deadline: NIODeadline, + _ task: @escaping () throws -> T + ) -> Scheduled { + // Call directly to insertTask. That function has a thread-safety check, and + // the rest of this method is using thread-safe operations so we don't + // need any extra debug-mode checking here. + let scheduled: Scheduled + switch self._prepareToSchedule(returnType: T.self) { + case .doSchedule(let taskID, let promise, let returned): + scheduled = returned + self.insertTask(taskID: taskID, deadline: deadline, promise: promise, task: task) + case .returnImmediately(let returned): + scheduled = returned + } + + return scheduled + } + + @discardableResult + public func _scheduleTaskIsolatedUnsafeUnchecked( + in delay: TimeAmount, + _ task: @escaping () throws -> T + ) -> Scheduled { + self._scheduleTaskIsolatedUnsafeUnchecked(deadline: self.now + delay, task) + } + public func preconditionInEventLoop(file: StaticString, line: UInt) { dispatchPrecondition(condition: .onQueue(self.queue)) } @@ -419,6 +461,45 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { dispatchPrecondition(condition: .notOnQueue(self.queue)) } + enum ScheduleCreationResult { + case doSchedule(taskID: UInt64, promise: EventLoopPromise, scheduled: Scheduled) + case returnImmediately(Scheduled) + } + private func _prepareToSchedule( + returnType: ReturnType.Type = ReturnType.self + ) -> ScheduleCreationResult { + let promise = self.makePromise(of: ReturnType.self) + + switch self.state.load(ordering: .acquiring) { + case .open: + break + case .closing, .closed: + // If the event loop is shut down, or shutting down, immediately cancel the task. + promise.fail(EventLoopError.cancelled) + return .returnImmediately(Scheduled(promise: promise, cancellationTask: {})) + } + + let taskID = self.scheduledTaskCounter.loadThenWrappingIncrement(ordering: .relaxed) + + let scheduled = Scheduled( + promise: promise, + cancellationTask: { + if self.inEventLoop { + self.removeTask(taskID: taskID) + } else { + self.queue.async { + self.removeTask(taskID: taskID) + } + } + } + ) + return .doSchedule( + taskID: taskID, + promise: promise, + scheduled: scheduled + ) + } + deinit { precondition(scheduledTasks.isEmpty, "NIOAsyncTestingEventLoop freed with unexecuted scheduled tasks!") }