Skip to content

Commit

Permalink
Call the cancellationTask of Scheduled directly
Browse files Browse the repository at this point in the history
### Motivation:

In my previous PR apple#2010, I was able to decrease the allocations for both `scheduleTask` and `execute` by 1 already. Gladly, there are no more allocations left to remove from `execute` now; however, `scheduleTask` still provides a couple of allocations that we can try to get rid of.

### Modifications:

This PR removes two allocations inside `Scheduled` where we were using the passed in `EventLoopPromise` to call the `cancellationTask` once the `EventLoopFuture` of the promise fails. This requires two allocations inside `whenFailure` and inside `_whenComplete`. However, since we are passing the `cancellationTask` to `Scheduled` anyhow and `Scheduled` is also the one that is failing the promise from the `cancel()` method. We can just go ahead and store the `cancellationTask` inside `Scheduled` and call it from the `cancel()` method directly instead of going through the future.

Importantly, here is that the `cancellationTask` is not allowed to retain the `ScheduledTask.task` otherwise we would change the semantics and retain the `ScheduledTask.task` longer than necessary. My previous PR apple#2010, already implemented the work to get rid of the retain from the `cancellationTask` closure. So we are good to go ahead and store the `cancellationTask` inside `Scheduled` now

### Result:

`scheduleTask` requires two fewer allocations
  • Loading branch information
FranzBusch committed Dec 14, 2021
1 parent 5b75a75 commit 026d3ad
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 29 deletions.
12 changes: 4 additions & 8 deletions Sources/NIOCore/EventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@ import Dispatch
/// will be notified once the execution is complete.
public struct Scheduled<T> {
/* private but usableFromInline */ @usableFromInline let _promise: EventLoopPromise<T>

@usableFromInline let cancellationTask: (() -> Void)

@inlinable
public init(promise: EventLoopPromise<T>, cancellationTask: @escaping () -> Void) {
self._promise = promise
promise.futureResult.whenFailure { error in
guard let err = error as? EventLoopError else {
return
}
if err == .cancelled {
cancellationTask()
}
}
self.cancellationTask = cancellationTask
}

/// Try to cancel the execution of the scheduled task.
Expand All @@ -42,6 +37,7 @@ public struct Scheduled<T> {
@inlinable
public func cancel() {
self._promise.fail(EventLoopError.cancelled)
self.cancellationTask()
}

/// Returns the `EventLoopFuture` which will be notified once the execution of the scheduled task completes.
Expand Down
12 changes: 6 additions & 6 deletions docker/docker-compose.1604.53.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ services:
test:
image: swift-nio:16.04-5.3
environment:
- MAX_ALLOCS_ALLOWED_1000_addHandlers=46050
- MAX_ALLOCS_ALLOWED_1000_addHandlers_sync=39050
- MAX_ALLOCS_ALLOWED_1000_addHandlers=48050
- MAX_ALLOCS_ALLOWED_1000_addHandlers_sync=41050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlercontext=9050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlername=9050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlertype=9050
- MAX_ALLOCS_ALLOWED_1000_autoReadGetAndSet=25050
- MAX_ALLOCS_ALLOWED_1000_autoReadGetAndSet_sync=0
- MAX_ALLOCS_ALLOWED_1000_getHandlers=9050
- MAX_ALLOCS_ALLOWED_1000_getHandlers_sync=36
- MAX_ALLOCS_ALLOWED_1000_getHandlers_sync=38
- MAX_ALLOCS_ALLOWED_1000_reqs_1_conn=30400
- MAX_ALLOCS_ALLOWED_1000_tcpbootstraps=4050
- MAX_ALLOCS_ALLOWED_1000_tcpconnections=163050
- MAX_ALLOCS_ALLOWED_1000_udp_reqs=12050
- MAX_ALLOCS_ALLOWED_1000_udp_reqs=12000
- MAX_ALLOCS_ALLOWED_1000_udpbootstraps=2050
- MAX_ALLOCS_ALLOWED_1000_udpconnections=89050
- MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=428050
- MAX_ALLOCS_ALLOWED_1000_udpconnections=88850
- MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=428000
- MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2050
- MAX_ALLOCS_ALLOWED_creating_10000_headers=0
- MAX_ALLOCS_ALLOWED_decode_1000_ws_frames=2050
Expand Down
8 changes: 4 additions & 4 deletions docker/docker-compose.1804.52.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ services:
test:
image: swift-nio:18.04-5.2
environment:
- MAX_ALLOCS_ALLOWED_1000_addHandlers=46050
- MAX_ALLOCS_ALLOWED_1000_addHandlers_sync=39050
- MAX_ALLOCS_ALLOWED_1000_addHandlers=48050
- MAX_ALLOCS_ALLOWED_1000_addHandlers_sync=41050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlercontext=9050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlername=9050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlertype=9050
- MAX_ALLOCS_ALLOWED_1000_autoReadGetAndSet=28050
- MAX_ALLOCS_ALLOWED_1000_autoReadGetAndSet_sync=0
- MAX_ALLOCS_ALLOWED_1000_getHandlers=9050
- MAX_ALLOCS_ALLOWED_1000_getHandlers_sync=36
- MAX_ALLOCS_ALLOWED_1000_getHandlers_sync=38
- MAX_ALLOCS_ALLOWED_1000_reqs_1_conn=30450
- MAX_ALLOCS_ALLOWED_1000_tcpbootstraps=4050
- MAX_ALLOCS_ALLOWED_1000_tcpconnections=164050
- MAX_ALLOCS_ALLOWED_1000_udp_reqs=12000
- MAX_ALLOCS_ALLOWED_1000_udpbootstraps=2050
- MAX_ALLOCS_ALLOWED_1000_udpconnections=89750
- MAX_ALLOCS_ALLOWED_1000_udpconnections=89800
- MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=433000
- MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2050
- MAX_ALLOCS_ALLOWED_creating_10000_headers=0
Expand Down
8 changes: 4 additions & 4 deletions docker/docker-compose.2004.54.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ services:
test:
image: swift-nio:20.04-5.4
environment:
- MAX_ALLOCS_ALLOWED_1000_addHandlers=46050
- MAX_ALLOCS_ALLOWED_1000_addHandlers_sync=39050
- MAX_ALLOCS_ALLOWED_1000_addHandlers=48050
- MAX_ALLOCS_ALLOWED_1000_addHandlers_sync=41050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlercontext=9050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlername=9050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlertype=9050
- MAX_ALLOCS_ALLOWED_1000_autoReadGetAndSet=25050
- MAX_ALLOCS_ALLOWED_1000_autoReadGetAndSet_sync=0
- MAX_ALLOCS_ALLOWED_1000_getHandlers=9050
- MAX_ALLOCS_ALLOWED_1000_getHandlers_sync=36
- MAX_ALLOCS_ALLOWED_1000_getHandlers_sync=38
- MAX_ALLOCS_ALLOWED_1000_reqs_1_conn=30450
- MAX_ALLOCS_ALLOWED_1000_tcpbootstraps=4050
- MAX_ALLOCS_ALLOWED_1000_tcpconnections=165050
- MAX_ALLOCS_ALLOWED_1000_udp_reqs=12050
- MAX_ALLOCS_ALLOWED_1000_udp_reqs=12000
- MAX_ALLOCS_ALLOWED_1000_udpbootstraps=2050
- MAX_ALLOCS_ALLOWED_1000_udpconnections=89950
- MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=431000
Expand Down
8 changes: 4 additions & 4 deletions docker/docker-compose.2004.55.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ services:
test:
image: swift-nio:20.04-5.5
environment:
- MAX_ALLOCS_ALLOWED_1000_addHandlers=46050
- MAX_ALLOCS_ALLOWED_1000_addHandlers_sync=39050
- MAX_ALLOCS_ALLOWED_1000_addHandlers=48050
- MAX_ALLOCS_ALLOWED_1000_addHandlers_sync=41050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlercontext=9050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlername=9050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlertype=9050
- MAX_ALLOCS_ALLOWED_1000_autoReadGetAndSet=25050
- MAX_ALLOCS_ALLOWED_1000_autoReadGetAndSet_sync=0
- MAX_ALLOCS_ALLOWED_1000_getHandlers=9050
- MAX_ALLOCS_ALLOWED_1000_getHandlers_sync=36
- MAX_ALLOCS_ALLOWED_1000_getHandlers_sync=38
- MAX_ALLOCS_ALLOWED_1000_reqs_1_conn=30450
- MAX_ALLOCS_ALLOWED_1000_tcpbootstraps=4050
- MAX_ALLOCS_ALLOWED_1000_tcpconnections=165050
- MAX_ALLOCS_ALLOWED_1000_udp_reqs=12050
- MAX_ALLOCS_ALLOWED_1000_udpbootstraps=2050
- MAX_ALLOCS_ALLOWED_1000_udpconnections=90000
- MAX_ALLOCS_ALLOWED_1000_udpconnections=89850
- MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=431000
- MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2050
- MAX_ALLOCS_ALLOWED_creating_10000_headers=0
Expand Down
6 changes: 3 additions & 3 deletions docker/docker-compose.2004.main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ services:
test:
image: swift-nio:20.04-main
environment:
- MAX_ALLOCS_ALLOWED_1000_addHandlers=46050
- MAX_ALLOCS_ALLOWED_1000_addHandlers_sync=39050
- MAX_ALLOCS_ALLOWED_1000_addHandlers=48050
- MAX_ALLOCS_ALLOWED_1000_addHandlers_sync=41050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlercontext=9050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlername=9050
- MAX_ALLOCS_ALLOWED_1000_addRemoveHandlers_handlertype=9050
- MAX_ALLOCS_ALLOWED_1000_autoReadGetAndSet=24050
- MAX_ALLOCS_ALLOWED_1000_autoReadGetAndSet_sync=0
- MAX_ALLOCS_ALLOWED_1000_getHandlers=9050
- MAX_ALLOCS_ALLOWED_1000_getHandlers_sync=36
- MAX_ALLOCS_ALLOWED_1000_getHandlers_sync=38
- MAX_ALLOCS_ALLOWED_1000_reqs_1_conn=30400
- MAX_ALLOCS_ALLOWED_1000_tcpbootstraps=4050
- MAX_ALLOCS_ALLOWED_1000_tcpconnections=161050
Expand Down

0 comments on commit 026d3ad

Please sign in to comment.