GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Optimize HTTP startline validation (#16030) (#16042) · netty/netty@54ba03e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src/main/java/io/netty/handler/codec/http
    • DefaultHttpRequest.java
    • HttpUtil.java
  • microbench/src/main/java/io/netty/handler/codec/http
    • HttpRequestEncoderInsertBenchmark.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpRequest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/DefaultHttpRequest.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public DefaultHttpRequest(HttpVersion httpVersion, HttpMethod method, String uri
9292
this.method = checkNotNull(method, "method");
9393
this.uri = checkNotNull(uri, "uri");
9494
if (validateRequestLine) {
95-
HttpUtil.validateRequestLineTokens(httpVersion, method, uri);
95+
HttpUtil.validateRequestLineTokens(method, uri);
9696
}
9797
}
9898

Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/http/HttpUtil.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/http/HttpUtil.java
+11-23Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public final class HttpUtil {
4040
private static final AsciiString CHARSET_EQUALS = AsciiString.of(HttpHeaderValues.CHARSET + "=");
4141
private static final AsciiString SEMICOLON = AsciiString.cached(";");
4242
private static final String COMMA_STRING = String.valueOf(COMMA);
43-
private static final long ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK = 1L << '\n' | 1L << '\r' | 1L << ' ';
4443

4544
private HttpUtil() { }
4645

@@ -76,7 +75,7 @@ public static boolean isAsteriskForm(String uri) {
7675
return "*".equals(uri);
7776
}
7877

79-
static void validateRequestLineTokens(HttpVersion httpVersion, HttpMethod method, String uri) {
78+
static void validateRequestLineTokens(HttpMethod method, String uri) {
8079
// The HttpVersion class does its own validation, and it's not possible for subclasses to circumvent it.
8180
// The HttpMethod class does its own validation, but subclasses might circumvent it.
8281
if (method.getClass() != HttpMethod.class) {
@@ -104,33 +103,22 @@ static void validateRequestLineTokens(HttpVersion httpVersion, HttpMethod method
104103
* otherwise {@code false}.
105104
*/
106105
public static boolean isEncodingSafeStartLineToken(CharSequence token) {
107-
int i = 0;
108106
int lenBytes = token.length();
109-
int modulo = lenBytes % 4;
110-
int lenInts = modulo == 0 ? lenBytes : lenBytes - modulo;
111-
for (; i < lenInts; i += 4) {
112-
long chars = charMask(token, i) |
113-
charMask(token, i + 1) |
114-
charMask(token, i + 2) |
115-
charMask(token, i + 3);
116-
if ((chars & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
117-
return false;
118-
}
119-
}
120-
for (; i < lenBytes; i++) {
121-
long ch = charMask(token, i);
122-
if ((ch & ILLEGAL_REQUEST_LINE_TOKEN_OCTET_MASK) != 0) {
123-
return false;
107+
for (int i = 0; i < lenBytes; i++) {
108+
char ch = token.charAt(i);
109+
// this is to help AOT compiled code which cannot profile the switch
110+
if (ch <= ' ') {
111+
switch (ch) {
112+
case '\n':
113+
case '\r':
114+
case ' ':
115+
return false;
116+
}
124117
}
125118
}
126119
return true;
127120
}
128121

129-
private static long charMask(CharSequence token, int i) {
130-
char c = token.charAt(i);
131-
return c < 64 ? 1L << c : 0;
132-
}
133-
134122
/**
135123
* Returns {@code true} if and only if the connection can remain open and
136124
* thus 'kept alive'. This method respects the value of the
Collapse file

‎microbench/src/main/java/io/netty/handler/codec/http/HttpRequestEncoderInsertBenchmark.java‎

Copy file name to clipboardExpand all lines: microbench/src/main/java/io/netty/handler/codec/http/HttpRequestEncoderInsertBenchmark.java
+75-4Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,98 @@
2323
import io.netty.util.CharsetUtil;
2424
import org.openjdk.jmh.annotations.Benchmark;
2525
import org.openjdk.jmh.annotations.Measurement;
26+
import org.openjdk.jmh.annotations.Param;
2627
import org.openjdk.jmh.annotations.Scope;
28+
import org.openjdk.jmh.annotations.Setup;
2729
import org.openjdk.jmh.annotations.State;
2830
import org.openjdk.jmh.annotations.Warmup;
2931

30-
import static io.netty.handler.codec.http.HttpConstants.*;
32+
import java.util.ArrayList;
33+
import java.util.Arrays;
34+
import java.util.List;
35+
import java.util.Random;
36+
37+
import static io.netty.handler.codec.http.HttpConstants.CR;
38+
import static io.netty.handler.codec.http.HttpConstants.LF;
39+
import static io.netty.handler.codec.http.HttpConstants.SP;
3140

3241
@State(Scope.Benchmark)
3342
@Warmup(iterations = 10)
3443
@Measurement(iterations = 20)
3544
public class HttpRequestEncoderInsertBenchmark extends AbstractMicrobenchmark {
3645

37-
private final String uri = "http://localhost?eventType=CRITICAL&from=0&to=1497437160327&limit=10&offset=0";
46+
private static final String[] PARAMS = {
47+
"eventType=CRITICAL",
48+
"from=0",
49+
"to=1497437160327",
50+
"limit=10",
51+
"offset=0"
52+
};
53+
@Param({"1024", "128000"})
54+
private int samples;
55+
56+
private String[] uris;
57+
private int index;
3858
private final OldHttpRequestEncoder encoderOld = new OldHttpRequestEncoder();
3959
private final HttpRequestEncoder encoderNew = new HttpRequestEncoder();
4060

61+
@Setup
62+
public void setup() {
63+
List<String[]> permutations = new ArrayList<String[]>();
64+
permute(PARAMS.clone(), 0, permutations);
65+
66+
String[] allCombinations = new String[permutations.size()];
67+
String base = "http://localhost?";
68+
for (int i = 0; i < permutations.size(); i++) {
69+
StringBuilder sb = new StringBuilder(base);
70+
String[] p = permutations.get(i);
71+
for (int j = 0; j < p.length; j++) {
72+
if (j != 0) {
73+
sb.append('&');
74+
}
75+
sb.append(p[j]);
76+
}
77+
allCombinations[i] = sb.toString();
78+
}
79+
uris = new String[samples];
80+
Random rand = new Random(42);
81+
for (int i = 0; i < uris.length; i++) {
82+
uris[i] = allCombinations[rand.nextInt(allCombinations.length)];
83+
}
84+
index = 0;
85+
}
86+
87+
private static void permute(String[] arr, int start, List<String[]> out) {
88+
if (start == arr.length - 1) {
89+
out.add(Arrays.copyOf(arr, arr.length));
90+
return;
91+
}
92+
for (int i = start; i < arr.length; i++) {
93+
swap(arr, start, i);
94+
permute(arr, start + 1, out);
95+
swap(arr, start, i);
96+
}
97+
}
98+
99+
private static void swap(String[] a, int i, int j) {
100+
String t = a[i];
101+
a[i] = a[j];
102+
a[j] = t;
103+
}
104+
105+
private String nextUri() {
106+
if (index >= uris.length) {
107+
index = 0;
108+
}
109+
return uris[index++];
110+
}
111+
41112
@Benchmark
42113
public ByteBuf oldEncoder() throws Exception {
43114
ByteBuf buffer = Unpooled.buffer(100);
44115
try {
45116
encoderOld.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1,
46-
HttpMethod.GET, uri));
117+
HttpMethod.GET, nextUri()));
47118
return buffer;
48119
} finally {
49120
buffer.release();
@@ -55,7 +126,7 @@ public ByteBuf newEncoder() throws Exception {
55126
ByteBuf buffer = Unpooled.buffer(100);
56127
try {
57128
encoderNew.encodeInitialLine(buffer, new DefaultHttpRequest(HttpVersion.HTTP_1_1,
58-
HttpMethod.GET, uri));
129+
HttpMethod.GET, nextUri()));
59130
return buffer;
60131
} finally {
61132
buffer.release();

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.