Remove unnecessary promise allocation in Selector.closeGently(...) (#237)

Motivation:

We can remove a promise allocation in Selector.closeGently if there are still Channels registered and also simplify the code a bit.

Modifications:

Remove creation of promise and just use eventLoop.new*Future(...) when needed.

Result:

Less allocations and cleaner code.
This commit is contained in:
Norman Maurer 2018-03-26 19:50:20 +02:00 committed by GitHub
parent 0a23199d7d
commit 774f298dca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 6 deletions

View File

@ -487,10 +487,8 @@ struct SelectorEvent<R> {
internal extension Selector where R == NIORegistration {
/// Gently close the `Selector` after all registered `Channel`s are closed.
internal func closeGently(eventLoop: EventLoop) -> EventLoopFuture<Void> {
let p0: EventLoopPromise<Void> = eventLoop.newPromise()
guard self.lifecycleState == .open else {
p0.fail(error: IOError(errnoCode: EBADF, reason: "can't close selector gently as it's \(self.lifecycleState)."))
return p0.futureResult
return eventLoop.newFailedFuture(error: IOError(errnoCode: EBADF, reason: "can't close selector gently as it's \(self.lifecycleState)."))
}
let futures: [EventLoopFuture<Void>] = self.registrations.map { (_, reg: NIORegistration) -> EventLoopFuture<Void> in
@ -514,11 +512,9 @@ internal extension Selector where R == NIORegistration {
}
guard futures.count > 0 else {
p0.succeed(result: ())
return p0.futureResult
return eventLoop.newSucceededFuture(result: ())
}
p0.succeed(result: ())
return EventLoopFuture<Void>.andAll(futures, eventLoop: eventLoop)
}
}