Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / benchmarks / libcxxabi / dynamic_cast_old_stress.bench.cpp
blobdf4daf7409b8f793b11a4ec4a041f4ae26999855
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include <cassert>
10 #include <cstddef>
11 #include <utility>
13 #include "benchmark/benchmark.h"
15 template <std::size_t Indx, std::size_t Depth>
16 struct C : public virtual C<Indx, Depth - 1>, public virtual C<Indx + 1, Depth - 1> {
17 virtual ~C() {}
20 template <std::size_t Indx>
21 struct C<Indx, 0> {
22 virtual ~C() {}
25 template <std::size_t Indx, std::size_t Depth>
26 struct B : public virtual C<Indx, Depth - 1>, public virtual C<Indx + 1, Depth - 1> {};
28 template <class Indx, std::size_t Depth>
29 struct makeB;
31 template <std::size_t... Indx, std::size_t Depth>
32 struct makeB<std::index_sequence<Indx...>, Depth> : public B<Indx, Depth>... {};
34 template <std::size_t Width, std::size_t Depth>
35 struct A : public makeB<std::make_index_sequence<Width>, Depth> {};
37 constexpr std::size_t Width = 10;
38 constexpr std::size_t Depth = 5;
40 template <typename Destination>
41 void CastTo(benchmark::State& state) {
42 A<Width, Depth> a;
43 auto base = static_cast<C<Width / 2, 0>*>(&a);
45 Destination* b = nullptr;
46 for (auto _ : state) {
47 b = dynamic_cast<Destination*>(base);
48 benchmark::DoNotOptimize(b);
51 assert(b != 0);
54 BENCHMARK(CastTo<B<Width / 2, Depth>>);
55 BENCHMARK(CastTo<A<Width, Depth>>);
57 BENCHMARK_MAIN();
59 /**
60 * Benchmark results: (release builds)
62 * libcxxabi:
63 * ----------------------------------------------------------------------
64 * Benchmark Time CPU Iterations
65 * ----------------------------------------------------------------------
66 * CastTo<B<Width / 2, Depth>> 1997 ns 1997 ns 349247
67 * CastTo<A<Width, Depth>> 256 ns 256 ns 2733871
69 * libsupc++:
70 * ----------------------------------------------------------------------
71 * Benchmark Time CPU Iterations
72 * ----------------------------------------------------------------------
73 * CastTo<B<Width / 2, Depth>> 5240 ns 5240 ns 133091
74 * CastTo<A<Width, Depth>> 866 ns 866 ns 808600