summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--absl/base/BUILD.bazel1
-rw-r--r--absl/base/internal/spinlock_benchmark.cc9
-rw-r--r--absl/container/BUILD.bazel4
-rw-r--r--absl/container/CMakeLists.txt2
-rw-r--r--absl/container/internal/container_memory_test.cc3
-rw-r--r--absl/container/internal/hash_generator_testing.cc12
-rw-r--r--absl/container/internal/raw_hash_set_probe_benchmark.cc16
-rw-r--r--absl/synchronization/BUILD.bazel2
-rw-r--r--absl/synchronization/blocking_counter_benchmark.cc5
-rw-r--r--absl/synchronization/mutex_benchmark.cc15
10 files changed, 44 insertions, 25 deletions
diff --git a/absl/base/BUILD.bazel b/absl/base/BUILD.bazel
index 5491232d..0eb735da 100644
--- a/absl/base/BUILD.bazel
+++ b/absl/base/BUILD.bazel
@@ -517,6 +517,7 @@ cc_library(
deps = [
":base",
":base_internal",
+ ":no_destructor",
":raw_logging_internal",
"//absl/synchronization",
"@com_github_google_benchmark//:benchmark_main",
diff --git a/absl/base/internal/spinlock_benchmark.cc b/absl/base/internal/spinlock_benchmark.cc
index 7135d3f5..1790d967 100644
--- a/absl/base/internal/spinlock_benchmark.cc
+++ b/absl/base/internal/spinlock_benchmark.cc
@@ -18,6 +18,7 @@
#include "absl/base/internal/raw_logging.h"
#include "absl/base/internal/scheduling_mode.h"
#include "absl/base/internal/spinlock.h"
+#include "absl/base/no_destructor.h"
#include "absl/synchronization/internal/create_thread_identity.h"
#include "benchmark/benchmark.h"
@@ -31,7 +32,8 @@ static void BM_TryLock(benchmark::State& state) {
nullptr,
"GetOrCreateCurrentThreadIdentity() failed");
- static auto* spinlock = new absl::base_internal::SpinLock(scheduling_mode);
+ static absl::NoDestructor<absl::base_internal::SpinLock> spinlock(
+ scheduling_mode);
for (auto _ : state) {
if (spinlock->TryLock()) spinlock->Unlock();
}
@@ -45,9 +47,10 @@ static void BM_SpinLock(benchmark::State& state) {
nullptr,
"GetOrCreateCurrentThreadIdentity() failed");
- static auto* spinlock = new absl::base_internal::SpinLock(scheduling_mode);
+ static absl::NoDestructor<absl::base_internal::SpinLock> spinlock(
+ scheduling_mode);
for (auto _ : state) {
- absl::base_internal::SpinLockHolder holder(spinlock);
+ absl::base_internal::SpinLockHolder holder(spinlock.get());
}
}
diff --git a/absl/container/BUILD.bazel b/absl/container/BUILD.bazel
index d2910ed8..0ba2fa76 100644
--- a/absl/container/BUILD.bazel
+++ b/absl/container/BUILD.bazel
@@ -403,6 +403,7 @@ cc_test(
deps = [
":container_memory",
":test_instance_tracker",
+ "//absl/base:no_destructor",
"//absl/meta:type_traits",
"//absl/strings",
"@com_google_googletest//:gtest",
@@ -453,6 +454,7 @@ cc_library(
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
":hash_policy_testing",
+ "//absl/base:no_destructor",
"//absl/memory",
"//absl/meta:type_traits",
"//absl/strings",
@@ -732,10 +734,12 @@ cc_binary(
":hash_function_defaults",
":hashtable_debug",
":raw_hash_set",
+ "//absl/base:no_destructor",
"//absl/random",
"//absl/random:distributions",
"//absl/strings",
"//absl/strings:str_format",
+ "//absl/types:optional",
],
)
diff --git a/absl/container/CMakeLists.txt b/absl/container/CMakeLists.txt
index 60afdb2b..128cc0e9 100644
--- a/absl/container/CMakeLists.txt
+++ b/absl/container/CMakeLists.txt
@@ -450,6 +450,7 @@ absl_cc_test(
${ABSL_TEST_COPTS}
DEPS
absl::container_memory
+ absl::no_destructor
absl::strings
absl::test_instance_tracker
absl::type_traits
@@ -503,6 +504,7 @@ absl_cc_library(
absl::hash_policy_testing
absl::memory
absl::meta
+ absl::no_destructor
absl::strings
TESTONLY
)
diff --git a/absl/container/internal/container_memory_test.cc b/absl/container/internal/container_memory_test.cc
index c1e57834..90d64bf5 100644
--- a/absl/container/internal/container_memory_test.cc
+++ b/absl/container/internal/container_memory_test.cc
@@ -25,6 +25,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
+#include "absl/base/no_destructor.h"
#include "absl/container/internal/test_instance_tracker.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/string_view.h"
@@ -58,7 +59,7 @@ TEST(Memory, AlignmentSmallerThanBase) {
}
std::map<std::type_index, int>& AllocationMap() {
- static auto* map = new std::map<std::type_index, int>;
+ static absl::NoDestructor<std::map<std::type_index, int>> map;
return *map;
}
diff --git a/absl/container/internal/hash_generator_testing.cc b/absl/container/internal/hash_generator_testing.cc
index 59cc5aac..e89dfdb5 100644
--- a/absl/container/internal/hash_generator_testing.cc
+++ b/absl/container/internal/hash_generator_testing.cc
@@ -16,6 +16,8 @@
#include <deque>
+#include "absl/base/no_destructor.h"
+
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace container_internal {
@@ -41,11 +43,11 @@ class RandomDeviceSeedSeq {
} // namespace
std::mt19937_64* GetSharedRng() {
- static auto* rng = [] {
+ static absl::NoDestructor<std::mt19937_64> rng([] {
RandomDeviceSeedSeq seed_seq;
- return new std::mt19937_64(seed_seq);
- }();
- return rng;
+ return std::mt19937_64(seed_seq);
+ }());
+ return rng.get();
}
std::string Generator<std::string>::operator()() const {
@@ -59,7 +61,7 @@ std::string Generator<std::string>::operator()() const {
}
absl::string_view Generator<absl::string_view>::operator()() const {
- static auto* arena = new std::deque<std::string>();
+ static absl::NoDestructor<std::deque<std::string>> arena;
// NOLINTNEXTLINE(runtime/int)
std::uniform_int_distribution<short> chars(0x20, 0x7E);
arena->emplace_back();
diff --git a/absl/container/internal/raw_hash_set_probe_benchmark.cc b/absl/container/internal/raw_hash_set_probe_benchmark.cc
index 7169a2e2..5d4184b2 100644
--- a/absl/container/internal/raw_hash_set_probe_benchmark.cc
+++ b/absl/container/internal/raw_hash_set_probe_benchmark.cc
@@ -19,6 +19,7 @@
#include <regex> // NOLINT
#include <vector>
+#include "absl/base/no_destructor.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/internal/hash_function_defaults.h"
#include "absl/container/internal/hashtable_debug.h"
@@ -29,6 +30,7 @@
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
+#include "absl/types/optional.h"
namespace {
@@ -71,7 +73,7 @@ struct Policy {
};
absl::BitGen& GlobalBitGen() {
- static auto* value = new absl::BitGen;
+ static absl::NoDestructor<absl::BitGen> value;
return *value;
}
@@ -112,7 +114,7 @@ class RandomizedAllocator {
static constexpr size_t kRandomPool = 20;
static std::vector<T*>& GetPointers(size_t n) {
- static auto* m = new absl::flat_hash_map<size_t, std::vector<T*>>();
+ static absl::NoDestructor<absl::flat_hash_map<size_t, std::vector<T*>>> m;
return (*m)[n];
}
};
@@ -460,12 +462,12 @@ constexpr int kNameWidth = 15;
constexpr int kDistWidth = 16;
bool CanRunBenchmark(absl::string_view name) {
- static std::regex* const filter = []() -> std::regex* {
+ static const absl::NoDestructor<absl::optional<std::regex>> filter([] {
return benchmarks.empty() || benchmarks == "all"
- ? nullptr
- : new std::regex(std::string(benchmarks));
- }();
- return filter == nullptr || std::regex_search(std::string(name), *filter);
+ ? absl::nullopt
+ : absl::make_optional(std::regex(std::string(benchmarks)));
+ }());
+ return !filter->has_value() || std::regex_search(std::string(name), **filter);
}
struct Result {
diff --git a/absl/synchronization/BUILD.bazel b/absl/synchronization/BUILD.bazel
index 0550b61c..de06ebdd 100644
--- a/absl/synchronization/BUILD.bazel
+++ b/absl/synchronization/BUILD.bazel
@@ -191,6 +191,7 @@ cc_binary(
deps = [
":synchronization",
":thread_pool",
+ "//absl/base:no_destructor",
"@com_github_google_benchmark//:benchmark_main",
],
)
@@ -291,6 +292,7 @@ cc_library(
":thread_pool",
"//absl/base",
"//absl/base:config",
+ "//absl/base:no_destructor",
"@com_github_google_benchmark//:benchmark_main",
],
alwayslink = 1,
diff --git a/absl/synchronization/blocking_counter_benchmark.cc b/absl/synchronization/blocking_counter_benchmark.cc
index b504d1a5..ea2bf9f9 100644
--- a/absl/synchronization/blocking_counter_benchmark.cc
+++ b/absl/synchronization/blocking_counter_benchmark.cc
@@ -14,6 +14,7 @@
#include <limits>
+#include "absl/base/no_destructor.h"
#include "absl/synchronization/blocking_counter.h"
#include "absl/synchronization/internal/thread_pool.h"
#include "benchmark/benchmark.h"
@@ -39,8 +40,8 @@ BENCHMARK(BM_BlockingCounter_SingleThread)
->Arg(256);
void BM_BlockingCounter_DecrementCount(benchmark::State& state) {
- static absl::BlockingCounter* counter =
- new absl::BlockingCounter{std::numeric_limits<int>::max()};
+ static absl::NoDestructor<absl::BlockingCounter> counter(
+ std::numeric_limits<int>::max());
for (auto _ : state) {
counter->DecrementCount();
}
diff --git a/absl/synchronization/mutex_benchmark.cc b/absl/synchronization/mutex_benchmark.cc
index c3f54764..06888dfe 100644
--- a/absl/synchronization/mutex_benchmark.cc
+++ b/absl/synchronization/mutex_benchmark.cc
@@ -19,6 +19,7 @@
#include "absl/base/config.h"
#include "absl/base/internal/cycleclock.h"
#include "absl/base/internal/spinlock.h"
+#include "absl/base/no_destructor.h"
#include "absl/synchronization/blocking_counter.h"
#include "absl/synchronization/internal/thread_pool.h"
#include "absl/synchronization/mutex.h"
@@ -27,17 +28,17 @@
namespace {
void BM_Mutex(benchmark::State& state) {
- static absl::Mutex* mu = new absl::Mutex;
+ static absl::NoDestructor<absl::Mutex> mu;
for (auto _ : state) {
- absl::MutexLock lock(mu);
+ absl::MutexLock lock(mu.get());
}
}
BENCHMARK(BM_Mutex)->UseRealTime()->Threads(1)->ThreadPerCpu();
void BM_ReaderLock(benchmark::State& state) {
- static absl::Mutex* mu = new absl::Mutex;
+ static absl::NoDestructor<absl::Mutex> mu;
for (auto _ : state) {
- absl::ReaderMutexLock lock(mu);
+ absl::ReaderMutexLock lock(mu.get());
}
}
BENCHMARK(BM_ReaderLock)->UseRealTime()->Threads(1)->ThreadPerCpu();
@@ -53,7 +54,7 @@ void BM_TryLock(benchmark::State& state) {
BENCHMARK(BM_TryLock);
void BM_ReaderTryLock(benchmark::State& state) {
- static absl::Mutex* mu = new absl::Mutex;
+ static absl::NoDestructor<absl::Mutex> mu;
for (auto _ : state) {
if (mu->ReaderTryLock()) {
mu->ReaderUnlock();
@@ -133,7 +134,7 @@ void BM_MutexEnqueue(benchmark::State& state) {
std::atomic<int> blocked_threads{0};
std::atomic<bool> thread_has_mutex{false};
};
- static Shared* shared = new Shared;
+ static absl::NoDestructor<Shared> shared;
// Set up 'blocked_threads' to count how many threads are currently blocked
// in Abseil synchronization code.
@@ -211,7 +212,7 @@ void BM_Contended(benchmark::State& state) {
MutexType mu;
int data = 0;
};
- static auto* shared = new Shared;
+ static absl::NoDestructor<Shared> shared;
int local = 0;
for (auto _ : state) {
// Here we model both local work outside of the critical section as well as