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 | |
17 | template <std::size_t Indx, std::size_t Depth> |
18 | struct C : public virtual C<Indx, Depth - 1>, public virtual C<Indx + 1, Depth - 1> { |
19 | virtual ~C() {} |
20 | }; |
21 | |
22 | template <std::size_t Indx> |
23 | struct C<Indx, 0> { |
24 | virtual ~C() {} |
25 | }; |
26 | |
27 | template <std::size_t Indx, std::size_t Depth> |
28 | struct B : public virtual C<Indx, Depth - 1>, public virtual C<Indx + 1, Depth - 1> {}; |
29 | |
30 | template <class Indx, std::size_t Depth> |
31 | struct makeB; |
32 | |
33 | template <std::size_t... Indx, std::size_t Depth> |
34 | struct makeB<std::index_sequence<Indx...>, Depth> : public B<Indx, Depth>... {}; |
35 | |
36 | template <std::size_t Width, std::size_t Depth> |
37 | struct A : public makeB<std::make_index_sequence<Width>, Depth> {}; |
38 | |
39 | constexpr std::size_t Width = 10; |
40 | constexpr std::size_t Depth = 5; |
41 | |
42 | template <typename Destination> |
43 | void 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 | |
56 | BENCHMARK(CastTo<B<Width / 2, Depth>>); |
57 | BENCHMARK(CastTo<A<Width, Depth>>); |
58 | |
59 | BENCHMARK_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 | |