forked from apple/swift-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add alloc counter benchmark for result erasing maps. (apple#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 apple#1697.
- Loading branch information
Showing
6 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
IntegrationTests/tests_04_performance/test_01_resources/test_future_erase_result.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters