Add alloc counter benchmark for result erasing maps. (#1858)

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.
This commit is contained in:
Cory Benfield 2021-05-25 12:56:38 +01:00 committed by GitHub
parent 465f87dd7c
commit 43fa7686db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 0 deletions

View File

@ -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
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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