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

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