Reduce object creation on Http2FrameCodec (#9333)

Motivation:

We don't need the extra ChannelPromise when writing headers anymore in Http2FrameCodec. This also means we cal re-use a ChannelFutureListener and so not need to create new instances all the time.

Modifications:

- Just pass the original ChannelPromise when writing headers
- Reuse the ChannelFutureListener

Result:

Two less objects created when writing headers for an not-yet created stream.
This commit is contained in:
Norman Maurer 2019-07-06 09:08:20 +02:00
parent c5289fbe92
commit f47cf0c4d3

View File

@ -17,7 +17,6 @@ package io.netty.handler.codec.http2;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler; import io.netty.channel.ChannelInboundHandler;
@ -159,6 +158,7 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
private int numBufferedStreams; private int numBufferedStreams;
private final IntObjectMap<DefaultHttp2FrameStream> frameStreamToInitializeMap = private final IntObjectMap<DefaultHttp2FrameStream> frameStreamToInitializeMap =
new IntObjectHashMap<DefaultHttp2FrameStream>(8); new IntObjectHashMap<DefaultHttp2FrameStream>(8);
private final ChannelFutureListener bufferedStreamsListener = future -> numBufferedStreams--;
Http2FrameCodec(Http2ConnectionEncoder encoder, Http2ConnectionDecoder decoder, Http2Settings initialSettings, Http2FrameCodec(Http2ConnectionEncoder encoder, Http2ConnectionDecoder decoder, Http2Settings initialSettings,
boolean decoupleCloseAndGoAway) { boolean decoupleCloseAndGoAway) {
@ -393,34 +393,15 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
// We should not re-use ids. // We should not re-use ids.
assert old == null; assert old == null;
// TODO(buchgr): Once Http2FrameStream and Http2Stream are merged this is no longer necessary.
final ChannelPromise writePromise = ctx.newPromise();
encoder().writeHeaders(ctx, streamId, headersFrame.headers(), headersFrame.padding(), encoder().writeHeaders(ctx, streamId, headersFrame.headers(), headersFrame.padding(),
headersFrame.isEndStream(), writePromise); headersFrame.isEndStream(), promise);
if (writePromise.isDone()) { if (!promise.isDone()) {
notifyHeaderWritePromise(writePromise, promise);
} else {
numBufferedStreams++; numBufferedStreams++;
promise.addListener(bufferedStreamsListener);
writePromise.addListener((ChannelFutureListener) future -> {
numBufferedStreams--;
notifyHeaderWritePromise(future, promise);
});
} }
} }
} }
private static void notifyHeaderWritePromise(ChannelFuture future, ChannelPromise promise) {
Throwable cause = future.cause();
if (cause == null) {
promise.setSuccess();
} else {
promise.setFailure(cause);
}
}
private void onStreamActive0(Http2Stream stream) { private void onStreamActive0(Http2Stream stream) {
if (stream.id() != Http2CodecUtil.HTTP_UPGRADE_STREAM_ID && if (stream.id() != Http2CodecUtil.HTTP_UPGRADE_STREAM_ID &&
connection().local().isValidStreamId(stream.id())) { connection().local().isValidStreamId(stream.id())) {
@ -507,7 +488,7 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
} }
} }
void onHttp2UnknownStreamError(@SuppressWarnings("unused") ChannelHandlerContext ctx, Throwable cause, private void onHttp2UnknownStreamError(@SuppressWarnings("unused") ChannelHandlerContext ctx, Throwable cause,
Http2Exception.StreamException streamException) { Http2Exception.StreamException streamException) {
// Just log.... // Just log....
LOG.warn("Stream exception thrown for unknown stream {}.", streamException.streamId(), cause); LOG.warn("Stream exception thrown for unknown stream {}.", streamException.streamId(), cause);
@ -610,11 +591,11 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
} }
} }
void onUpgradeEvent(ChannelHandlerContext ctx, UpgradeEvent evt) { private void onUpgradeEvent(ChannelHandlerContext ctx, UpgradeEvent evt) {
ctx.fireUserEventTriggered(evt); ctx.fireUserEventTriggered(evt);
} }
void onHttp2StreamWritabilityChanged(ChannelHandlerContext ctx, Http2FrameStream stream, private void onHttp2StreamWritabilityChanged(ChannelHandlerContext ctx, Http2FrameStream stream,
@SuppressWarnings("unused") boolean writable) { @SuppressWarnings("unused") boolean writable) {
ctx.fireUserEventTriggered(Http2FrameStreamEvent.writabilityChanged(stream)); ctx.fireUserEventTriggered(Http2FrameStreamEvent.writabilityChanged(stream));
} }