Skip to content

Commit

Permalink
Add alloc counter benchmark for result erasing maps. (#1858)
Browse files Browse the repository at this point in the history
Motivation:

Peter thinks that result-erasing maps should not allocate, and we have
special code paths in the code to try to make Void -> Void maps not
allocate. Sadly, both code paths currently do allocate.

Per our rules for not trying to make optimizations without data, we
should start measuing these closures so we can make optimizations.

Modifications:

- Added an alloc couter test for result-erasing maps.

Result:

Alloc counter test suitable for any fix of #1697.
  • Loading branch information
Lukasa committed May 25, 2021
1 parent 465f87d commit 43fa768
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021 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
//
//===----------------------------------------------------------------------===//

import NIO

func run(identifier: String) {
measure(identifier: identifier) {
@inline(never)
func doEraseResult(loop: EventLoop) {
// In an ideal implementation the only allocation is this promise.
let p = loop.makePromise(of: Int.self)
let f = p.futureResult.map { (r: Int) -> Void in
// This closure is a value-to-no-value erase that closes over nothing.
// Ideally this would not allocate.
return
}.map { (_: Void) -> Void in
// This closure is a nothing-to-nothing map, basically a "completed" observer. This should
// also not allocate, but it has a separate code path to the above.
}
p.succeed(0)
}

let el = EmbeddedEventLoop()
for _ in 0..<1000 {
doEraseResult(loop: el)
}
return 1000
}
}
1 change: 1 addition & 0 deletions docker/docker-compose.1604.52.yaml
Expand Up @@ -41,6 +41,7 @@ services:
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_holding_buffer_with_space=3
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_new_buffer=3050
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_new_buffer_with_space=3050
- MAX_ALLOCS_ALLOWED_future_erase_result=4050
- MAX_ALLOCS_ALLOWED_future_lots_of_callbacks=60050
- MAX_ALLOCS_ALLOWED_modifying_1000_circular_buffer_elements=0
- MAX_ALLOCS_ALLOWED_modifying_byte_buffer_view=2050
Expand Down
1 change: 1 addition & 0 deletions docker/docker-compose.1604.53.yaml
Expand Up @@ -41,6 +41,7 @@ services:
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_holding_buffer_with_space=3
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_new_buffer=3050
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_new_buffer_with_space=3050
- MAX_ALLOCS_ALLOWED_future_erase_result=4050
- MAX_ALLOCS_ALLOWED_future_lots_of_callbacks=60050
- MAX_ALLOCS_ALLOWED_modifying_1000_circular_buffer_elements=0
- MAX_ALLOCS_ALLOWED_modifying_byte_buffer_view=2050
Expand Down
1 change: 1 addition & 0 deletions docker/docker-compose.1804.51.yaml
Expand Up @@ -41,6 +41,7 @@ services:
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_holding_buffer_with_space=3
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_new_buffer=3050
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_new_buffer_with_space=3050
- MAX_ALLOCS_ALLOWED_future_erase_result=4050
- MAX_ALLOCS_ALLOWED_future_lots_of_callbacks=60050
- MAX_ALLOCS_ALLOWED_modifying_1000_circular_buffer_elements=0
- MAX_ALLOCS_ALLOWED_modifying_byte_buffer_view=6050
Expand Down
1 change: 1 addition & 0 deletions docker/docker-compose.2004.54.yaml
Expand Up @@ -41,6 +41,7 @@ services:
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_holding_buffer_with_space=3
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_new_buffer=3050
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_new_buffer_with_space=3050
- MAX_ALLOCS_ALLOWED_future_erase_result=4050
- MAX_ALLOCS_ALLOWED_future_lots_of_callbacks=60050
- MAX_ALLOCS_ALLOWED_modifying_1000_circular_buffer_elements=0
- MAX_ALLOCS_ALLOWED_modifying_byte_buffer_view=2050
Expand Down
1 change: 1 addition & 0 deletions docker/docker-compose.2004.main.yaml
Expand Up @@ -41,6 +41,7 @@ services:
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_holding_buffer_with_space=3
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_new_buffer=3050
- MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_new_buffer_with_space=3050
- MAX_ALLOCS_ALLOWED_future_erase_result=4050
- MAX_ALLOCS_ALLOWED_future_lots_of_callbacks=60050
- MAX_ALLOCS_ALLOWED_modifying_1000_circular_buffer_elements=0
- MAX_ALLOCS_ALLOWED_modifying_byte_buffer_view=2050
Expand Down

0 comments on commit 43fa768

Please sign in to comment.