1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Every benchmark should have the same performance as the corresponding
16 // headroom benchmark.
17 
18 #include "absl/base/internal/raw_logging.h"
19 #include "absl/container/internal/layout.h"
20 #include "benchmark/benchmark.h"
21 
22 namespace absl {
23 ABSL_NAMESPACE_BEGIN
24 namespace container_internal {
25 namespace {
26 
27 using ::benchmark::DoNotOptimize;
28 
29 using Int128 = int64_t[2];
30 
31 // This benchmark provides the upper bound on performance for BM_OffsetConstant.
32 template <size_t Offset, class... Ts>
BM_OffsetConstantHeadroom(benchmark::State & state)33 void BM_OffsetConstantHeadroom(benchmark::State& state) {
34   for (auto _ : state) {
35     DoNotOptimize(Offset);
36   }
37 }
38 
39 template <size_t Offset, class... Ts>
BM_OffsetConstant(benchmark::State & state)40 void BM_OffsetConstant(benchmark::State& state) {
41   using L = Layout<Ts...>;
42   ABSL_RAW_CHECK(L::Partial(3, 5, 7).template Offset<3>() == Offset,
43                  "Invalid offset");
44   for (auto _ : state) {
45     DoNotOptimize(L::Partial(3, 5, 7).template Offset<3>());
46   }
47 }
48 
49 template <class... Ts>
50 size_t VariableOffset(size_t n, size_t m, size_t k);
51 
52 template <>
VariableOffset(size_t n,size_t m,size_t k)53 size_t VariableOffset<int8_t, int16_t, int32_t, Int128>(size_t n, size_t m,
54                                                         size_t k) {
55   auto Align = [](size_t n, size_t m) { return (n + m - 1) & ~(m - 1); };
56   return Align(Align(Align(n * 1, 2) + m * 2, 4) + k * 4, 8);
57 }
58 
59 template <>
VariableOffset(size_t n,size_t m,size_t k)60 size_t VariableOffset<Int128, int32_t, int16_t, int8_t>(size_t n, size_t m,
61                                                         size_t k) {
62   // No alignment is necessary.
63   return n * 16 + m * 4 + k * 2;
64 }
65 
66 // This benchmark provides the upper bound on performance for BM_OffsetVariable.
67 template <size_t Offset, class... Ts>
BM_OffsetVariableHeadroom(benchmark::State & state)68 void BM_OffsetVariableHeadroom(benchmark::State& state) {
69   size_t n = 3;
70   size_t m = 5;
71   size_t k = 7;
72   ABSL_RAW_CHECK(VariableOffset<Ts...>(n, m, k) == Offset, "Invalid offset");
73   for (auto _ : state) {
74     DoNotOptimize(n);
75     DoNotOptimize(m);
76     DoNotOptimize(k);
77     DoNotOptimize(VariableOffset<Ts...>(n, m, k));
78   }
79 }
80 
81 template <size_t Offset, class... Ts>
BM_OffsetVariable(benchmark::State & state)82 void BM_OffsetVariable(benchmark::State& state) {
83   using L = Layout<Ts...>;
84   size_t n = 3;
85   size_t m = 5;
86   size_t k = 7;
87   ABSL_RAW_CHECK(L::Partial(n, m, k).template Offset<3>() == Offset,
88                  "Inavlid offset");
89   for (auto _ : state) {
90     DoNotOptimize(n);
91     DoNotOptimize(m);
92     DoNotOptimize(k);
93     DoNotOptimize(L::Partial(n, m, k).template Offset<3>());
94   }
95 }
96 
97 // Run all benchmarks in two modes:
98 //
99 //   Layout with padding: int8_t[3], int16_t[5], int32_t[7], Int128[?].
100 //   Layout without padding: Int128[3], int32_t[5], int16_t[7], int8_t[?].
101 
102 #define OFFSET_BENCHMARK(NAME, OFFSET, T1, T2, T3, T4) \
103   auto& NAME##_##OFFSET##_##T1##_##T2##_##T3##_##T4 =  \
104       NAME<OFFSET, T1, T2, T3, T4>;                    \
105   BENCHMARK(NAME##_##OFFSET##_##T1##_##T2##_##T3##_##T4)
106 
107 OFFSET_BENCHMARK(BM_OffsetConstantHeadroom, 48, int8_t, int16_t, int32_t,
108                  Int128);
109 OFFSET_BENCHMARK(BM_OffsetConstant, 48, int8_t, int16_t, int32_t, Int128);
110 OFFSET_BENCHMARK(BM_OffsetConstantHeadroom, 82, Int128, int32_t, int16_t,
111                  int8_t);
112 OFFSET_BENCHMARK(BM_OffsetConstant, 82, Int128, int32_t, int16_t, int8_t);
113 OFFSET_BENCHMARK(BM_OffsetVariableHeadroom, 48, int8_t, int16_t, int32_t,
114                  Int128);
115 OFFSET_BENCHMARK(BM_OffsetVariable, 48, int8_t, int16_t, int32_t, Int128);
116 OFFSET_BENCHMARK(BM_OffsetVariableHeadroom, 82, Int128, int32_t, int16_t,
117                  int8_t);
118 OFFSET_BENCHMARK(BM_OffsetVariable, 82, Int128, int32_t, int16_t, int8_t);
119 }  // namespace
120 }  // namespace container_internal
121 ABSL_NAMESPACE_END
122 }  // namespace absl
123