mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Motivation:
NIO's dynamic pipeline comes with one performance caveat: If your type
isn't specialised in NIOAny (only types in the NIO module can be) and
it's over 3 words (24 bytes on 64-bit platforms), you'll suffer an
allocation for every time you box it in NIOAny. Very unfortunately, both
ByteBuffer and FileRegion were exactly 3 words wide which means that any
enum containing those would be wider than 3 words. Worse, both
HTTP{Request,Response}Head contained types that were wider than 3 words.
The best solution to this problem is to shrink ByteBuffer and FileRegion
to just under 3 words and that's exactly what this PR is doing. That is
slightly tricky as ByteBuffer was already bit packed fairly well
(reader/writer indices & slice begin/end were stored as a UInt32). The
trick we employ now is to store the slice beginning in a UInt24 and the
file region reader index in a UInt56. That saves one byte for both
ByteBuffer and FileRegion with very moderate tradeoffs: The reader index
in a file now needs to be within 64 PiB (peta bytes) and a byte buffer
slice beginning must start within 16 MiB (mega bytes). Note: The
reader/writer indices as well as slice ends are _not_ affected and can
still be within 4 GiB. Clearly no one would care about the restrictions
for FileRegions in the real world but we might hit the ByteBuffer slice
beginning limit in which case the slice would be copied out. But
given that slices are mostly used to slice off headers in network
protocols, 16 MiB should be _plenty_.
Norman was kind enough to measure the perf differences:
master (before this PR):
```
$ wrk -c 256 -d 10s -t 4 -s ~/Downloads/pipeline-many.lua -H "X-Host: SomeValue" -H "Host: swiftnio.io" -H "ThereAreEvenMoreHeaders: AndMoreValues" http://localhost:8888
Running 10s test @ http://localhost:8888
4 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 93.02ms 158.13ms 1.97s 93.07%
Req/Sec 112.79k 26.05k 258.75k 84.50%
4501098 requests in 10.04s, 214.63MB read
Socket errors: connect 0, read 111, write 0, timeout 89
Requests/sec: 448485.61
Transfer/sec: 21.39MB
```
after this PR:
```
$ wrk -c 256 -d 10s -t 4 -s ~/Downloads/pipeline-many.lua -H "X-Host: SomeValue" -H "Host: swiftnio.io" -H "ThereAreEvenMoreHeaders: AndMoreValues" http://localhost:8888
Running 10s test @ http://localhost:8888
4 threads and 256 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 107.53ms 206.56ms 1.99s 90.55%
Req/Sec 124.15k 26.56k 290.41k 89.25%
4952904 requests in 10.03s, 236.17MB read
Socket errors: connect 0, read 161, write 0, timeout 22
Requests/sec: 493852.65
Transfer/sec: 23.55MB
```
so we see a nice 10% improvement
Modifications:
- shrank ByteBuffer by 1 byte, making it 23 bytes in total
- shrank FileRegion by 1 byte, making it 23 bytes in total
- added @_inlineable to NIOAny where appropriate to not suffer boxed existentials in different places
- added tests for the new edge cases
Result:
- more speed
- fewer allocations
89 lines
3.5 KiB
Swift
89 lines
3.5 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// ChannelHandler which will emit data by calling `ChannelHandlerContext.write`.
|
|
///
|
|
/// We _strongly_ advice against implementing this protocol directly. Please implement `ChannelInboundHandler` or / and `ChannelOutboundHandler`.
|
|
public protocol _EmittingChannelHandler {
|
|
/// The type of the outbound data which will be forwarded to the next `ChannelOutboundHandler` in the `ChannelPipeline`.
|
|
associatedtype OutboundOut = Never
|
|
|
|
/// Wrap the provided `OutboundOut` that will be passed to the next `ChannelOutboundHandler` by calling `ChannelHandlerContext.write`.
|
|
@_inlineable
|
|
func wrapOutboundOut(_ value: OutboundOut) -> NIOAny
|
|
}
|
|
|
|
/// Default implementations for `_EmittingChannelHandler`.
|
|
extension _EmittingChannelHandler {
|
|
@_inlineable
|
|
public func wrapOutboundOut(_ value: OutboundOut) -> NIOAny {
|
|
return NIOAny(value)
|
|
}
|
|
}
|
|
|
|
/// `ChannelHandler` which handles inbound I/O events for a `Channel`.
|
|
///
|
|
/// Please refer to `_ChannelInboundHandler` and `_EmittingChannelHandler` for more details on the provided methods.
|
|
public protocol ChannelInboundHandler: _ChannelInboundHandler, _EmittingChannelHandler {
|
|
/// The type of the inbound data which is wrapped in `NIOAny`.
|
|
associatedtype InboundIn
|
|
|
|
/// The type of the inbound data which will be forwarded to the next `ChannelInboundHandler` in the `ChannelPipeline`.
|
|
associatedtype InboundOut = Never
|
|
|
|
/// Unwrap the provided `NIOAny` that was passed to `channelRead`.
|
|
@_inlineable
|
|
func unwrapInboundIn(_ value: NIOAny) -> InboundIn
|
|
|
|
/// Wrap the provided `InboundOut` that will be passed to the next `ChannelInboundHandler` by calling `ChannelHandlerContext.fireChannelRead`.
|
|
@_inlineable
|
|
func wrapInboundOut(_ value: InboundOut) -> NIOAny
|
|
}
|
|
|
|
/// Default implementations for `ChannelInboundHandler`.
|
|
extension ChannelInboundHandler {
|
|
@_inlineable
|
|
public func unwrapInboundIn(_ value: NIOAny) -> InboundIn {
|
|
return value.forceAs()
|
|
}
|
|
|
|
@_inlineable
|
|
public func wrapInboundOut(_ value: InboundOut) -> NIOAny {
|
|
return NIOAny(value)
|
|
}
|
|
}
|
|
|
|
/// `ChannelHandler` which handles outbound I/O events or intercept an outbound I/O operation for a `Channel`.
|
|
///
|
|
/// Please refer to `_ChannelOutboundHandler` and `_EmittingChannelHandler` for more details on the provided methods.
|
|
public protocol ChannelOutboundHandler: _ChannelOutboundHandler, _EmittingChannelHandler {
|
|
/// The type of the outbound data which is wrapped in `NIOAny`.
|
|
associatedtype OutboundIn
|
|
|
|
/// Unwrap the provided `NIOAny` that was passed to `write`.
|
|
@_inlineable
|
|
func unwrapOutboundIn(_ value: NIOAny) -> OutboundIn
|
|
}
|
|
|
|
/// Default implementations for `ChannelOutboundHandler`.
|
|
extension ChannelOutboundHandler {
|
|
@_inlineable
|
|
public func unwrapOutboundIn(_ value: NIOAny) -> OutboundIn {
|
|
return value.forceAs()
|
|
}
|
|
}
|
|
|
|
/// A combination of `ChannelInboundHandler` and `ChannelOutboundHandler`.
|
|
public typealias ChannelDuplexHandler = ChannelInboundHandler & ChannelOutboundHandler
|