Skip to content

Commit

Permalink
Avoid 2 heap allocations on child channel creation (apple#115)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lukasa authored and weissi committed May 16, 2019
1 parent d66cd24 commit 016bf0b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
9 changes: 6 additions & 3 deletions Sources/NIOHTTP2/HTTP2StreamChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -124,8 +126,9 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
} else {
self.errorEncountered(error: error)
}

promise?.fail(error)
}
return f
}

/// Activates this channel.
Expand Down
8 changes: 2 additions & 6 deletions Sources/NIOHTTP2/HTTP2StreamMultiplexer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
}
}

0 comments on commit 016bf0b

Please sign in to comment.