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//===----------------------------------------------------------------------===//
8
9#include <cassert>
10#include <cstddef>
11#include <utility>
12
13#include "benchmark/benchmark.h"
14
15template <std::size_t Indx, std::size_t Depth>
16struct C : public virtual C<Indx, Depth - 1>, public virtual C<Indx + 1, Depth - 1> {
17 virtual ~C() {}
18};
19
20template <std::size_t Indx>
21struct C<Indx, 0> {
22 virtual ~C() {}
23};
24
25template <std::size_t Indx, std::size_t Depth>
26struct B : public virtual C<Indx, Depth - 1>, public virtual C<Indx + 1, Depth - 1> {};
27
28template <class Indx, std::size_t Depth>
29struct makeB;
30
31template <std::size_t... Indx, std::size_t Depth>
32struct makeB<std::index_sequence<Indx...>, Depth> : public B<Indx, Depth>... {};
33
34template <std::size_t Width, std::size_t Depth>
35struct A : public makeB<std::make_index_sequence<Width>, Depth> {};
36
37constexpr std::size_t Width = 10;
38constexpr std::size_t Depth = 5;
39
40template <typename Destination>
41void CastTo(benchmark::State& state) {
42 A<Width, Depth> a;
43 auto base = static_cast<C<Width / 2, 0>*>(&a);
44
45 Destination* b = nullptr;
46 for (auto _ : state) {
47 b = dynamic_cast<Destination*>(base);
48 benchmark::DoNotOptimize(b);
49 }
50
51 assert(b != 0);
52}
53
54BENCHMARK(CastTo<B<Width / 2, Depth>>);
55BENCHMARK(CastTo<A<Width, Depth>>);
56
57BENCHMARK_MAIN();
58
59/**
60 * Benchmark results: (release builds)
61 *
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
68 *
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
75 *
76 *
77 */
78

source code of libcxx/benchmarks/libcxxabi/dynamic_cast_old_stress.bench.cpp