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."
This commit is contained in:
MarcoFalke
2026-01-06 17:05:27 +01:00
parent fa51a28a94
commit fa8938f08c
2 changed files with 4 additions and 3 deletions

View File

@@ -5,6 +5,7 @@
#include <bench/bench.h>
#include <test/util/setup_common.h> // IWYU pragma: keep
#include <util/check.h>
#include <util/fs.h>
#include <chrono>
@@ -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)

View File

@@ -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