Try to reduce GC produced while writing headers (#9682)

Motivation:

bbc34d0eda introduced correct handling of "in process" setup of streams but there is some room for improvements. Often the writeHeaders(...) is completed directly which means there is not need to create the extra listener object.

Modifications:

- Only create the listener if we really need too.

Result:

Less GC
This commit is contained in:
Norman Maurer 2019-10-17 10:12:20 -07:00 committed by GitHub
parent 19b4adf79c
commit 95230e01da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -159,12 +159,6 @@ 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 = new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
numBufferedStreams--;
}
};
Http2FrameCodec(Http2ConnectionEncoder encoder, Http2ConnectionDecoder decoder, Http2Settings initialSettings, Http2FrameCodec(Http2ConnectionEncoder encoder, Http2ConnectionDecoder decoder, Http2Settings initialSettings,
boolean decoupleCloseAndGoAway) { boolean decoupleCloseAndGoAway) {
@ -420,26 +414,33 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
// We should not re-use ids. // We should not re-use ids.
assert old == null; assert old == null;
// Clean up the stream being initialized if writing the headers fails.
promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) {
if (!channelFuture.isSuccess()) {
frameStreamToInitializeMap.remove(streamId);
}
}
});
encoder().writeHeaders(ctx, streamId, headersFrame.headers(), headersFrame.padding(), encoder().writeHeaders(ctx, streamId, headersFrame.headers(), headersFrame.padding(),
headersFrame.isEndStream(), promise); headersFrame.isEndStream(), promise);
if (!promise.isDone()) { if (!promise.isDone()) {
numBufferedStreams++; numBufferedStreams++;
promise.addListener(bufferedStreamsListener); // Clean up the stream being initialized if writing the headers fails and also
// decrement the number of buffered streams.
promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture channelFuture) {
numBufferedStreams--;
handleHeaderFuture(channelFuture, streamId);
}
});
} else {
handleHeaderFuture(promise, streamId);
} }
} }
} }
private void handleHeaderFuture(ChannelFuture channelFuture, int streamId) {
if (!channelFuture.isSuccess()) {
frameStreamToInitializeMap.remove(streamId);
}
}
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())) {