From 016bf0be4d0aa0fbe464935da553880867fd09ea Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 16 May 2019 14:57:52 +0100 Subject: [PATCH] Avoid 2 heap allocations on child channel creation (#115) Motivation: When users create a child channel and want to get hold of it, they will pass a promise to createStreamChannel. This promise will force a call to map{} and a call to cascade(), both of which heap allocate. We don't need to have these heap allocations, and they're easily removed, so we should. Modifications: Manually cascade the result. Result: Slightly fewer heap allocations in some workloads. --- Sources/NIOHTTP2/HTTP2StreamChannel.swift | 9 ++++++--- Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift | 8 ++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Sources/NIOHTTP2/HTTP2StreamChannel.swift b/Sources/NIOHTTP2/HTTP2StreamChannel.swift index 165b87a..889fac0 100644 --- a/Sources/NIOHTTP2/HTTP2StreamChannel.swift +++ b/Sources/NIOHTTP2/HTTP2StreamChannel.swift @@ -100,8 +100,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore { self._pipeline = ChannelPipeline(channel: self) } - @discardableResult - internal func configure(initializer: ((Channel, HTTP2StreamID) -> EventLoopFuture)?) -> EventLoopFuture { + internal func configure(initializer: ((Channel, HTTP2StreamID) -> EventLoopFuture)?, userPromise promise: EventLoopPromise?){ // We need to configure this channel. This involves doing four things: // 1. Setting our autoRead state from the parent // 2. Calling the initializer, if provided. @@ -116,6 +115,9 @@ final class HTTP2StreamChannel: Channel, ChannelCore { if self.parent!.isActive { self.performActivation() } + + // We aren't using cascade here to avoid the allocations it causes. + promise?.succeed(self) } f.whenFailure { (error: Error) in @@ -124,8 +126,9 @@ final class HTTP2StreamChannel: Channel, ChannelCore { } else { self.errorEncountered(error: error) } + + promise?.fail(error) } - return f } /// Activates this channel. diff --git a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift index 331a4e7..a9d1a23 100644 --- a/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift +++ b/Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift @@ -64,7 +64,7 @@ public final class HTTP2StreamMultiplexer: ChannelInboundHandler, ChannelOutboun targetWindowSize: 65535, initiatedRemotely: true) self.streams[streamID] = channel - channel.configure(initializer: self.inboundStreamStateInitializer) + channel.configure(initializer: self.inboundStreamStateInitializer, userPromise: nil) channel.closeFuture.whenComplete { _ in self.childChannelClosed(streamID: streamID) } @@ -184,14 +184,10 @@ extension HTTP2StreamMultiplexer { targetWindowSize: 65535, // TODO: make configurable initiatedRemotely: false) self.streams[streamID] = channel - let activationFuture = channel.configure(initializer: streamStateInitializer) + channel.configure(initializer: streamStateInitializer, userPromise: promise) channel.closeFuture.whenComplete { _ in self.childChannelClosed(streamID: streamID) } - - if let promise = promise { - activationFuture.map { channel }.cascade(to: promise) - } } } }