aboutsummaryrefslogtreecommitdiff
path: root/libcxx/benchmarks
diff options
context:
space:
mode:
authorEdoardo Sanguineti <edoardo.sanguineti222@gmail.com>2023-08-11 13:06:41 -0700
committerNikolas Klauser <nikolasklauser@berlin.de>2023-08-11 13:08:45 -0700
commit66bb6b4fdaedf1c4f5bc19a6fc7d2f5c33aa9471 (patch)
tree2956853138a17c83518addeaa9b3cbf532984c72 /libcxx/benchmarks
parente789bcbb967f391d4f641bc8fa0403a45039a592 (diff)
downloadllvm-66bb6b4fdaedf1c4f5bc19a6fc7d2f5c33aa9471.zip
llvm-66bb6b4fdaedf1c4f5bc19a6fc7d2f5c33aa9471.tar.gz
llvm-66bb6b4fdaedf1c4f5bc19a6fc7d2f5c33aa9471.tar.bz2
[libc++] Optimize internal function in <system_error>
In the event the internal function __init is called with an empty string the code will take unnecessary extra steps, in addition, the code generated might be overall greater because, to my understanding, when initializing a string with an empty `const char*` "" (like in this case), the compiler might be unable to deduce the string is indeed empty at compile time and more code is generated. The goal of this patch is to make a new internal function that will accept just an error code skipping the empty string argument. It should skip the unnecessary steps and in the event `if (ec)` is `false`, it will return an empty string using the correct ctor, avoiding any extra code generation issues. After the conversation about this patch matured in the libcxx channel on the LLVM Discord server, the patch was analyzed quickly with "Compiler Explorer" and other tools and it was discovered that it does indeed reduce the amount of code generated when using the latest stable clang version (16) which in turn produces faster code. This patch targets LLVM 18 as it will break the ABI by addressing https://github.com/llvm/llvm-project/issues/63985 Benchmark tests run on other machines as well show in the best case, that the new version without the extra string as an argument performs 10 times faster. On the buildkite CI run it shows the new code takes less CPU time as well. In conclusion, the new code should also just appear cleaner because there are fewer checks to do when there is no message. Reviewed By: #libc, philnik Spies: emaste, nemanjai, philnik, libcxx-commits Differential Revision: https://reviews.llvm.org/D155820
Diffstat (limited to 'libcxx/benchmarks')
-rw-r--r--libcxx/benchmarks/CMakeLists.txt1
-rw-r--r--libcxx/benchmarks/system_error.bench.cpp30
2 files changed, 31 insertions, 0 deletions
diff --git a/libcxx/benchmarks/CMakeLists.txt b/libcxx/benchmarks/CMakeLists.txt
index 1de1cfa9..b5d3fbf 100644
--- a/libcxx/benchmarks/CMakeLists.txt
+++ b/libcxx/benchmarks/CMakeLists.txt
@@ -192,6 +192,7 @@ set(BENCHMARK_TESTS
std_format_spec_string_unicode.bench.cpp
string.bench.cpp
stringstream.bench.cpp
+ system_error.bench.cpp
to_chars.bench.cpp
unordered_set_operations.bench.cpp
util_smartptr.bench.cpp
diff --git a/libcxx/benchmarks/system_error.bench.cpp b/libcxx/benchmarks/system_error.bench.cpp
new file mode 100644
index 0000000..4b0568d
--- /dev/null
+++ b/libcxx/benchmarks/system_error.bench.cpp
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <string>
+#include <system_error>
+
+#include "benchmark/benchmark.h"
+
+static void BM_SystemErrorWithMessage(benchmark::State& state) {
+ for (auto _ : state) {
+ std::error_code ec{};
+ benchmark::DoNotOptimize(std::system_error{ec, ""});
+ }
+}
+BENCHMARK(BM_SystemErrorWithMessage);
+
+static void BM_SystemErrorWithoutMessage(benchmark::State& state) {
+ for (auto _ : state) {
+ std::error_code ec{};
+ benchmark::DoNotOptimize(std::system_error{ec});
+ }
+}
+BENCHMARK(BM_SystemErrorWithoutMessage);
+
+BENCHMARK_MAIN();