• 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 "absl/flags/flag.h"
17 #include "absl/time/time.h"
18 #include "absl/types/optional.h"
19 #include "benchmark/benchmark.h"
20 
21 namespace {
22 using String = std::string;
23 using VectorOfStrings = std::vector<std::string>;
24 using AbslDuration = absl::Duration;
25 
26 // We do not want to take over marshalling for the types absl::optional<int>,
27 // absl::optional<std::string> which we do not own. Instead we introduce unique
28 // "aliases" to these types, which we do.
29 using AbslOptionalInt = absl::optional<int>;
30 struct OptionalInt : AbslOptionalInt {
31   using AbslOptionalInt::AbslOptionalInt;
32 };
33 // Next two functions represent Abseil Flags marshalling for OptionalInt.
AbslParseFlag(absl::string_view src,OptionalInt * flag,std::string * error)34 bool AbslParseFlag(absl::string_view src, OptionalInt* flag,
35                    std::string* error) {
36   int val;
37   if (src.empty())
38     flag->reset();
39   else if (!absl::ParseFlag(src, &val, error))
40     return false;
41   *flag = val;
42   return true;
43 }
AbslUnparseFlag(const OptionalInt & flag)44 std::string AbslUnparseFlag(const OptionalInt& flag) {
45   return !flag ? "" : absl::UnparseFlag(*flag);
46 }
47 
48 using AbslOptionalString = absl::optional<std::string>;
49 struct OptionalString : AbslOptionalString {
50   using AbslOptionalString::AbslOptionalString;
51 };
52 // Next two functions represent Abseil Flags marshalling for OptionalString.
AbslParseFlag(absl::string_view src,OptionalString * flag,std::string * error)53 bool AbslParseFlag(absl::string_view src, OptionalString* flag,
54                    std::string* error) {
55   std::string val;
56   if (src.empty())
57     flag->reset();
58   else if (!absl::ParseFlag(src, &val, error))
59     return false;
60   *flag = val;
61   return true;
62 }
AbslUnparseFlag(const OptionalString & flag)63 std::string AbslUnparseFlag(const OptionalString& flag) {
64   return !flag ? "" : absl::UnparseFlag(*flag);
65 }
66 
67 struct UDT {
68   UDT() = default;
UDT__anon67ad14db0111::UDT69   UDT(const UDT&) {}
operator =__anon67ad14db0111::UDT70   UDT& operator=(const UDT&) { return *this; }
71 };
72 // Next two functions represent Abseil Flags marshalling for UDT.
AbslParseFlag(absl::string_view,UDT *,std::string *)73 bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
AbslUnparseFlag(const UDT &)74 std::string AbslUnparseFlag(const UDT&) { return ""; }
75 
76 }  // namespace
77 
78 #define BENCHMARKED_TYPES(A) \
79   A(bool)                    \
80   A(int16_t)                 \
81   A(uint16_t)                \
82   A(int32_t)                 \
83   A(uint32_t)                \
84   A(int64_t)                 \
85   A(uint64_t)                \
86   A(double)                  \
87   A(float)                   \
88   A(String)                  \
89   A(VectorOfStrings)         \
90   A(OptionalInt)             \
91   A(OptionalString)          \
92   A(AbslDuration)            \
93   A(UDT)
94 
95 #define FLAG_DEF(T) ABSL_FLAG(T, T##_flag, {}, "");
96 
97 BENCHMARKED_TYPES(FLAG_DEF)
98 
99 namespace {
100 
101 #define BM_GetFlag(T)                                            \
102   void BM_GetFlag_##T(benchmark::State& state) {                 \
103     for (auto _ : state) {                                       \
104       benchmark::DoNotOptimize(absl::GetFlag(FLAGS_##T##_flag)); \
105     }                                                            \
106   }                                                              \
107   BENCHMARK(BM_GetFlag_##T);
108 
109 BENCHMARKED_TYPES(BM_GetFlag)
110 
111 }  // namespace
112