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.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
@ -159,6 +158,7 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
private int numBufferedStreams;
private final IntObjectMap<DefaultHttp2FrameStream> frameStreamToInitializeMap =
new IntObjectHashMap<DefaultHttp2FrameStream>(8);
private final ChannelFutureListener bufferedStreamsListener = future -> numBufferedStreams--;
Http2FrameCodec(Http2ConnectionEncoder encoder, Http2ConnectionDecoder decoder, Http2Settings initialSettings,
boolean decoupleCloseAndGoAway) {
@ -393,34 +393,15 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
// We should not re-use ids.
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(),
headersFrame.isEndStream(), writePromise);
if (writePromise.isDone()) {
notifyHeaderWritePromise(writePromise, promise);
} else {
headersFrame.isEndStream(), promise);
if (!promise.isDone()) {
numBufferedStreams++;
writePromise.addListener((ChannelFutureListener) future -> {
numBufferedStreams--;
notifyHeaderWritePromise(future, promise);
});
promise.addListener(bufferedStreamsListener);
}
}
}
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) {
if (stream.id() != Http2CodecUtil.HTTP_UPGRADE_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) {
// Just log....
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);
}
void onHttp2StreamWritabilityChanged(ChannelHandlerContext ctx, Http2FrameStream stream,
private void onHttp2StreamWritabilityChanged(ChannelHandlerContext ctx, Http2FrameStream stream,
@SuppressWarnings("unused") boolean writable) {
ctx.fireUserEventTriggered(Http2FrameStreamEvent.writabilityChanged(stream));
}