diff options
author | Sirui Mu <msrlancern@gmail.com> | 2023-09-08 09:19:02 -0400 |
---|---|---|
committer | Louis Dionne <ldionne.2@gmail.com> | 2023-09-08 11:47:24 -0400 |
commit | 679c0b48d7418b40996e5dcab61c0ffa73089718 (patch) | |
tree | 378f53245296e3dc3e75d97ebce967cc3fac1279 /libcxx/benchmarks | |
parent | 08de6508ab6af53779d2daf276295473c5b0906e (diff) | |
download | llvm-679c0b48d7418b40996e5dcab61c0ffa73089718.zip llvm-679c0b48d7418b40996e5dcab61c0ffa73089718.tar.gz llvm-679c0b48d7418b40996e5dcab61c0ffa73089718.tar.bz2 |
[libc++abi] Refactor around __dynamic_cast
This commit contains refactorings around __dynamic_cast without changing
its behavior. Some important changes include:
- Refactor __dynamic_cast into various small helper functions;
- Move dynamic_cast_stress.pass.cpp to libcxx/benchmarks and refactor
it into a benchmark. The benchmark performance numbers are updated
as well.
Differential Revision: https://reviews.llvm.org/D138006
Diffstat (limited to 'libcxx/benchmarks')
-rw-r--r-- | libcxx/benchmarks/CMakeLists.txt | 1 | ||||
-rw-r--r-- | libcxx/benchmarks/libcxxabi/dynamic_cast_old_stress.bench.cpp | 77 |
2 files changed, 78 insertions, 0 deletions
diff --git a/libcxx/benchmarks/CMakeLists.txt b/libcxx/benchmarks/CMakeLists.txt index d79c647..66cc293 100644 --- a/libcxx/benchmarks/CMakeLists.txt +++ b/libcxx/benchmarks/CMakeLists.txt @@ -183,6 +183,7 @@ set(BENCHMARK_TESTS algorithms/sort_heap.bench.cpp algorithms/stable_sort.bench.cpp libcxxabi/dynamic_cast.bench.cpp + libcxxabi/dynamic_cast_old_stress.bench.cpp allocation.bench.cpp deque.bench.cpp deque_iterator.bench.cpp diff --git a/libcxx/benchmarks/libcxxabi/dynamic_cast_old_stress.bench.cpp b/libcxx/benchmarks/libcxxabi/dynamic_cast_old_stress.bench.cpp new file mode 100644 index 0000000..df4daf7 --- /dev/null +++ b/libcxx/benchmarks/libcxxabi/dynamic_cast_old_stress.bench.cpp @@ -0,0 +1,77 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include <cassert> +#include <cstddef> +#include <utility> + +#include "benchmark/benchmark.h" + +template <std::size_t Indx, std::size_t Depth> +struct C : public virtual C<Indx, Depth - 1>, public virtual C<Indx + 1, Depth - 1> { + virtual ~C() {} +}; + +template <std::size_t Indx> +struct C<Indx, 0> { + virtual ~C() {} +}; + +template <std::size_t Indx, std::size_t Depth> +struct B : public virtual C<Indx, Depth - 1>, public virtual C<Indx + 1, Depth - 1> {}; + +template <class Indx, std::size_t Depth> +struct makeB; + +template <std::size_t... Indx, std::size_t Depth> +struct makeB<std::index_sequence<Indx...>, Depth> : public B<Indx, Depth>... {}; + +template <std::size_t Width, std::size_t Depth> +struct A : public makeB<std::make_index_sequence<Width>, Depth> {}; + +constexpr std::size_t Width = 10; +constexpr std::size_t Depth = 5; + +template <typename Destination> +void CastTo(benchmark::State& state) { + A<Width, Depth> a; + auto base = static_cast<C<Width / 2, 0>*>(&a); + + Destination* b = nullptr; + for (auto _ : state) { + b = dynamic_cast<Destination*>(base); + benchmark::DoNotOptimize(b); + } + + assert(b != 0); +} + +BENCHMARK(CastTo<B<Width / 2, Depth>>); +BENCHMARK(CastTo<A<Width, Depth>>); + +BENCHMARK_MAIN(); + +/** + * Benchmark results: (release builds) + * + * libcxxabi: + * ---------------------------------------------------------------------- + * Benchmark Time CPU Iterations + * ---------------------------------------------------------------------- + * CastTo<B<Width / 2, Depth>> 1997 ns 1997 ns 349247 + * CastTo<A<Width, Depth>> 256 ns 256 ns 2733871 + * + * libsupc++: + * ---------------------------------------------------------------------- + * Benchmark Time CPU Iterations + * ---------------------------------------------------------------------- + * CastTo<B<Width / 2, Depth>> 5240 ns 5240 ns 133091 + * CastTo<A<Width, Depth>> 866 ns 866 ns 808600 + * + * + */ |