2014-06-05 11:31:04 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2014 The Netty Project
|
|
|
|
*
|
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package io.netty.handler.codec.http;
|
|
|
|
|
|
|
|
import io.netty.buffer.ByteBuf;
|
2015-04-01 01:23:52 +02:00
|
|
|
import io.netty.buffer.ByteBufUtil;
|
|
|
|
import io.netty.util.AsciiString;
|
2017-06-23 12:00:01 +02:00
|
|
|
import io.netty.util.CharsetUtil;
|
2014-06-05 11:31:04 +02:00
|
|
|
|
2017-07-03 22:57:07 +02:00
|
|
|
import static io.netty.handler.codec.http.HttpConstants.*;
|
|
|
|
import static io.netty.handler.codec.http.HttpObjectEncoder.CRLF_SHORT;
|
|
|
|
|
Faster and more memory efficient headers for HTTP, HTTP/2, STOMP and SPYD. Fixes #3600
Motivation:
We noticed that the headers implementation in Netty for HTTP/2 uses quite a lot of memory
and that also at least the performance of randomly accessing a header is quite poor. The main
concern however was memory usage, as profiling has shown that a DefaultHttp2Headers
not only use a lot of memory it also wastes a lot due to the underlying hashmaps having
to be resized potentially several times as new headers are being inserted.
This is tracked as issue #3600.
Modifications:
We redesigned the DefaultHeaders to simply take a Map object in its constructor and
reimplemented the class using only the Map primitives. That way the implementation
is very concise and hopefully easy to understand and it allows each concrete headers
implementation to provide its own map or to even use a different headers implementation
for processing requests and writing responses i.e. incoming headers need to provide
fast random access while outgoing headers need fast insertion and fast iteration. The
new implementation can support this with hardly any code changes. It also comes
with the advantage that if the Netty project decides to add a third party collections library
as a dependency, one can simply plug in one of those very fast and memory efficient map
implementations and get faster and smaller headers for free.
For now, we are using the JDK's TreeMap for HTTP and HTTP/2 default headers.
Result:
- Significantly fewer lines of code in the implementation. While the total commit is still
roughly 400 lines less, the actual implementation is a lot less. I just added some more
tests and microbenchmarks.
- Overall performance is up. The current implementation should be significantly faster
for insertion and retrieval. However, it is slower when it comes to iteration. There is simply
no way a TreeMap can have the same iteration performance as a linked list (as used in the
current headers implementation). That's totally fine though, because when looking at the
benchmark results @ejona86 pointed out that the performance of the headers is completely
dominated by insertion, that is insertion is so significantly faster in the new implementation
that it does make up for several times the iteration speed. You can't iterate what you haven't
inserted. I am demonstrating that in this spreadsheet [1]. (Actually, iteration performance is
only down for HTTP, it's significantly improved for HTTP/2).
- Memory is down. The implementation with TreeMap uses on avg ~30% less memory. It also does not
produce any garbage while being resized. In load tests for GRPC we have seen a memory reduction
of up to 1.2KB per RPC. I summarized the memory improvements in this spreadsheet [1]. The data
was generated by [2] using JOL.
- While it was my original intend to only improve the memory usage for HTTP/2, it should be similarly
improved for HTTP, SPDY and STOMP as they all share a common implementation.
[1] https://docs.google.com/spreadsheets/d/1ck3RQklyzEcCLlyJoqDXPCWRGVUuS-ArZf0etSXLVDQ/edit#gid=0
[2] https://gist.github.com/buchgr/4458a8bdb51dd58c82b4
2015-04-15 06:14:00 +02:00
|
|
|
final class HttpHeadersEncoder {
|
2017-07-03 22:57:07 +02:00
|
|
|
private static final int COLON_AND_SPACE_SHORT = (COLON << 8) | SP;
|
2014-09-19 01:04:35 +02:00
|
|
|
|
Faster and more memory efficient headers for HTTP, HTTP/2, STOMP and SPYD. Fixes #3600
Motivation:
We noticed that the headers implementation in Netty for HTTP/2 uses quite a lot of memory
and that also at least the performance of randomly accessing a header is quite poor. The main
concern however was memory usage, as profiling has shown that a DefaultHttp2Headers
not only use a lot of memory it also wastes a lot due to the underlying hashmaps having
to be resized potentially several times as new headers are being inserted.
This is tracked as issue #3600.
Modifications:
We redesigned the DefaultHeaders to simply take a Map object in its constructor and
reimplemented the class using only the Map primitives. That way the implementation
is very concise and hopefully easy to understand and it allows each concrete headers
implementation to provide its own map or to even use a different headers implementation
for processing requests and writing responses i.e. incoming headers need to provide
fast random access while outgoing headers need fast insertion and fast iteration. The
new implementation can support this with hardly any code changes. It also comes
with the advantage that if the Netty project decides to add a third party collections library
as a dependency, one can simply plug in one of those very fast and memory efficient map
implementations and get faster and smaller headers for free.
For now, we are using the JDK's TreeMap for HTTP and HTTP/2 default headers.
Result:
- Significantly fewer lines of code in the implementation. While the total commit is still
roughly 400 lines less, the actual implementation is a lot less. I just added some more
tests and microbenchmarks.
- Overall performance is up. The current implementation should be significantly faster
for insertion and retrieval. However, it is slower when it comes to iteration. There is simply
no way a TreeMap can have the same iteration performance as a linked list (as used in the
current headers implementation). That's totally fine though, because when looking at the
benchmark results @ejona86 pointed out that the performance of the headers is completely
dominated by insertion, that is insertion is so significantly faster in the new implementation
that it does make up for several times the iteration speed. You can't iterate what you haven't
inserted. I am demonstrating that in this spreadsheet [1]. (Actually, iteration performance is
only down for HTTP, it's significantly improved for HTTP/2).
- Memory is down. The implementation with TreeMap uses on avg ~30% less memory. It also does not
produce any garbage while being resized. In load tests for GRPC we have seen a memory reduction
of up to 1.2KB per RPC. I summarized the memory improvements in this spreadsheet [1]. The data
was generated by [2] using JOL.
- While it was my original intend to only improve the memory usage for HTTP/2, it should be similarly
improved for HTTP, SPDY and STOMP as they all share a common implementation.
[1] https://docs.google.com/spreadsheets/d/1ck3RQklyzEcCLlyJoqDXPCWRGVUuS-ArZf0etSXLVDQ/edit#gid=0
[2] https://gist.github.com/buchgr/4458a8bdb51dd58c82b4
2015-04-15 06:14:00 +02:00
|
|
|
private HttpHeadersEncoder() {
|
2014-06-05 11:31:04 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 22:57:07 +02:00
|
|
|
static void encoderHeader(CharSequence name, CharSequence value, ByteBuf buf) {
|
2014-06-05 11:31:04 +02:00
|
|
|
final int nameLen = name.length();
|
|
|
|
final int valueLen = value.length();
|
|
|
|
final int entryLen = nameLen + valueLen + 4;
|
|
|
|
buf.ensureWritable(entryLen);
|
2015-10-27 21:47:47 +01:00
|
|
|
int offset = buf.writerIndex();
|
2017-06-23 12:00:01 +02:00
|
|
|
writeAscii(buf, offset, name);
|
2014-06-05 11:31:04 +02:00
|
|
|
offset += nameLen;
|
2017-07-03 22:57:07 +02:00
|
|
|
ByteBufUtil.setShortBE(buf, offset, COLON_AND_SPACE_SHORT);
|
|
|
|
offset += 2;
|
2017-06-23 12:00:01 +02:00
|
|
|
writeAscii(buf, offset, value);
|
2014-06-05 11:31:04 +02:00
|
|
|
offset += valueLen;
|
2017-07-03 22:57:07 +02:00
|
|
|
ByteBufUtil.setShortBE(buf, offset, CRLF_SHORT);
|
|
|
|
offset += 2;
|
2014-06-05 11:31:04 +02:00
|
|
|
buf.writerIndex(offset);
|
|
|
|
}
|
|
|
|
|
2017-06-23 12:00:01 +02:00
|
|
|
private static void writeAscii(ByteBuf buf, int offset, CharSequence value) {
|
2014-06-05 11:31:04 +02:00
|
|
|
if (value instanceof AsciiString) {
|
2017-06-23 12:00:01 +02:00
|
|
|
ByteBufUtil.copy((AsciiString) value, 0, buf, offset, value.length());
|
2014-06-05 11:31:04 +02:00
|
|
|
} else {
|
2017-06-23 12:00:01 +02:00
|
|
|
buf.setCharSequence(offset, value, CharsetUtil.US_ASCII);
|
2014-06-05 11:31:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|