Change the type of HttpMethod.name() to AsciiString

Related: #3132

Motivation:

Changing the type of HttpMethod.name() gives us the performance
advantage when encoding it into the wire.

Modifications:

- Change the type of HttpMethod.name()
- Inline HttpMethod.encode() because it's used only in a single place
  and it's trivial.

Result:

Getting close to the machine
This commit is contained in:
Trustin Lee 2014-12-06 18:09:27 +09:00
parent 13ad58aed7
commit 377ef31bb1
2 changed files with 35 additions and 49 deletions

View File

@ -15,76 +15,75 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import io.netty.buffer.ByteBuf; import io.netty.handler.codec.AsciiString;
import io.netty.util.CharsetUtil;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
* The request getMethod of HTTP or its derived protocols, such as * The request method of HTTP or its derived protocols, such as
* <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and * <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
* <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>. * <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
*/ */
public class HttpMethod implements Comparable<HttpMethod> { public class HttpMethod implements Comparable<HttpMethod> {
/** /**
* The OPTIONS getMethod represents a request for information about the communication options * The OPTIONS method represents a request for information about the communication options
* available on the request/response chain identified by the Request-URI. This getMethod allows * available on the request/response chain identified by the Request-URI. This method allows
* the client to determine the options and/or requirements associated with a resource, or the * the client to determine the options and/or requirements associated with a resource, or the
* capabilities of a server, without implying a resource action or initiating a resource * capabilities of a server, without implying a resource action or initiating a resource
* retrieval. * retrieval.
*/ */
public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS", true); public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS");
/** /**
* The GET getMethod means retrieve whatever information (in the form of an entity) is identified * The GET method means retrieve whatever information (in the form of an entity) is identified
* by the Request-URI. If the Request-URI refers to a data-producing process, it is the * by the Request-URI. If the Request-URI refers to a data-producing process, it is the
* produced data which shall be returned as the entity in the response and not the source text * produced data which shall be returned as the entity in the response and not the source text
* of the process, unless that text happens to be the output of the process. * of the process, unless that text happens to be the output of the process.
*/ */
public static final HttpMethod GET = new HttpMethod("GET", true); public static final HttpMethod GET = new HttpMethod("GET");
/** /**
* The HEAD getMethod is identical to GET except that the server MUST NOT return a message-body * The HEAD method is identical to GET except that the server MUST NOT return a message-body
* in the response. * in the response.
*/ */
public static final HttpMethod HEAD = new HttpMethod("HEAD", true); public static final HttpMethod HEAD = new HttpMethod("HEAD");
/** /**
* The POST getMethod is used to request that the origin server accept the entity enclosed in the * The POST method is used to request that the origin server accept the entity enclosed in the
* request as a new subordinate of the resource identified by the Request-URI in the * request as a new subordinate of the resource identified by the Request-URI in the
* Request-Line. * Request-Line.
*/ */
public static final HttpMethod POST = new HttpMethod("POST", true); public static final HttpMethod POST = new HttpMethod("POST");
/** /**
* The PUT getMethod requests that the enclosed entity be stored under the supplied Request-URI. * The PUT method requests that the enclosed entity be stored under the supplied Request-URI.
*/ */
public static final HttpMethod PUT = new HttpMethod("PUT", true); public static final HttpMethod PUT = new HttpMethod("PUT");
/** /**
* The PATCH getMethod requests that a set of changes described in the * The PATCH method requests that a set of changes described in the
* request entity be applied to the resource identified by the Request-URI. * request entity be applied to the resource identified by the Request-URI.
*/ */
public static final HttpMethod PATCH = new HttpMethod("PATCH", true); public static final HttpMethod PATCH = new HttpMethod("PATCH");
/** /**
* The DELETE getMethod requests that the origin server delete the resource identified by the * The DELETE method requests that the origin server delete the resource identified by the
* Request-URI. * Request-URI.
*/ */
public static final HttpMethod DELETE = new HttpMethod("DELETE", true); public static final HttpMethod DELETE = new HttpMethod("DELETE");
/** /**
* The TRACE getMethod is used to invoke a remote, application-layer loop- back of the request * The TRACE method is used to invoke a remote, application-layer loop- back of the request
* message. * message.
*/ */
public static final HttpMethod TRACE = new HttpMethod("TRACE", true); public static final HttpMethod TRACE = new HttpMethod("TRACE");
/** /**
* This specification reserves the getMethod name CONNECT for use with a proxy that can dynamically * This specification reserves the method name CONNECT for use with a proxy that can dynamically
* switch to being a tunnel * switch to being a tunnel
*/ */
public static final HttpMethod CONNECT = new HttpMethod("CONNECT", true); public static final HttpMethod CONNECT = new HttpMethod("CONNECT");
private static final Map<String, HttpMethod> methodMap = private static final Map<String, HttpMethod> methodMap =
new HashMap<String, HttpMethod>(); new HashMap<String, HttpMethod>();
@ -103,7 +102,7 @@ public class HttpMethod implements Comparable<HttpMethod> {
/** /**
* Returns the {@link HttpMethod} represented by the specified name. * Returns the {@link HttpMethod} represented by the specified name.
* If the specified name is a standard HTTP getMethod name, a cached instance * If the specified name is a standard HTTP method name, a cached instance
* will be returned. Otherwise, a new instance will be returned. * will be returned. Otherwise, a new instance will be returned.
*/ */
public static HttpMethod valueOf(String name) { public static HttpMethod valueOf(String name) {
@ -124,21 +123,17 @@ public class HttpMethod implements Comparable<HttpMethod> {
} }
} }
private final String name; private final AsciiString name;
private final byte[] bytes; private final String nameAsString;
/** /**
* Creates a new HTTP getMethod with the specified name. You will not need to * Creates a new HTTP method with the specified name. You will not need to
* create a new getMethod unless you are implementing a protocol derived from * create a new method unless you are implementing a protocol derived from
* HTTP, such as * HTTP, such as
* <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and * <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
* <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a> * <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>
*/ */
public HttpMethod(String name) { public HttpMethod(String name) {
this(name, false);
}
private HttpMethod(String name, boolean bytes) {
if (name == null) { if (name == null) {
throw new NullPointerException("name"); throw new NullPointerException("name");
} }
@ -155,18 +150,14 @@ public class HttpMethod implements Comparable<HttpMethod> {
} }
} }
this.name = name; this.name = new AsciiString(name);
if (bytes) { nameAsString = name;
this.bytes = name.getBytes(CharsetUtil.US_ASCII);
} else {
this.bytes = null;
}
} }
/** /**
* Returns the name of this getMethod. * Returns the name of this method.
*/ */
public String name() { public AsciiString name() {
return name; return name;
} }
@ -187,19 +178,11 @@ public class HttpMethod implements Comparable<HttpMethod> {
@Override @Override
public String toString() { public String toString() {
return name(); return nameAsString;
} }
@Override @Override
public int compareTo(HttpMethod o) { public int compareTo(HttpMethod o) {
return name().compareTo(o.name()); return name().compareTo(o.name());
} }
void encode(ByteBuf buf) {
if (bytes == null) {
HttpHeaderUtil.encodeAscii0(name, buf);
} else {
buf.writeBytes(bytes);
}
}
} }

View File

@ -16,6 +16,7 @@
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.AsciiString;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import static io.netty.handler.codec.http.HttpConstants.*; import static io.netty.handler.codec.http.HttpConstants.*;
@ -36,7 +37,9 @@ public class HttpRequestEncoder extends HttpObjectEncoder<HttpRequest> {
@Override @Override
protected void encodeInitialLine(ByteBuf buf, HttpRequest request) throws Exception { protected void encodeInitialLine(ByteBuf buf, HttpRequest request) throws Exception {
request.method().encode(buf); AsciiString method = request.method().name();
buf.writeBytes(method.array(), method.arrayOffset(), method.length());
buf.writeByte(SP); buf.writeByte(SP);
// Add / as absolute path if no is present. // Add / as absolute path if no is present.