mirror of
https://github.com/apple/swift-nio-http2
synced 2026-11-26 04:57:50 +00:00
1.1.0
4
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
ddc982111e |
Move to String-backed HPACK representations. (#89)
Motivation: When originally written our HPACK implementation targetted Swift 4.2, which had performance issues when storing mostly-ASCII data in Strings. For this reason we had a number of data types that used ByteBuffers as their backing storage and reified Strings when necessary. In Swift 5 this is no longer worthwhile: in fact, it imposes performance penalties above and beyond simply using the Strings. For this reason, we should move to a simpler model where we simply store Strings again. Modifications: - Rewrote all HPACK data structures to use Strings as their backing store. - Removed now-redundant genericism. - Removed now-unnecessary StringRing data structure and associated tests. Result: Faster performance in almost all cases. |
||
|
|
70ac404a42 |
Decrease allocations in encode loop. (#86)
While investigating performance of HTTP/2 I noticed a substantial number of allocations occuring in a hot loop in header compression. Specifically I set up a simple harness that encoded headers 100 times, and then ran that harness 10k times. This harness ended up performing 17 million allocations, which is frankly a bit too high! The fact that the number of allocations was in the millions suggested that we were allocating some resources per header encode. That's not good: assuming the target `ByteBuffer` is big enough we should not need to perform new allocations to encode headers. The allocations came from two places. Firstly, we were allocating in the `HeaderTableStorage.indices(matching:)` function. This was because that operation was returning a lazy collection, which required multiple heap allocations for the closure contexts. Those needed to be eliminated. Secondly, we were triggering a CoW of the `ByteBuffer` passed in to `encode()`. This is the result of a complex bit of control flow that fundamentally meant that the buffer was held in two places in that method. Removing these two sources of allocations dropped the count in my test from 17 million to 176k, a decrease of 100x. We also saw a speed boost of nearly 66% in terms of runtime of the microbenchmark. - Removed `HeaderTableStorage.indices(matching:)` - Implemented `HeaderTableStorage.closestMatch(name:value:), containing the repeated logic of the body of the two loops over the header indices. - Used swap() and an empty byte buffer to ensure that encode does not cause a CoW operation. Much faster and more memory efficient header encoding. |
||
|
|
3c90345c36 |
Pure-Swift HTTP/2 implementation (#40)
* Updated to use SwiftNIO 1.10+ to gain access to HTTPHeaders internals.
* Add pure-Swift HTTP/2 protocol implementation.
Motivation:
Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.
This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.
Modifications:
- Add pure Swift protocol implementations.
- Test them.
Results:
Another step towards protocol independence.
* Initial basic parser code.
* Adding unit tests.
* Add NIOHTTP2FlowControlHandler
Motivation:
One of the things that nghttp2 used to do for us was to handle buffering
DATA frames until flow control allowed them to go out. For users that
want that behaviour, we need to replace the nghttp2 code with a new
ChannelHandler that can do this work.
Modifications:
- Added NIOHTTP2FlowControlHandler.
Result:
Users will have the same feature set as they did with nghttp2.
* Generate Linux test information.
* Documentation comment.
* Updated new tests from master to use new payload types.
* Updated following review.
Notable changes:
- Implemented unit tests for the ALTSVC (0xa) and ORIGIN (0xc) frame types.
- Additional DATA frames are synthesized to wrap individual payload chunks as they arrive. If the payload of a DATA frame with 4MB data arrives in 4 separate 1MB chunks, the decoder will emit 4 DATA frames, with only the last containing the END_STREAM flag (if that was set on the incoming frame).
- HEADERS / PUSH_PROMISE frames and any following CONTINUATION frames are now handled properly: header fragments from all frames are gathered up into a single buffer. Once a frame with END_HEADERS is encountered, the resultant accumulated buffer is sent for decoding along with the flags and stream ID of the original HEADERS / PUSH_PROMISE frame.
- HPACK encoders/decoders now live in the frame encoder/decoder types, rather than being attached to a stream.
* Updated Linux tests.
* Headers placed in relevant files.
* Add pure-Swift HTTP/2 protocol implementation.
Motivation:
Currently we use nghttp2 for our basic protocol implementation. This is
plenty workable, but provides us a number of limitations. It adds
complexity to our build system, provides us with external dependencies we
have little control over, limits our users' ability to express low-level
protocol semantics if they so wish, causes us to heap-allocate like crazy over
the lifetime of a connection, and brings a world of pain around reentrancy.
This set of limitations has motivated a desire to move away from using
nghttp2. This is a part of that dream.
Modifications:
- Add pure Swift protocol implementations.
- Test them.
Results:
Another step towards protocol independence.
* Revamped frame decoder state machine and API. Now uses separate input and output methods to manage multiple frames in a single buffer.
* Updated Linux tests.
* Bumped swift-nio version to 1.12.0 and updated `HTTPHeaders` -> `HPACKHeaders` conversion initializer to directly copy the buffer and map the header name/value indices.
* Set up HTTP2Parser in handlerAdded if the channel had been previously activated. (#36)
By setting up the handler in the handlerAdded hook, HTTP2Parser supports
being added dynamically in a channelRead event, after the channel has sent
the channelActive event.
(cherry picked from commit
|
||
|
|
6f8f523684 |
Implementation of HPACK encoding and header tables (#10)
Motivation: This is the beginning of removing the dependency on `nghttp2`. Modifications: I've added a new library target, `NIOHPACK`, which contains all the necessary pieces to implement HPACK encoding as specified in RFC 7541. There are unit tests exercising all the code and some additional edge cases around integer encoding, along with some performance tests for the Huffman encode/decode process. The latter currently takes longer to decode than to encode, which is worrying, but this is only really noticeable when encoding > 16KB strings as a rule; therefore I've decided not to pull out the optimization hammer just yet. Included in the unit tests are the inputs and outputs from all the examples in RFC 7541 and RFC 7540. I've tried to match the coding style from the repo, but some of my own idioms may have crept in here & there. Please let me know if you see anything wrong in that regard. Currently the static header table and Huffman encoding tables are implemented as Swift `let`s. For the static header table that's ok (only 62 entries), but the Huffman table is huge. I'm debating whether it's worth having that in a plain C file somewhere, but I'm not sure what the tradeoffs are vs. performance. Right now it seems to only affect compilation time, but I may be wrong. Result: In general, nothing will change: nothing in NIOHTTP2 is using this library yet. That part is what I'm working on now. |