Skip to content

Commit

Permalink
Use sync options when creating a stream channel (#283)
Browse files Browse the repository at this point in the history
Motivation:

SwiftNIO 2.27.0 added synchronous channel options. We can use these if
available when creating a stream channel to avoid a future allocation.

Modifications:

- 'getAutoReadFromParent' is synchronous when possible and now takes a
  closure to handle the result.
- Update allocation counts (and sort them to make updating them easier,
  this also includes switching the order we run test_1k_requests
  interleaved and noninterleaved)

Result:

Fewer allocations.
  • Loading branch information
glbrntt authored Mar 16, 2021
1 parent 4e96d02 commit e7af013
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,19 @@ fileprivate class TestServer: ChannelInboundHandler {
}

func run(identifier: String) {
var noninterleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 1)

measure(identifier: identifier + "_noninterleaved") {
return try! noninterleaved.run()
}

noninterleaved.tearDown()

var interleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 100)

measure(identifier: identifier + "_interleaved") {
return try! interleaved.run()
}

interleaved.tearDown()

var noninterleaved = try! ServerOnly1KRequestsBenchmark(concurrentStreams: 1)

measure(identifier: identifier + "_noninterleaved") {
return try! noninterleaved.run()
}

noninterleaved.tearDown()
}
18 changes: 13 additions & 5 deletions Sources/NIOHTTP2/HTTP2StreamChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
// 2. Calling the initializer, if provided.
// 3. Activating when complete.
// 4. Catching errors if they occur.
self.getAutoReadFromParent().whenComplete { autoReadResult in
self.getAutoReadFromParent { autoReadResult in
switch autoReadResult {
case .success(let autoRead):
self.autoRead = autoRead
Expand Down Expand Up @@ -216,7 +216,7 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
// 2. Calling the initializer, if provided.
// 3. Activating when complete.
// 4. Catching errors if they occur.
self.getAutoReadFromParent().whenComplete { autoReadResult in
self.getAutoReadFromParent { autoReadResult in
switch autoReadResult {
case .success(let autoRead):
self.autoRead = autoRead
Expand All @@ -238,12 +238,20 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
}
}

/// Gets the 'autoRead' option from the parent channel.
private func getAutoReadFromParent() -> EventLoopFuture<Bool> {
/// Gets the 'autoRead' option from the parent channel and invokes the `body` closure with the
/// result. This may be done synchronously if the parent `Channel` supports synchronous options.
private func getAutoReadFromParent(_ body: @escaping (Result<Bool, Error>) -> Void) {
// This force unwrap is safe as parent is assigned in the initializer, and never unassigned.
// Note we also don't set the value here: the additional `map` causes an extra allocation
// when using a Swift 5.0 compiler.
return self.parent!.getOption(ChannelOptions.autoRead)
if let syncOptions = self.parent!.syncOptions {
let autoRead = Result(catching: { try syncOptions.getOption(ChannelOptions.autoRead) })
body(autoRead)
} else {
self.parent!.getOption(ChannelOptions.autoRead).whenComplete { autoRead in
body(autoRead)
}
}
}

/// Activates the channel if the parent channel is active and succeeds the given `promise`.
Expand Down
24 changes: 12 additions & 12 deletions docker/docker-compose.1604.51.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ services:
integration-tests:
image: swift-nio-http2:16.04-5.1
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=58000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=53000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=52000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=336000
- MAX_ALLOCS_ALLOWED_client_server_request_response=300000
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=55000
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=306000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=342000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=56000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=55000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=384000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=354000

performance-test:
image: swift-nio-http2:16.04-5.1
Expand All @@ -33,13 +33,13 @@ services:
test:
image: swift-nio-http2:16.04-5.1
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=58000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=53000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=52000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=336000
- MAX_ALLOCS_ALLOWED_client_server_request_response=300000
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=55000
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=306000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=342000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=56000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=55000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=384000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=354000
- SANITIZER_ARG=--sanitize=thread

shell:
Expand Down
24 changes: 12 additions & 12 deletions docker/docker-compose.1804.50.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ services:
integration-tests:
image: swift-nio-http2:18.04-5.0
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=62000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=60000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=59000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=359000
- MAX_ALLOCS_ALLOWED_client_server_request_response=324000
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=59000
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=330000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=365000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=63000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=62000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=455000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=425000

performance-test:
image: swift-nio-http2:18.04-5.0
Expand All @@ -33,13 +33,13 @@ services:
test:
image: swift-nio-http2:18.04-5.0
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=62000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=60000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=59000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=359000
- MAX_ALLOCS_ALLOWED_client_server_request_response=324000
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=59000
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=330000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=365000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=63000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=62000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=455000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=425000

shell:
image: swift-nio-http2:18.04-5.0
25 changes: 12 additions & 13 deletions docker/docker-compose.1804.52.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ services:
integration-tests:
image: swift-nio-http2:18.04-5.2
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=58000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=52000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=51000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=333000
- MAX_ALLOCS_ALLOWED_client_server_request_response=296000
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=55000
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=302000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=339000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=55000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=54000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=384000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=354000

performance-test:
image: swift-nio-http2:18.04-5.2
Expand All @@ -33,14 +33,13 @@ services:
test:
image: swift-nio-http2:18.04-5.2
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=58000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=52000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=51000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=333000
- MAX_ALLOCS_ALLOWED_client_server_request_response=296000
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=55000
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=302000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=339000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=55000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=54000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=384000

- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=354000

shell:
image: swift-nio-http2:18.04-5.2
24 changes: 12 additions & 12 deletions docker/docker-compose.1804.53.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ services:
integration-tests:
image: swift-nio-http2:18.04-5.3
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=58000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=50000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=49000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=332000
- MAX_ALLOCS_ALLOWED_client_server_request_response=295000
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=55000
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=301000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=338000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=53000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=52000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=364000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=334000

performance-test:
image: swift-nio-http2:18.04-5.3
Expand All @@ -33,13 +33,13 @@ services:
test:
image: swift-nio-http2:18.04-5.3
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=58000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=50000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=49000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=332000
- MAX_ALLOCS_ALLOWED_client_server_request_response=295000
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=55000
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=301000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=338000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=53000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=52000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=364000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=334000

shell:
image: swift-nio-http2:18.04-5.3
24 changes: 12 additions & 12 deletions docker/docker-compose.2004.54.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ services:
integration-tests:
image: swift-nio-http2:20.04-5.4
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=58000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=50000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=49000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=332000
- MAX_ALLOCS_ALLOWED_client_server_request_response=295000
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=55000
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=301000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=338000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=53000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=52000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=364000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=334000

performance-test:
image: swift-nio-http2:20.04-5.4
Expand All @@ -34,13 +34,13 @@ services:
test:
image: swift-nio-http2:20.04-5.4
environment:
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=58000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=50000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=49000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=332000
- MAX_ALLOCS_ALLOWED_client_server_request_response=295000
- MAX_ALLOCS_ALLOWED_create_client_stream_channel=55000
- MAX_ALLOCS_ALLOWED_hpack_decoding=5050
- MAX_ALLOCS_ALLOWED_client_server_request_response=301000
- MAX_ALLOCS_ALLOWED_client_server_h1_request_response=338000
- MAX_ALLOCS_ALLOWED_1k_requests_interleaved=53000
- MAX_ALLOCS_ALLOWED_1k_requests_noninterleaved=52000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=364000
- MAX_ALLOCS_ALLOWED_stream_teardown_100_concurrent=334000

shell:
image: swift-nio-http2:20.04-5.4

0 comments on commit e7af013

Please sign in to comment.