From 43fa7686db1fefeb0080e7dfa71a89fed167b051 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Tue, 25 May 2021 12:56:38 +0100 Subject: [PATCH] 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. --- .../test_future_erase_result.swift | 40 +++++++++++++++++++ docker/docker-compose.1604.52.yaml | 1 + docker/docker-compose.1604.53.yaml | 1 + docker/docker-compose.1804.51.yaml | 1 + docker/docker-compose.2004.54.yaml | 1 + docker/docker-compose.2004.main.yaml | 1 + 6 files changed, 45 insertions(+) create mode 100644 IntegrationTests/tests_04_performance/test_01_resources/test_future_erase_result.swift diff --git a/IntegrationTests/tests_04_performance/test_01_resources/test_future_erase_result.swift b/IntegrationTests/tests_04_performance/test_01_resources/test_future_erase_result.swift new file mode 100644 index 0000000000..d2d565efd8 --- /dev/null +++ b/IntegrationTests/tests_04_performance/test_01_resources/test_future_erase_result.swift @@ -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 + } +} diff --git a/docker/docker-compose.1604.52.yaml b/docker/docker-compose.1604.52.yaml index 0461ab79d6..79e926112e 100644 --- a/docker/docker-compose.1604.52.yaml +++ b/docker/docker-compose.1604.52.yaml @@ -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 diff --git a/docker/docker-compose.1604.53.yaml b/docker/docker-compose.1604.53.yaml index d5368b9c34..d48b152344 100644 --- a/docker/docker-compose.1604.53.yaml +++ b/docker/docker-compose.1604.53.yaml @@ -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 diff --git a/docker/docker-compose.1804.51.yaml b/docker/docker-compose.1804.51.yaml index f3c2110cb6..9cc6504252 100644 --- a/docker/docker-compose.1804.51.yaml +++ b/docker/docker-compose.1804.51.yaml @@ -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 diff --git a/docker/docker-compose.2004.54.yaml b/docker/docker-compose.2004.54.yaml index c8abb4c4b7..bf31598aaf 100644 --- a/docker/docker-compose.2004.54.yaml +++ b/docker/docker-compose.2004.54.yaml @@ -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 diff --git a/docker/docker-compose.2004.main.yaml b/docker/docker-compose.2004.main.yaml index 127f83e9f6..ac3fbf7209 100644 --- a/docker/docker-compose.2004.main.yaml +++ b/docker/docker-compose.2004.main.yaml @@ -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