about summary refs log tree commit diff
path: root/absl/flags
diff options
context:
space:
mode:
Diffstat (limited to 'absl/flags')
-rw-r--r--absl/flags/BUILD.bazel17
-rw-r--r--absl/flags/flag.cc9
-rw-r--r--absl/flags/flag.h24
-rw-r--r--absl/flags/flag_benchmark.cc111
4 files changed, 150 insertions, 11 deletions
diff --git a/absl/flags/BUILD.bazel b/absl/flags/BUILD.bazel
index 6c7b2b6e7950..cbdbae52ed83 100644
--- a/absl/flags/BUILD.bazel
+++ b/absl/flags/BUILD.bazel
@@ -324,6 +324,23 @@ cc_test(
     ],
 )
 
+cc_binary(
+    name = "flag_benchmark",
+    testonly = 1,
+    srcs = [
+        "flag_benchmark.cc",
+    ],
+    copts = ABSL_TEST_COPTS,
+    tags = ["benchmark"],
+    visibility = ["//visibility:private"],
+    deps = [
+        ":flag",
+        "//absl/time",
+        "//absl/types:optional",
+        "@com_github_google_benchmark//:benchmark_main",
+    ],
+)
+
 cc_test(
     name = "marshalling_test",
     size = "small",
diff --git a/absl/flags/flag.cc b/absl/flags/flag.cc
index 9af800796a61..e67f7304c6ff 100644
--- a/absl/flags/flag.cc
+++ b/absl/flags/flag.cc
@@ -22,7 +22,14 @@
 namespace absl {
 ABSL_NAMESPACE_BEGIN
 
-// This global nutex protects on-demand construction of flag objects in MSVC
+#ifndef NDEBUG
+#define ABSL_FLAGS_GET(T) \
+  T GetFlag(const absl::Flag<T>& flag) { return flag.Get(); }
+ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(ABSL_FLAGS_GET)
+#undef ABSL_FLAGS_GET
+#endif
+
+// This global mutex protects on-demand construction of flag objects in MSVC
 // builds.
 #if defined(_MSC_VER) && !defined(__clang__)
 
diff --git a/absl/flags/flag.h b/absl/flags/flag.h
index cc22cdb923e6..bd61668ff4f1 100644
--- a/absl/flags/flag.h
+++ b/absl/flags/flag.h
@@ -186,25 +186,29 @@ class Flag {
 //
 //   // FLAGS_firstname is a Flag of type `std::string`
 //   std::string first_name = absl::GetFlag(FLAGS_firstname);
-template <typename T,
-          typename std::enable_if<
-              !flags_internal::IsAtomicFlagTypeTrait<T>::value, int>::type = 0>
-ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag) {
-  return flag.Get();
-}
-
+#ifndef NDEBUG
 // We want to validate the type mismatch between type definition and
 // declaration. The lock-free implementation does not allow us to do it,
 // so in debug builds we always use the slower implementation, which always
 // validates the type.
-#ifndef NDEBUG
+template <typename T>
+ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag) {
+  return flag.Get();
+}
+// We currently need an external linkage for built-in types because shared
+// libraries have different addresses of flags_internal::FlagOps<T> which
+// might cause log spam when checking the same flag type.
+#define ABSL_FLAGS_INTERNAL_BUILT_IN_EXPORT(T) \
+  ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag);
+ABSL_FLAGS_INTERNAL_BUILTIN_TYPES(ABSL_FLAGS_INTERNAL_BUILT_IN_EXPORT)
+#undef ABSL_FLAGS_INTERNAL_BUILT_IN_EXPORT
+#else
 template <typename T,
           typename std::enable_if<
-              flags_internal::IsAtomicFlagTypeTrait<T>::value, int>::type = 0>
+              !flags_internal::IsAtomicFlagTypeTrait<T>::value, int>::type = 0>
 ABSL_MUST_USE_RESULT T GetFlag(const absl::Flag<T>& flag) {
   return flag.Get();
 }
-#else
 // Overload for `GetFlag()` for types that support lock-free reads.
 template <typename T,
           typename std::enable_if<
diff --git a/absl/flags/flag_benchmark.cc b/absl/flags/flag_benchmark.cc
new file mode 100644
index 000000000000..87f731704c87
--- /dev/null
+++ b/absl/flags/flag_benchmark.cc
@@ -0,0 +1,111 @@
+//
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/flags/flag.h"
+#include "absl/time/time.h"
+#include "absl/types/optional.h"
+#include "benchmark/benchmark.h"
+
+namespace {
+using String = std::string;
+using VectorOfStrings = std::vector<std::string>;
+using AbslDuration = absl::Duration;
+
+// We do not want to take over marshalling for the types absl::optional<int>,
+// absl::optional<std::string> which we do not own. Instead we introduce unique
+// "aliases" to these types, which we do.
+using AbslOptionalInt = absl::optional<int>;
+struct OptionalInt : AbslOptionalInt {
+  using AbslOptionalInt::AbslOptionalInt;
+};
+// Next two functions represent Abseil Flags marshalling for OptionalInt.
+bool AbslParseFlag(absl::string_view src, OptionalInt* flag,
+                   std::string* error) {
+  int val;
+  if (src.empty())
+    flag->reset();
+  else if (!absl::ParseFlag(src, &val, error))
+    return false;
+  *flag = val;
+  return true;
+}
+std::string AbslUnparseFlag(const OptionalInt& flag) {
+  return !flag ? "" : absl::UnparseFlag(*flag);
+}
+
+using AbslOptionalString = absl::optional<std::string>;
+struct OptionalString : AbslOptionalString {
+  using AbslOptionalString::AbslOptionalString;
+};
+// Next two functions represent Abseil Flags marshalling for OptionalString.
+bool AbslParseFlag(absl::string_view src, OptionalString* flag,
+                   std::string* error) {
+  std::string val;
+  if (src.empty())
+    flag->reset();
+  else if (!absl::ParseFlag(src, &val, error))
+    return false;
+  *flag = val;
+  return true;
+}
+std::string AbslUnparseFlag(const OptionalString& flag) {
+  return !flag ? "" : absl::UnparseFlag(*flag);
+}
+
+struct UDT {
+  UDT() = default;
+  UDT(const UDT&) {}
+  UDT& operator=(const UDT&) { return *this; }
+};
+// Next two functions represent Abseil Flags marshalling for UDT.
+bool AbslParseFlag(absl::string_view, UDT*, std::string*) { return true; }
+std::string AbslUnparseFlag(const UDT&) { return ""; }
+
+}  // namespace
+
+#define BENCHMARKED_TYPES(A) \
+  A(bool)                    \
+  A(int16_t)                 \
+  A(uint16_t)                \
+  A(int32_t)                 \
+  A(uint32_t)                \
+  A(int64_t)                 \
+  A(uint64_t)                \
+  A(double)                  \
+  A(float)                   \
+  A(String)                  \
+  A(VectorOfStrings)         \
+  A(OptionalInt)             \
+  A(OptionalString)          \
+  A(AbslDuration)            \
+  A(UDT)
+
+#define FLAG_DEF(T) ABSL_FLAG(T, T##_flag, {}, "");
+
+BENCHMARKED_TYPES(FLAG_DEF)
+
+namespace {
+
+#define BM_GetFlag(T)                                            \
+  void BM_GetFlag_##T(benchmark::State& state) {                 \
+    for (auto _ : state) {                                       \
+      benchmark::DoNotOptimize(absl::GetFlag(FLAGS_##T##_flag)); \
+    }                                                            \
+  }                                                              \
+  BENCHMARK(BM_GetFlag_##T);
+
+BENCHMARKED_TYPES(BM_GetFlag)
+
+}  // namespace