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.
This commit is contained in:
Cory Benfield authored and Johannes Weiss committed 2019-05-16 14:57:52 +01:00
1 parent d66cd24206
commit 016bf0be4d
2 files changed
+8 -9

No files matched your search

+6 -3
View File
@@ -100,8 +100,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
self._pipeline = ChannelPipeline(channel: self)
}
@discardableResult
internal func configure(initializer: ((Channel, HTTP2StreamID) -> EventLoopFuture<Void>)?) -> EventLoopFuture<Void> {
internal func configure(initializer: ((Channel, HTTP2StreamID) -> EventLoopFuture<Void>)?, userPromise promise: EventLoopPromise<Channel>?){
// 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.
@@ -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)
}
}
}
}