• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2020 The Abseil Authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      https://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include <stdint.h>
17 
18 #include <string>
19 #include <vector>
20 
21 #include "absl/flags/flag.h"
22 #include "absl/flags/marshalling.h"
23 #include "absl/flags/parse.h"
24 #include "absl/flags/reflection.h"
25 #include "absl/strings/string_view.h"
26 #include "absl/time/time.h"
27 #include "absl/types/optional.h"
28 #include "benchmark/benchmark.h"
29 
30 namespace {
31 using String = std::string;
32 using VectorOfStrings = std::vector<std::string>;
33 using AbslDuration = absl::Duration;
34 
35 // We do not want to take over marshalling for the types absl::optional<int>,
36 // absl::optional<std::string> which we do not own. Instead we introduce unique
37 // "aliases" to these types, which we do.
38 using AbslOptionalInt = absl::optional<int>;
39 struct OptionalInt : AbslOptionalInt {
40   using AbslOptionalInt::AbslOptionalInt;
41 };
42 // Next two functions represent Abseil Flags marshalling for OptionalInt.
AbslParseFlag(absl::string_view src,OptionalInt * flag,std::string * error)43 bool AbslParseFlag(absl::string_view src, OptionalInt* flag,
44                    std::string* error) {
45   int val;
46   if (src.empty())
47     flag->reset();
48   else if (!absl::ParseFlag(src, &val, error))
49     return false;
50   *flag = val;
51   return true;
52 }
AbslUnparseFlag(const OptionalInt & flag)53 std::string AbslUnparseFlag(const OptionalInt& flag) {
54   return !flag ? "" : absl::UnparseFlag(*flag);
55 }
56 
57 using AbslOptionalString = absl::optional<std::string>;
58 struct OptionalString : AbslOptionalString {
59   using AbslOptionalString::AbslOptionalString;
60 };
61 // Next two functions represent Abseil Flags marshalling for OptionalString.
AbslParseFlag(absl::string_view src,OptionalString * flag,std::string * error)62 bool AbslParseFlag(absl::string_view src, OptionalString* flag,
63                    std::string* error) {
64   std::string val;
65   if (src.empty())
66     flag->reset();
67   else if (!absl::ParseFlag(src, &val, error))
68     return false;
69   *flag = val;
70   return true;
71 }
AbslUnparseFlag(const OptionalString & flag)72 std::string AbslUnparseFlag(const OptionalString& flag) {
73   return !flag ? "" : absl::UnparseFlag(*flag);
74 }
75 
76 struct UDT {
77   UDT() = default;
UDT__anon5ad54c320111::UDT78   UDT(const UDT&) {}
operator =__anon5ad54c320111::UDT79   UDT& operator=(const UDT&) { return *this; }
80 };
81 // Next two functions represent Abseil Flags marshalling for UDT.
AbslParseFlag(absl::string_view,UDT *,std::string *)82 bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
AbslUnparseFlag(const UDT &)83 std::string AbslUnparseFlag(const UDT&) { return ""; }
84 
85 }  // namespace
86 
87 #define BENCHMARKED_TYPES(A) \
88   A(bool)                    \
89   A(int16_t)                 \
90   A(uint16_t)                \
91   A(int32_t)                 \
92   A(uint32_t)                \
93   A(int64_t)                 \
94   A(uint64_t)                \
95   A(double)                  \
96   A(float)                   \
97   A(String)                  \
98   A(VectorOfStrings)         \
99   A(OptionalInt)             \
100   A(OptionalString)          \
101   A(AbslDuration)            \
102   A(UDT)
103 
104 #define FLAG_DEF(T) ABSL_FLAG(T, T##_flag, {}, "");
105 
106 BENCHMARKED_TYPES(FLAG_DEF)
107 
108 // Register thousands of flags to bloat up the size of the registry.
109 // This mimics real life production binaries.
110 #define DEFINE_FLAG_0(name) ABSL_FLAG(int, name, 0, "");
111 #define DEFINE_FLAG_1(name) DEFINE_FLAG_0(name##0) DEFINE_FLAG_0(name##1)
112 #define DEFINE_FLAG_2(name) DEFINE_FLAG_1(name##0) DEFINE_FLAG_1(name##1)
113 #define DEFINE_FLAG_3(name) DEFINE_FLAG_2(name##0) DEFINE_FLAG_2(name##1)
114 #define DEFINE_FLAG_4(name) DEFINE_FLAG_3(name##0) DEFINE_FLAG_3(name##1)
115 #define DEFINE_FLAG_5(name) DEFINE_FLAG_4(name##0) DEFINE_FLAG_4(name##1)
116 #define DEFINE_FLAG_6(name) DEFINE_FLAG_5(name##0) DEFINE_FLAG_5(name##1)
117 #define DEFINE_FLAG_7(name) DEFINE_FLAG_6(name##0) DEFINE_FLAG_6(name##1)
118 #define DEFINE_FLAG_8(name) DEFINE_FLAG_7(name##0) DEFINE_FLAG_7(name##1)
119 #define DEFINE_FLAG_9(name) DEFINE_FLAG_8(name##0) DEFINE_FLAG_8(name##1)
120 #define DEFINE_FLAG_10(name) DEFINE_FLAG_9(name##0) DEFINE_FLAG_9(name##1)
121 #define DEFINE_FLAG_11(name) DEFINE_FLAG_10(name##0) DEFINE_FLAG_10(name##1)
122 #define DEFINE_FLAG_12(name) DEFINE_FLAG_11(name##0) DEFINE_FLAG_11(name##1)
123 DEFINE_FLAG_12(bloat_flag_);
124 
125 namespace {
126 
127 #define BM_GetFlag(T)                                            \
128   void BM_GetFlag_##T(benchmark::State& state) {                 \
129     for (auto _ : state) {                                       \
130       benchmark::DoNotOptimize(absl::GetFlag(FLAGS_##T##_flag)); \
131     }                                                            \
132   }                                                              \
133   BENCHMARK(BM_GetFlag_##T);
134 
BENCHMARKED_TYPES(BM_GetFlag)135 BENCHMARKED_TYPES(BM_GetFlag)
136 
137 void BM_ThreadedFindCommandLineFlag(benchmark::State& state) {
138   char dummy[] = "dummy";
139   char* argv[] = {dummy};
140   // We need to ensure that flags have been parsed. That is where the registry
141   // is finalized.
142   absl::ParseCommandLine(1, argv);
143 
144   for (auto s : state) {
145     benchmark::DoNotOptimize(
146         absl::FindCommandLineFlag("bloat_flag_010101010101"));
147   }
148 }
149 BENCHMARK(BM_ThreadedFindCommandLineFlag)->ThreadRange(1, 16);
150 
151 }  // namespace
152 
153 #define InvokeGetFlag(T)                                               \
154   T AbslInvokeGetFlag##T() { return absl::GetFlag(FLAGS_##T##_flag); } \
155   int odr##T = (benchmark::DoNotOptimize(AbslInvokeGetFlag##T), 1);
156 
157 BENCHMARKED_TYPES(InvokeGetFlag)
158 
159 // To veiw disassembly use: gdb ${BINARY}  -batch -ex "disassemble /s $FUNC"
160