Skip to content

Commit 37ce3bc

Browse files
committed
performance: use cached version of well-known headers
1 parent cca182d commit 37ce3bc

File tree

6 files changed

+51
-21
lines changed

6 files changed

+51
-21
lines changed

.envrc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function setjdk() {
2+
if [ $# -ne 0 ]; then
3+
removeFromPath '/System/Library/Frameworks/JavaVM.framework/Home/bin'
4+
if [ -n "${JAVA_HOME+x}" ]; then
5+
removeFromPath $JAVA_HOME/bin
6+
fi
7+
export JAVA_HOME=`/usr/libexec/java_home -v $@`
8+
export PATH=$JAVA_HOME/bin:$PATH
9+
fi
10+
}
11+
function removeFromPath() {
12+
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
13+
}
14+
setjdk 21

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,3 @@ node_modules
3737
build
3838
.gradle
3939
!gradle-wrapper.jar
40-
.envrc

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyContext.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
package io.jooby.internal.netty;
77

88
import static io.jooby.internal.netty.NettyHeadersFactory.HEADERS;
9+
import static io.jooby.internal.netty.NettyString.CONTENT_LENGTH;
10+
import static io.jooby.internal.netty.NettyString.ZERO;
911
import static io.netty.buffer.Unpooled.wrappedBuffer;
10-
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
1112
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
1213
import static io.netty.handler.codec.http.HttpHeaderNames.RANGE;
1314
import static io.netty.handler.codec.http.HttpHeaderNames.SET_COOKIE;
@@ -546,13 +547,13 @@ public Context send(@NonNull String data) {
546547
return send(data, UTF_8);
547548
}
548549

549-
@Override
550-
public final Context send(String data, Charset charset) {
550+
@NonNull @Override
551+
public final Context send(@NonNull String data, @NonNull Charset charset) {
551552
return send(wrappedBuffer(data.getBytes(charset)));
552553
}
553554

554-
@Override
555-
public final Context send(byte[] data) {
555+
@NonNull @Override
556+
public final Context send(@NonNull byte[] data) {
556557
return send(wrappedBuffer(data));
557558
}
558559

@@ -566,8 +567,8 @@ public Context send(@NonNull ByteBuffer[] data) {
566567
return send(Unpooled.wrappedBuffer(data));
567568
}
568569

569-
@Override
570-
public final Context send(ByteBuffer data) {
570+
@NonNull @Override
571+
public final Context send(@NonNull ByteBuffer data) {
571572
return send(wrappedBuffer(data));
572573
}
573574

@@ -738,12 +739,12 @@ public Context setResetHeadersOnError(boolean value) {
738739
}
739740

740741
@NonNull @Override
741-
public Context send(StatusCode statusCode) {
742+
public Context send(@NonNull StatusCode statusCode) {
742743
try {
743-
setResponseCode(statusCode);
744+
setResponseCode(statusCode.value());
744745
responseStarted = true;
745-
if (!setHeaders.contains(CONTENT_LENGTH)) {
746-
setHeaders.set(CONTENT_LENGTH, "0");
746+
if (contentLength == -1) {
747+
setHeaders.set(CONTENT_LENGTH, ZERO);
747748
}
748749
var rsp =
749750
new DefaultFullHttpResponse(

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyHandler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package io.jooby.internal.netty;
77

8+
import static io.jooby.internal.netty.NettyString.*;
89
import static io.jooby.internal.netty.SlowPathChecks.*;
910
import static io.netty.handler.codec.http.HttpUtil.isTransferEncodingChunked;
1011

@@ -25,11 +26,6 @@
2526

2627
public class NettyHandler extends ChannelInboundHandlerAdapter {
2728
private final Logger log = LoggerFactory.getLogger(NettyServer.class);
28-
private static final CharSequence server = NettyString.of("N");
29-
public static final CharSequence CONTENT_TYPE = NettyString.of("content-type");
30-
public static final CharSequence TEXT_PLAIN = NettyString.of("text/plain");
31-
public static final CharSequence DATE = NettyString.of("date");
32-
public static final CharSequence SERVER = NettyString.of("server");
3329
private final NettyDateService serverDate;
3430
private final List<Jooby> applications;
3531
private Router router;

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyString.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010
import edu.umd.cs.findbugs.annotations.NonNull;
1111

1212
public class NettyString implements CharSequence {
13+
static final CharSequence server = NettyString.of("N");
14+
static final CharSequence CONTENT_TYPE = NettyString.of("content-type");
15+
static final CharSequence CONTENT_LENGTH = NettyString.of("content-length");
16+
static final CharSequence ZERO = NettyString.of("0");
17+
static final CharSequence TEXT_PLAIN = NettyString.of("text/plain");
18+
static final CharSequence DATE = NettyString.of("date");
19+
static final CharSequence SERVER = NettyString.of("server");
1320

1421
final byte[] bytes;
1522
private final String value;
23+
private final int hashCode;
1624

1725
public NettyString(String value) {
1826
this.value = value;
27+
this.hashCode = value.hashCode();
1928
this.bytes = value.getBytes(StandardCharsets.US_ASCII);
2029
}
2130

@@ -38,6 +47,19 @@ public char charAt(int index) {
3847
return value.subSequence(start, end);
3948
}
4049

50+
@Override
51+
public boolean equals(Object obj) {
52+
if (obj != null && obj.getClass() == NettyString.class) {
53+
return value.equals(((NettyString) obj).value);
54+
}
55+
return false;
56+
}
57+
58+
@Override
59+
public int hashCode() {
60+
return hashCode;
61+
}
62+
4163
@Override
4264
@NonNull public String toString() {
4365
return value;

tests/src/test/java/examples/Performance.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static io.jooby.MediaType.JSON;
1010

1111
import io.jooby.Jooby;
12+
import io.jooby.ServerOptions;
1213
import io.jooby.StatusCode;
1314
import io.jooby.netty.NettyServer;
1415

@@ -38,9 +39,6 @@ public class Performance extends Jooby {
3839

3940
public static void main(final String[] args) {
4041
System.setProperty("io.netty.disableHttpHeadersValidation", "true");
41-
// runApp(args, new
42-
// UndertowServer().setOutputFactory(OutputFactory.threadLocal(OutputFactory.create())),
43-
// EVENT_LOOP, Performance::new);
44-
runApp(args, new NettyServer(), EVENT_LOOP, Performance::new);
42+
runApp(args, new NettyServer(new ServerOptions().setPort(3001)), EVENT_LOOP, Performance::new);
4543
}
4644
}

0 commit comments

Comments
 (0)