134 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++0310 11#include <benchmark/benchmark.h>12#include <exception>13 14void bm_make_exception_ptr(benchmark::State& state) {15 for (auto _ : state) {16 benchmark::DoNotOptimize(std::make_exception_ptr(42));17 }18}19BENCHMARK(bm_make_exception_ptr)->ThreadRange(1, 8);20 21void bm_exception_ptr_copy_ctor_nonnull(benchmark::State& state) {22 std::exception_ptr excptr = std::make_exception_ptr(42);23 for (auto _ : state) {24 benchmark::DoNotOptimize(std::exception_ptr(excptr));25 }26}27BENCHMARK(bm_exception_ptr_copy_ctor_nonnull);28 29void bm_exception_ptr_copy_ctor_null(benchmark::State& state) {30 std::exception_ptr excptr = nullptr;31 for (auto _ : state) {32 std::exception_ptr excptr_copy(excptr);33 // The compiler should be able to constant-fold the comparison34 benchmark::DoNotOptimize(excptr_copy == nullptr);35 benchmark::DoNotOptimize(excptr_copy);36 }37}38BENCHMARK(bm_exception_ptr_copy_ctor_null);39 40void bm_exception_ptr_move_ctor_nonnull(benchmark::State& state) {41 std::exception_ptr excptr = std::make_exception_ptr(42);42 for (auto _ : state) {43 // Need to copy, such that the `excptr` is not moved from and44 // empty after the first loop iteration.45 std::exception_ptr excptr_copy(excptr);46 benchmark::DoNotOptimize(std::exception_ptr(std::move(excptr_copy)));47 }48}49BENCHMARK(bm_exception_ptr_move_ctor_nonnull);50 51void bm_exception_ptr_move_ctor_null(benchmark::State& state) {52 std::exception_ptr excptr = nullptr;53 for (auto _ : state) {54 std::exception_ptr new_excptr(std::move(excptr));55 // The compiler should be able to constant-fold the comparison56 benchmark::DoNotOptimize(new_excptr == nullptr);57 benchmark::DoNotOptimize(new_excptr);58 }59}60BENCHMARK(bm_exception_ptr_move_ctor_null);61 62void bm_exception_ptr_copy_assign_nonnull(benchmark::State& state) {63 std::exception_ptr excptr = std::make_exception_ptr(42);64 for (auto _ : state) {65 std::exception_ptr new_excptr;66 new_excptr = excptr;67 benchmark::DoNotOptimize(new_excptr);68 }69}70BENCHMARK(bm_exception_ptr_copy_assign_nonnull);71 72void bm_exception_ptr_copy_assign_null(benchmark::State& state) {73 std::exception_ptr excptr = nullptr;74 for (auto _ : state) {75 std::exception_ptr new_excptr;76 new_excptr = excptr;77 // The compiler should be able to constant-fold the comparison78 benchmark::DoNotOptimize(new_excptr == nullptr);79 benchmark::DoNotOptimize(new_excptr);80 }81}82BENCHMARK(bm_exception_ptr_copy_assign_null);83 84void bm_exception_ptr_move_assign_nonnull(benchmark::State& state) {85 std::exception_ptr excptr = std::make_exception_ptr(42);86 for (auto _ : state) {87 // Need to copy, such that the `excptr` is not moved from and88 // empty after the first loop iteration.89 std::exception_ptr excptr_copy(excptr);90 std::exception_ptr new_excptr;91 new_excptr = std::move(excptr_copy);92 benchmark::DoNotOptimize(new_excptr);93 }94}95BENCHMARK(bm_exception_ptr_move_assign_nonnull);96 97void bm_exception_ptr_move_assign_null(benchmark::State& state) {98 std::exception_ptr excptr = nullptr;99 for (auto _ : state) {100 std::exception_ptr new_excptr;101 new_excptr = std::move(excptr);102 // The compiler should be able to constant-fold the comparison103 benchmark::DoNotOptimize(new_excptr == nullptr);104 benchmark::DoNotOptimize(new_excptr);105 }106}107BENCHMARK(bm_exception_ptr_move_assign_null);108 109void bm_exception_ptr_swap_nonnull(benchmark::State& state) {110 std::exception_ptr excptr1 = std::make_exception_ptr(41);111 std::exception_ptr excptr2 = std::make_exception_ptr(42);112 for (auto _ : state) {113 swap(excptr1, excptr2);114 benchmark::DoNotOptimize(excptr1);115 benchmark::DoNotOptimize(excptr2);116 }117}118BENCHMARK(bm_exception_ptr_swap_nonnull);119 120void bm_exception_ptr_swap_null(benchmark::State& state) {121 std::exception_ptr excptr1 = nullptr;122 std::exception_ptr excptr2 = nullptr;123 for (auto _ : state) {124 swap(excptr1, excptr2);125 // The compiler should be able to constant-fold those comparisons126 benchmark::DoNotOptimize(excptr1 == nullptr);127 benchmark::DoNotOptimize(excptr2 == nullptr);128 benchmark::DoNotOptimize(excptr1 == excptr2);129 }130}131BENCHMARK(bm_exception_ptr_swap_null);132 133BENCHMARK_MAIN();134