Skip to content

Commit

Permalink
Save expensive slow path type checks (#7942)
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 authored Feb 22, 2023
1 parent 3440e48 commit 81e5aa8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion frameworks/Java/netty/netty.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ COPY --from=maven /netty/target/netty-example-0.1-jar-with-dependencies.jar app.

EXPOSE 8080

CMD ["java", "-server", "-XX:+UseNUMA", "-XX:+UseParallelGC", "-XX:+AggressiveOpts", "-jar", "app.jar"]
CMD ["java", "-server", "-XX:+UseNUMA", "-XX:+UseParallelGC", "-XX:+AggressiveOpts", "-Dio.netty.buffer.checkBounds=false", "-Dio.netty.buffer.checkAccessible=false", "-jar", "app.jar"]
5 changes: 3 additions & 2 deletions frameworks/Java/netty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<netty.version>4.1.86.Final</netty.version>
<netty.version>4.1.89.Final</netty.version>
<io_uring.version>0.0.18.Final</io_uring.version>
</properties>

<packaging>jar</packaging>
Expand Down Expand Up @@ -41,7 +42,7 @@
<dependency>
<groupId>io.netty.incubator</groupId>
<artifactId>netty-incubator-transport-native-io_uring</artifactId>
<version>0.0.15.Final</version>
<version>${io_uring.version}</version>
<classifier>linux-x86_64</classifier>
</dependency>

Expand Down
16 changes: 16 additions & 0 deletions frameworks/Java/netty/src/main/java/hello/HelloServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.AsciiString;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
Expand Down Expand Up @@ -88,6 +90,20 @@ public void run() {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// fast path
if (msg == LastHttpContent.EMPTY_LAST_CONTENT) {
return;
}
if (msg.getClass() == DefaultHttpRequest.class) {
DefaultHttpRequest request = (DefaultHttpRequest) msg;
process(ctx, request);
} else {
channelReadSlowPath(ctx, msg);
}
}

private void channelReadSlowPath(ChannelHandlerContext ctx, Object msg) throws Exception {
// slow path
if (msg instanceof HttpRequest) {
try {
HttpRequest request = (HttpRequest) msg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpVersion;

public class HelloServerInitializer extends ChannelInitializer<SocketChannel> {

private ScheduledExecutorService service;
private final ScheduledExecutorService service;

public HelloServerInitializer(ScheduledExecutorService service) {
this.service = service;
Expand All @@ -18,8 +23,29 @@ public HelloServerInitializer(ScheduledExecutorService service) {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast("encoder", new HttpResponseEncoder())
.addLast("decoder", new HttpRequestDecoder(4096, 8192, 8192, false))
.addLast("encoder", new HttpResponseEncoder() {
@Override
public boolean acceptOutboundMessage(final Object msg) throws Exception {
if (msg.getClass() == DefaultFullHttpResponse.class) {
return true;
}
return super.acceptOutboundMessage(msg);
}
})
.addLast("decoder", new HttpRequestDecoder(4096, 8192, 8192, false) {

@Override
protected HttpMessage createMessage(final String[] initialLine) throws Exception {
return new DefaultHttpRequest(
HttpVersion.valueOf(initialLine[2]),
HttpMethod.valueOf(initialLine[0]), initialLine[1], validateHeaders);
}

@Override
protected boolean isContentAlwaysEmpty(final HttpMessage msg) {
return false;
}
})
.addLast("handler", new HelloServerHandler(service));
}
}

0 comments on commit 81e5aa8

Please sign in to comment.