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 <cstddef>
10
11#include "benchmark/benchmark.h"
12
13template <std::size_t Depth>
14struct Chain : Chain<Depth - 1> {};
15
16template <>
17struct Chain<0> {
18 virtual ~Chain() noexcept = default;
19};
20
21template <std::size_t Index, std::size_t Depth>
22struct Dag : Dag<Index, Depth - 1>, Dag<Index + 1, Depth - 1> {};
23
24template <std::size_t Index>
25struct Dag<Index, 0> {
26 virtual ~Dag() noexcept = default;
27};
28
29template <std::size_t Depth>
30struct VChain : virtual VChain<Depth - 1> {};
31
32template <>
33struct VChain<0> {
34 virtual ~VChain() noexcept = default;
35};
36
37template <std::size_t Index, std::size_t Depth>
38struct VDag : virtual VDag<Index, Depth - 1>, virtual VDag<Index + 1, Depth - 1> {};
39
40template <std::size_t Index>
41struct VDag<Index, 0> {
42 virtual ~VDag() noexcept = default;
43};
44
45template <typename Dyn, typename From, typename To = Dyn>
46static void DynCast(benchmark::State& state) {
47 Dyn obj;
48 From* from_ptr = &obj;
49 for (auto _ : state) {
50 To* to_ptr = dynamic_cast<To*>(from_ptr);
51 benchmark::DoNotOptimize(to_ptr);
52 }
53}
54
55static void StaticCast(benchmark::State& state) {
56 Chain<9> obj;
57 Chain<0>* from_ptr = &obj;
58 for (auto _ : state) {
59 Chain<9>* to_ptr = static_cast<Chain<9>*>(from_ptr);
60 benchmark::DoNotOptimize(value&: to_ptr);
61 }
62}
63
64// Downcast along a chain from base to the most derived type
65BENCHMARK(DynCast<Chain<1>, Chain<0>>)->Name(name: "Chain, 1 level");
66BENCHMARK(DynCast<Chain<2>, Chain<0>>)->Name(name: "Chain, 2 levels");
67BENCHMARK(DynCast<Chain<3>, Chain<0>>)->Name(name: "Chain, 3 levels");
68BENCHMARK(DynCast<Chain<4>, Chain<0>>)->Name(name: "Chain, 4 levels");
69BENCHMARK(DynCast<Chain<5>, Chain<0>>)->Name(name: "Chain, 5 levels");
70BENCHMARK(DynCast<Chain<6>, Chain<0>>)->Name(name: "Chain, 6 levels");
71BENCHMARK(DynCast<Chain<7>, Chain<0>>)->Name(name: "Chain, 7 levels");
72BENCHMARK(DynCast<Chain<8>, Chain<0>>)->Name(name: "Chain, 8 levels");
73BENCHMARK(DynCast<Chain<9>, Chain<0>>)->Name(name: "Chain, 9 levels");
74
75// Downcast along a chain from base to the middle of the chain
76BENCHMARK(DynCast<Chain<2>, Chain<0>, Chain<1>>)->Name(name: "Chain middle, 1 level");
77BENCHMARK(DynCast<Chain<4>, Chain<0>, Chain<2>>)->Name(name: "Chain middle, 2 levels");
78BENCHMARK(DynCast<Chain<6>, Chain<0>, Chain<3>>)->Name(name: "Chain middle, 3 levels");
79BENCHMARK(DynCast<Chain<8>, Chain<0>, Chain<4>>)->Name(name: "Chain middle, 4 levels");
80
81// Downcast along a chain that fails
82BENCHMARK(DynCast<Chain<1>, Chain<0>, Chain<9>>)->Name(name: "Chain fail, 1 level");
83BENCHMARK(DynCast<Chain<2>, Chain<0>, Chain<9>>)->Name(name: "Chain fail, 2 levels");
84BENCHMARK(DynCast<Chain<3>, Chain<0>, Chain<9>>)->Name(name: "Chain fail, 3 levels");
85BENCHMARK(DynCast<Chain<4>, Chain<0>, Chain<9>>)->Name(name: "Chain fail, 4 levels");
86BENCHMARK(DynCast<Chain<5>, Chain<0>, Chain<9>>)->Name(name: "Chain fail, 5 levels");
87BENCHMARK(DynCast<Chain<6>, Chain<0>, Chain<9>>)->Name(name: "Chain fail, 6 levels");
88BENCHMARK(DynCast<Chain<7>, Chain<0>, Chain<9>>)->Name(name: "Chain fail, 7 levels");
89BENCHMARK(DynCast<Chain<8>, Chain<0>, Chain<9>>)->Name(name: "Chain fail, 8 levels");
90
91// Downcast along a virtual inheritance chain from base to the most derived type
92BENCHMARK(DynCast<VChain<1>, VChain<0>>)->Name(name: "VChain, 1 level");
93BENCHMARK(DynCast<VChain<2>, VChain<0>>)->Name(name: "VChain, 2 levels");
94BENCHMARK(DynCast<VChain<3>, VChain<0>>)->Name(name: "VChain, 3 levels");
95BENCHMARK(DynCast<VChain<4>, VChain<0>>)->Name(name: "VChain, 4 levels");
96BENCHMARK(DynCast<VChain<5>, VChain<0>>)->Name(name: "VChain, 5 levels");
97
98// Downcast along a virtual inheritance chain from base to the middle of the chain
99BENCHMARK(DynCast<VChain<2>, VChain<0>, VChain<1>>)->Name(name: "VChain middle, 1 level");
100BENCHMARK(DynCast<VChain<4>, VChain<0>, VChain<2>>)->Name(name: "VChain middle, 2 levels");
101BENCHMARK(DynCast<VChain<6>, VChain<0>, VChain<3>>)->Name(name: "VChain middle, 3 levels");
102BENCHMARK(DynCast<VChain<8>, VChain<0>, VChain<4>>)->Name(name: "VChain middle, 4 levels");
103
104// Downcast along a virtual chain that fails
105BENCHMARK(DynCast<VChain<1>, VChain<0>, VChain<8>>)->Name(name: "VChain fail, 1 level");
106BENCHMARK(DynCast<VChain<2>, VChain<0>, VChain<8>>)->Name(name: "VChain fail, 2 levels");
107BENCHMARK(DynCast<VChain<3>, VChain<0>, VChain<8>>)->Name(name: "VChain fail, 3 levels");
108BENCHMARK(DynCast<VChain<4>, VChain<0>, VChain<8>>)->Name(name: "VChain fail, 4 levels");
109BENCHMARK(DynCast<VChain<5>, VChain<0>, VChain<8>>)->Name(name: "VChain fail, 5 levels");
110
111// Downcast along a DAG from base to the most derived type
112BENCHMARK(DynCast<Dag<0, 3>, Dag<3, 0>>)->Name(name: "DAG rightmost, 3 levels");
113BENCHMARK(DynCast<Dag<0, 4>, Dag<4, 0>>)->Name(name: "DAG rightmost, 4 levels");
114BENCHMARK(DynCast<Dag<0, 5>, Dag<5, 0>>)->Name(name: "DAG rightmost, 5 levels");
115BENCHMARK(DynCast<Dag<0, 3>, Dag<0, 0>>)->Name(name: "DAG leftmost, 3 levels");
116BENCHMARK(DynCast<Dag<0, 4>, Dag<0, 0>>)->Name(name: "DAG leftmost, 4 levels");
117BENCHMARK(DynCast<Dag<0, 5>, Dag<0, 0>>)->Name(name: "DAG leftmost, 5 levels");
118
119// Downcast along a DAG from base to the middle of the DAG
120BENCHMARK(DynCast<Dag<0, 4>, Dag<4, 0>, Dag<3, 1>>)->Name(name: "DAG rightmost middle, 1 level");
121BENCHMARK(DynCast<Dag<0, 4>, Dag<4, 0>, Dag<2, 2>>)->Name(name: "DAG rightmost middle, 2 levels");
122BENCHMARK(DynCast<Dag<0, 4>, Dag<4, 0>, Dag<1, 3>>)->Name(name: "DAG rightmost middle, 3 levels");
123BENCHMARK(DynCast<Dag<0, 4>, Dag<0, 0>, Dag<0, 1>>)->Name(name: "DAG leftmost middle, 1 level");
124BENCHMARK(DynCast<Dag<0, 4>, Dag<0, 0>, Dag<0, 2>>)->Name(name: "DAG leftmost middle, 2 levels");
125BENCHMARK(DynCast<Dag<0, 4>, Dag<0, 0>, Dag<0, 3>>)->Name(name: "DAG leftmost middle, 3 levels");
126
127// Sidecast along a DAG
128BENCHMARK(DynCast<Dag<0, 3>, Dag<3, 0>, Dag<0, 0>>)->Name(name: "DAG sidecast, 3 levels");
129BENCHMARK(DynCast<Dag<0, 3>, Dag<2, 1>, Dag<0, 1>>)->Name(name: "DAG sidecast, 2 levels");
130BENCHMARK(DynCast<Dag<0, 3>, Dag<1, 2>, Dag<0, 2>>)->Name(name: "DAG sidecast, 1 level");
131
132// Sidecast along a DAG that fails
133BENCHMARK(DynCast<Dag<0, 3>, Dag<3, 0>, Dag<0, 4>>)->Name(name: "DAG sidecast fail, 3 levels");
134BENCHMARK(DynCast<Dag<0, 3>, Dag<2, 1>, Dag<0, 4>>)->Name(name: "DAG sidecast fail, 2 levels");
135BENCHMARK(DynCast<Dag<0, 3>, Dag<1, 2>, Dag<0, 4>>)->Name(name: "DAG sidecast fail, 1 level");
136
137// Downcast along a virtual inheritance DAG from base to the most derived type
138BENCHMARK(DynCast<VDag<0, 3>, VDag<3, 0>>)->Name(name: "VDAG rightmost, 3 levels");
139BENCHMARK(DynCast<VDag<0, 4>, VDag<4, 0>>)->Name(name: "VDAG rightmost, 4 levels");
140BENCHMARK(DynCast<VDag<0, 5>, VDag<5, 0>>)->Name(name: "VDAG rightmost, 5 levels");
141BENCHMARK(DynCast<VDag<0, 3>, VDag<0, 0>>)->Name(name: "VDAG leftmost, 3 levels");
142BENCHMARK(DynCast<VDag<0, 4>, VDag<0, 0>>)->Name(name: "VDAG leftmost, 4 levels");
143BENCHMARK(DynCast<VDag<0, 5>, VDag<0, 0>>)->Name(name: "VDAG leftmost, 5 levels");
144
145// Downcast along a virtual inheritance DAG from base to the middle of the DAG
146BENCHMARK(DynCast<VDag<0, 3>, VDag<3, 0>, VDag<2, 1>>)->Name(name: "VDAG rightmost middle, 1 level");
147BENCHMARK(DynCast<VDag<0, 4>, VDag<4, 0>, VDag<2, 2>>)->Name(name: "VDAG rightmost middle, 2 levels");
148BENCHMARK(DynCast<VDag<0, 5>, VDag<5, 0>, VDag<2, 3>>)->Name(name: "VDAG rightmost middle, 3 levels");
149BENCHMARK(DynCast<VDag<0, 3>, VDag<0, 0>, VDag<0, 1>>)->Name(name: "VDAG leftmost middle, 1 level");
150BENCHMARK(DynCast<VDag<0, 4>, VDag<0, 0>, VDag<0, 2>>)->Name(name: "VDAG leftmost middle, 2 levels");
151BENCHMARK(DynCast<VDag<0, 5>, VDag<0, 0>, VDag<0, 3>>)->Name(name: "VDAG leftmost middle, 3 levels");
152
153// Sidecast along a virtual inheritance DAG
154BENCHMARK(DynCast<VDag<0, 3>, VDag<3, 0>, VDag<0, 0>>)->Name(name: "VDAG sidecast, 3 levels");
155BENCHMARK(DynCast<VDag<0, 3>, VDag<2, 1>, VDag<0, 1>>)->Name(name: "VDAG sidecast, 2 levels");
156BENCHMARK(DynCast<VDag<0, 3>, VDag<1, 2>, VDag<0, 2>>)->Name(name: "VDAG sidecast, 1 level");
157
158// Sidecast along a virtual inheritance DAG that fails
159BENCHMARK(DynCast<VDag<0, 3>, VDag<3, 0>, VDag<0, 4>>)->Name(name: "VDAG sidecast fail, 3 levels");
160BENCHMARK(DynCast<VDag<0, 3>, VDag<2, 1>, VDag<0, 4>>)->Name(name: "VDAG sidecast fail, 2 levels");
161BENCHMARK(DynCast<VDag<0, 3>, VDag<1, 2>, VDag<0, 4>>)->Name(name: "VDAG sidecast fail, 1 level");
162
163// Cast to complete object pointer
164BENCHMARK(DynCast<Chain<8>, Chain<0>, void>)->Name(name: "Chain to complete");
165BENCHMARK(DynCast<VChain<5>, VChain<0>, void>)->Name(name: "VChain to complete");
166BENCHMARK(DynCast<Dag<0, 3>, Dag<3, 0>, void>)->Name(name: "DAG to complete");
167BENCHMARK(DynCast<VDag<0, 3>, VDag<3, 0>, void>)->Name(name: "VDAG to complete");
168
169// Static cast as the baseline.
170BENCHMARK(StaticCast)->Name(name: "Static");
171
172BENCHMARK_MAIN();
173

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