From eee18dc81ffdc00f4d187eb05699e53f135b126b Mon Sep 17 00:00:00 2001 From: Johannes Weiss Date: Fri, 6 Dec 2019 16:55:51 +0000 Subject: [PATCH] save allocations on connect (#1290) Motivation: ClientBootstrap.connect was needlessly overallocaing (for a needless promise and a needless cascade). Modifications: Remove the needless allocations. Result: Fewer allocations. --- Sources/NIO/Bootstrap.swift | 11 ++++------- docker/docker-compose.1604.51.yaml | 2 +- docker/docker-compose.1804.50.yaml | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Sources/NIO/Bootstrap.swift b/Sources/NIO/Bootstrap.swift index 540359d88..bbfd02ce2 100644 --- a/Sources/NIO/Bootstrap.swift +++ b/Sources/NIO/Bootstrap.swift @@ -526,20 +526,18 @@ public final class ClientBootstrap { let channelInitializer = self.channelInitializer ?? { _ in eventLoop.makeSucceededFuture(()) } let channelOptions = self._channelOptions - let promise = eventLoop.makePromise(of: Channel.self) let channel: SocketChannel do { channel = try SocketChannel(eventLoop: eventLoop as! SelectableEventLoop, protocolFamily: protocolFamily) - } catch let err { - promise.fail(err) - return promise.futureResult + } catch { + return eventLoop.makeFailedFuture(error) } @inline(__always) func setupChannel() -> EventLoopFuture { eventLoop.assertInEventLoop() // We need to hop to `eventLoop` as the user might have returned a future from a different `EventLoop`. - channelInitializer(channel).hop(to: eventLoop).flatMap { + return channelInitializer(channel).hop(to: eventLoop).flatMap { channelOptions.applyAllChannelOptions(to: channel) }.flatMap { channel.registerAndDoSynchronously(body) @@ -548,8 +546,7 @@ public final class ClientBootstrap { }.flatMapError { error in channel.close0(error: error, mode: .all, promise: nil) return channel.eventLoop.makeFailedFuture(error) - }.cascade(to: promise) - return promise.futureResult + } } if eventLoop.inEventLoop { diff --git a/docker/docker-compose.1604.51.yaml b/docker/docker-compose.1604.51.yaml index f92343aeb..e64189a28 100644 --- a/docker/docker-compose.1604.51.yaml +++ b/docker/docker-compose.1604.51.yaml @@ -19,7 +19,7 @@ services: image: swift-nio:16.04-5.1 environment: - MAX_ALLOCS_ALLOWED_1000_reqs_1_conn=30540 - - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=491050 + - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=490050 - MAX_ALLOCS_ALLOWED_ping_pong_1000_reqs_1_conn=4440 - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2100 - MAX_ALLOCS_ALLOWED_future_lots_of_callbacks=75010 diff --git a/docker/docker-compose.1804.50.yaml b/docker/docker-compose.1804.50.yaml index 3dd64aa5a..f6693da97 100644 --- a/docker/docker-compose.1804.50.yaml +++ b/docker/docker-compose.1804.50.yaml @@ -19,7 +19,7 @@ services: image: swift-nio:18.04-5.0 environment: - MAX_ALLOCS_ALLOWED_1000_reqs_1_conn=30990 - - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=963050 + - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=962050 - MAX_ALLOCS_ALLOWED_ping_pong_1000_reqs_1_conn=4500 - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2100 - MAX_ALLOCS_ALLOWED_future_lots_of_callbacks=75010