llvm-capstone/libcxx/benchmarks/system_error.bench.cpp
Edoardo Sanguineti 66bb6b4fda [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
2023-08-11 13:08:45 -07:00

31 lines
903 B
C++

//===----------------------------------------------------------------------===//
//
// 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();