Reuse Http2FrameStreamEvent instances to reduce GC pressure (#9392)

Motivation:

We can easily reuse the Http2FrameStreamEvent instances and so reduce GC pressure as there may be multiple events per streams over the life-time.

Modifications:

Reuse instances

Result:

Less allocations
This commit is contained in:
Norman Maurer 2019-07-21 20:35:35 +02:00
parent e2525ced48
commit f6005c5b2d
2 changed files with 15 additions and 13 deletions

View File

@ -415,7 +415,7 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
return;
}
Http2FrameStream stream2 = newStream().setStreamAndProperty(streamKey, stream);
DefaultHttp2FrameStream stream2 = newStream().setStreamAndProperty(streamKey, stream);
onHttp2StreamStateChanged(ctx, stream2);
}
@ -446,7 +446,7 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
}
private void onHttp2StreamStateChanged0(Http2Stream stream) {
Http2FrameStream stream2 = stream.getProperty(streamKey);
DefaultHttp2FrameStream stream2 = stream.getProperty(streamKey);
if (stream2 != null) {
onHttp2StreamStateChanged(ctx, stream2);
}
@ -603,13 +603,13 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
ctx.fireUserEventTriggered(evt);
}
private void onHttp2StreamWritabilityChanged(ChannelHandlerContext ctx, Http2FrameStream stream,
private void onHttp2StreamWritabilityChanged(ChannelHandlerContext ctx, DefaultHttp2FrameStream stream,
@SuppressWarnings("unused") boolean writable) {
ctx.fireUserEventTriggered(Http2FrameStreamEvent.writabilityChanged(stream));
ctx.fireUserEventTriggered(stream.writabilityChanged);
}
void onHttp2StreamStateChanged(ChannelHandlerContext ctx, Http2FrameStream stream) {
ctx.fireUserEventTriggered(Http2FrameStreamEvent.stateChanged(stream));
void onHttp2StreamStateChanged(ChannelHandlerContext ctx, DefaultHttp2FrameStream stream) {
ctx.fireUserEventTriggered(stream.stateChanged);
}
void onHttp2Frame(ChannelHandlerContext ctx, Http2Frame frame) {
@ -623,7 +623,7 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
private final class Http2RemoteFlowControllerListener implements Http2RemoteFlowController.Listener {
@Override
public void writabilityChanged(Http2Stream stream) {
Http2FrameStream frameStream = stream.getProperty(streamKey);
DefaultHttp2FrameStream frameStream = stream.getProperty(streamKey);
if (frameStream == null) {
return;
}
@ -641,6 +641,9 @@ public class Http2FrameCodec extends Http2ConnectionHandler {
private volatile int id = -1;
volatile Http2Stream stream;
final Http2FrameStreamEvent stateChanged = Http2FrameStreamEvent.stateChanged(this);
final Http2FrameStreamEvent writabilityChanged = Http2FrameStreamEvent.writabilityChanged(this);
Channel attachment;
DefaultHttp2FrameStream setStreamAndProperty(PropertyKey streamKey, Http2Stream stream) {

View File

@ -166,23 +166,22 @@ public class Http2MultiplexCodec extends Http2FrameCodec {
}
@Override
final void onHttp2StreamStateChanged(ChannelHandlerContext ctx, Http2FrameStream stream) {
DefaultHttp2FrameStream s = (DefaultHttp2FrameStream) stream;
final void onHttp2StreamStateChanged(ChannelHandlerContext ctx, DefaultHttp2FrameStream stream) {
switch (stream.state()) {
case HALF_CLOSED_LOCAL:
if (stream.id() == HTTP_UPGRADE_STREAM_ID) {
onHttp2UpgradeStreamInitialized(ctx, s);
onHttp2UpgradeStreamInitialized(ctx, stream);
}
break;
case HALF_CLOSED_REMOTE:
case OPEN:
if (s.attachment != null) {
if (stream.attachment != null) {
// ignore if child channel was already created.
break;
}
// fall-trough
ChannelFuture future = new Http2MultiplexCodecStreamChannel(s, inboundStreamHandler).register();
ChannelFuture future = new Http2MultiplexCodecStreamChannel(stream, inboundStreamHandler).register();
if (future.isDone()) {
Http2MultiplexHandler.registerDone(future);
} else {
@ -190,7 +189,7 @@ public class Http2MultiplexCodec extends Http2FrameCodec {
}
break;
case CLOSED:
AbstractHttp2StreamChannel channel = (AbstractHttp2StreamChannel) s.attachment;
AbstractHttp2StreamChannel channel = (AbstractHttp2StreamChannel) stream.attachment;
if (channel != null) {
channel.streamClosed();
}