Howard Hinnant | a39c104 | 2012-01-13 01:22:31 +0000 | [diff] [blame] | 1 | //===------------------------- dynamic_cast_stress.cpp --------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include <cassert> |
| 11 | #include <tuple> |
Eric Fiselier | 30ad829 | 2014-11-24 22:38:57 +0000 | [diff] [blame] | 12 | #include "support/timer.hpp" |
Howard Hinnant | a39c104 | 2012-01-13 01:22:31 +0000 | [diff] [blame] | 13 | |
| 14 | template <std::size_t Indx, std::size_t Depth> |
| 15 | struct C |
| 16 | : public virtual C<Indx, Depth-1>, |
| 17 | public virtual C<Indx+1, Depth-1> |
| 18 | { |
| 19 | virtual ~C() {} |
| 20 | }; |
| 21 | |
| 22 | template <std::size_t Indx> |
| 23 | struct C<Indx, 0> |
| 24 | { |
| 25 | virtual ~C() {} |
| 26 | }; |
| 27 | |
| 28 | template <std::size_t Indx, std::size_t Depth> |
| 29 | struct B |
| 30 | : public virtual C<Indx, Depth-1>, |
| 31 | public virtual C<Indx+1, Depth-1> |
| 32 | { |
| 33 | }; |
| 34 | |
| 35 | template <class Indx, std::size_t Depth> |
| 36 | struct makeB; |
| 37 | |
| 38 | template <std::size_t ...Indx, std::size_t Depth> |
| 39 | struct makeB<std::__tuple_indices<Indx...>, Depth> |
| 40 | : public B<Indx, Depth>... |
| 41 | { |
| 42 | }; |
| 43 | |
| 44 | template <std::size_t Width, std::size_t Depth> |
| 45 | struct A |
| 46 | : public makeB<typename std::__make_tuple_indices<Width>::type, Depth> |
| 47 | { |
| 48 | }; |
| 49 | |
| 50 | void test() |
| 51 | { |
Howard Hinnant | 830713c | 2012-01-31 23:52:20 +0000 | [diff] [blame] | 52 | const std::size_t Width = 10; |
| 53 | const std::size_t Depth = 5; |
Howard Hinnant | a39c104 | 2012-01-13 01:22:31 +0000 | [diff] [blame] | 54 | A<Width, Depth> a; |
| 55 | typedef B<Width/2, Depth> Destination; |
| 56 | // typedef A<Width, Depth> Destination; |
Eric Fiselier | 30ad829 | 2014-11-24 22:38:57 +0000 | [diff] [blame] | 57 | Destination *b = nullptr; |
| 58 | { |
| 59 | timer t; |
| 60 | b = dynamic_cast<Destination*>((C<Width/2, 0>*)&a); |
| 61 | } |
Howard Hinnant | a39c104 | 2012-01-13 01:22:31 +0000 | [diff] [blame] | 62 | assert(b != 0); |
| 63 | } |
| 64 | |
| 65 | int main() |
| 66 | { |
| 67 | test(); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | Timing results I'm seeing (median of 3 microseconds): |
| 72 | |
| 73 | libc++abi gcc's dynamic_cast |
Howard Hinnant | 6525563 | 2012-01-16 18:21:05 +0000 | [diff] [blame] | 74 | B<Width/2, Depth> -O3 48.334 93.190 libc++abi 93% faster |
| 75 | B<Width/2, Depth> -Os 58.535 94.103 libc++abi 61% faster |
| 76 | A<Width, Depth> -O3 11.515 33.134 libc++abi 188% faster |
| 77 | A<Width, Depth> -Os 12.631 31.553 libc++abi 150% faster |
Howard Hinnant | a39c104 | 2012-01-13 01:22:31 +0000 | [diff] [blame] | 78 | |
| 79 | */ |