From fa8938f08c9a9da81a482bccb6bfe86f37a5a841 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Tue, 6 Jan 2026 17:05:27 +0100 Subject: [PATCH] bench: Remove incorrect __LINE__ in BENCHMARK macro Duplicate benchmarks with the same name are not supported. Expanding the name with __LINE__ is confusing and brittle, because it makes duplication bugs silent. Fix this twofold: * By enforcing unique benchmarks at compile-time and link-time. For example, a link failure may now look like: "mold: error: duplicate symbol: bench_runner_AddrManAdd" * By enforcing unique benchmarks at run-time. This should never happen, due to the build-failure, but a failure may look like: "Assertion `benchmarks().try_emplace(std::move(name), std::move(func)).second' failed." --- src/bench/bench.cpp | 3 ++- src/bench/bench.h | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/bench/bench.cpp b/src/bench/bench.cpp index 9b74af51bfa..0b2ee6e30d1 100644 --- a/src/bench/bench.cpp +++ b/src/bench/bench.cpp @@ -5,6 +5,7 @@ #include #include // IWYU pragma: keep +#include #include #include @@ -71,7 +72,7 @@ BenchRunner::BenchmarkMap& BenchRunner::benchmarks() BenchRunner::BenchRunner(std::string name, BenchFunction func) { - benchmarks().insert(std::make_pair(name, func)); + Assert(benchmarks().try_emplace(std::move(name), std::move(func)).second); } void BenchRunner::RunAll(const Args& args) diff --git a/src/bench/bench.h b/src/bench/bench.h index 591ea8bc805..14f0c4b053b 100644 --- a/src/bench/bench.h +++ b/src/bench/bench.h @@ -64,8 +64,8 @@ public: }; } // namespace benchmark -// BENCHMARK(foo) expands to: benchmark::BenchRunner bench_11foo{"foo", foo}; +// BENCHMARK(foo) expands to: benchmark::BenchRunner bench_runner_foo{"foo", foo}; #define BENCHMARK(n) \ - benchmark::BenchRunner PASTE2(bench_, PASTE2(__LINE__, n)){STRINGIZE(n), n}; + benchmark::BenchRunner PASTE2(bench_runner_, n){STRINGIZE(n), n}; #endif // BITCOIN_BENCH_BENCH_H