Skip to content

Commit 664f203

Browse files
authored
Optimise HttpMethod#valueOf fast path lookup. (#14982)
Motivation: `HttpMethod#valueOf` has a fast path lookup for _GET_ and _POST_, those call the `HttpMethod#name()` which calls `AsciiString#toString()` and that is not necessary since we can cache the interned corresponding strings locally like `HttpVersion#valueOf` does. Changes: Declare _GET_/_POST_ interned string constants and use them for fast path lookup in `valueOf` method. Result: Avoid two un-necessaries method call on the fast path.
1 parent a7e206d commit 664f203

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

codec-http/src/main/java/io/netty/handler/codec/http/HttpMethod.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
* <a href="https://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
2727
*/
2828
public class HttpMethod implements Comparable<HttpMethod> {
29+
30+
private static final String GET_STRING = "GET";
31+
private static final String POST_STRING = "POST";
32+
2933
/**
3034
* The OPTIONS method represents a request for information about the communication options
3135
* available on the request/response chain identified by the Request-URI. This method allows
@@ -41,7 +45,7 @@ public class HttpMethod implements Comparable<HttpMethod> {
4145
* produced data which shall be returned as the entity in the response and not the source text
4246
* of the process, unless that text happens to be the output of the process.
4347
*/
44-
public static final HttpMethod GET = new HttpMethod("GET");
48+
public static final HttpMethod GET = new HttpMethod(GET_STRING);
4549

4650
/**
4751
* The HEAD method is identical to GET except that the server MUST NOT return a message-body
@@ -54,7 +58,7 @@ public class HttpMethod implements Comparable<HttpMethod> {
5458
* request as a new subordinate of the resource identified by the Request-URI in the
5559
* Request-Line.
5660
*/
57-
public static final HttpMethod POST = new HttpMethod("POST");
61+
public static final HttpMethod POST = new HttpMethod(POST_STRING);
5862

5963
/**
6064
* The PUT method requests that the enclosed entity be stored under the supplied Request-URI.
@@ -107,11 +111,11 @@ public class HttpMethod implements Comparable<HttpMethod> {
107111
*/
108112
public static HttpMethod valueOf(String name) {
109113
// fast-path
110-
if (name == HttpMethod.GET.name()) {
111-
return HttpMethod.GET;
114+
if (name == GET_STRING) {
115+
return GET;
112116
}
113-
if (name == HttpMethod.POST.name()) {
114-
return HttpMethod.POST;
117+
if (name == POST_STRING) {
118+
return POST;
115119
}
116120
// "slow"-path
117121
HttpMethod result = methodMap.get(name);

0 commit comments

Comments
 (0)