Skip to content

Commit

Permalink
Introduce Global() to replace NoDestructor.
Browse files Browse the repository at this point in the history
`Global()` is simpler to use, especially if construction requires more code than
specifying constructor arguments: it does not require to name a variable and to
remember about `static`.

PiperOrigin-RevId: 631019686
  • Loading branch information
QrczakMK committed May 6, 2024
1 parent 36b4e49 commit 3498555
Show file tree
Hide file tree
Showing 25 changed files with 332 additions and 196 deletions.
4 changes: 2 additions & 2 deletions python/riegeli/bytes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cc_library(
"//python/riegeli/base:utils",
"//riegeli/base:arithmetic",
"//riegeli/base:assert",
"//riegeli/base:no_destructor",
"//riegeli/base:global",
"//riegeli/base:object",
"//riegeli/base:types",
"//riegeli/bytes:buffer_options",
Expand All @@ -41,7 +41,7 @@ cc_library(
"//python/riegeli/base:utils",
"//riegeli/base:arithmetic",
"//riegeli/base:assert",
"//riegeli/base:no_destructor",
"//riegeli/base:global",
"//riegeli/base:object",
"//riegeli/base:types",
"//riegeli/bytes:buffer_options",
Expand Down
16 changes: 9 additions & 7 deletions python/riegeli/bytes/python_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "python/riegeli/base/utils.h"
#include "riegeli/base/arithmetic.h"
#include "riegeli/base/assert.h"
#include "riegeli/base/no_destructor.h"
#include "riegeli/base/global.h"
#include "riegeli/base/types.h"
#include "riegeli/bytes/buffered_reader.h"

Expand All @@ -52,9 +52,10 @@ PythonReader::PythonReader(PyObject* src, Options options)
if (options.assumed_pos() != absl::nullopt) {
set_limit_pos(*options.assumed_pos());
// `supports_random_access_` is left as `false`.
static const NoDestructor<absl::Status> status(absl::UnimplementedError(
"PythonReader::Options::assumed_pos() excludes random access"));
random_access_status_ = *status;
random_access_status_ = Global([] {
return absl::UnimplementedError(
"PythonReader::Options::assumed_pos() excludes random access");
});
} else {
static constexpr Identifier id_seekable("seekable");
const PythonPtr seekable_result(
Expand All @@ -71,9 +72,10 @@ PythonReader::PythonReader(PyObject* src, Options options)
if (seekable_is_true == 0) {
// Random access is not supported. Assume 0 as the initial position.
// `supports_random_access_` is left as `false`.
static const NoDestructor<absl::Status> status(absl::UnimplementedError(
"seekable() is False which excludes random access"));
random_access_status_ = *status;
random_access_status_ = Global([] {
return absl::UnimplementedError(
"seekable() is False which excludes random access");
});
return;
}
static constexpr Identifier id_tell("tell");
Expand Down
16 changes: 9 additions & 7 deletions python/riegeli/bytes/python_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "python/riegeli/base/utils.h"
#include "riegeli/base/arithmetic.h"
#include "riegeli/base/assert.h"
#include "riegeli/base/no_destructor.h"
#include "riegeli/base/global.h"
#include "riegeli/base/types.h"
#include "riegeli/bytes/buffered_writer.h"

Expand All @@ -52,9 +52,10 @@ PythonWriter::PythonWriter(PyObject* dest, Options options)
if (options.assumed_pos() != absl::nullopt) {
set_start_pos(*options.assumed_pos());
// `supports_random_access_` is left as `false`.
static const NoDestructor<absl::Status> status(absl::UnimplementedError(
"PythonWriter::Options::assumed_pos() excludes random access"));
random_access_status_ = *status;
random_access_status_ = Global([] {
return absl::UnimplementedError(
"PythonWriter::Options::assumed_pos() excludes random access");
});
} else {
static constexpr Identifier id_seekable("seekable");
const PythonPtr seekable_result(
Expand All @@ -71,9 +72,10 @@ PythonWriter::PythonWriter(PyObject* dest, Options options)
if (seekable_is_true == 0) {
// Random access is not supported. Assume 0 as the initial position.
// `supports_random_access_` is left as `false`.
static const NoDestructor<absl::Status> status(absl::UnimplementedError(
"seekable() is False which excludes random access"));
random_access_status_ = *status;
random_access_status_ = Global([] {
return absl::UnimplementedError(
"seekable() is False which excludes random access");
});
return;
}
static constexpr Identifier id_tell("tell");
Expand Down
15 changes: 8 additions & 7 deletions riegeli/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ cc_library(
)

cc_library(
name = "no_destructor",
hdrs = ["no_destructor.h"],
name = "global",
hdrs = ["global.h"],
deps = ["@com_google_absl//absl/meta:type_traits"],
)

cc_library(
Expand Down Expand Up @@ -127,7 +128,7 @@ cc_library(
hdrs = ["zeros.h"],
deps = [
":cord_utils",
":no_destructor",
":global",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
],
Expand Down Expand Up @@ -410,10 +411,10 @@ cc_library(
":buffering",
":compare",
":cord_utils",
":global",
":initializer",
":memory_estimator",
":new_aligned",
":no_destructor",
":shared_buffer",
":shared_ptr",
":sized_shared_buffer",
Expand Down Expand Up @@ -466,7 +467,7 @@ cc_library(
visibility = ["//riegeli:__subpackages__"],
deps = [
":assert",
":no_destructor",
":global",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/synchronization",
Expand All @@ -480,7 +481,7 @@ cc_library(
hdrs = ["background_cleaning.h"],
deps = [
":assert",
":no_destructor",
":global",
":parallelism",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/synchronization",
Expand All @@ -497,7 +498,7 @@ cc_library(
":assert",
":background_cleaning",
":compare",
":no_destructor",
":global",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:inlined_vector",
Expand Down
5 changes: 2 additions & 3 deletions riegeli/base/background_cleaning.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "riegeli/base/no_destructor.h"
#include "riegeli/base/global.h"

namespace riegeli {

Expand Down Expand Up @@ -83,8 +83,7 @@ class BackgroundCleaner {

// Returns a default global `BackgroundCleaner`.
static BackgroundCleaner& global() {
static NoDestructor<BackgroundCleaner> kStaticBackgroundCleaner;
return *kStaticBackgroundCleaner;
return Global<BackgroundCleaner>([] {});
}

// Registers the cleanee, allowing `ScheduleCleaning()` calls.
Expand Down
8 changes: 4 additions & 4 deletions riegeli/base/chain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
#include "riegeli/base/buffering.h"
#include "riegeli/base/compare.h"
#include "riegeli/base/cord_utils.h"
#include "riegeli/base/global.h"
#include "riegeli/base/intrusive_shared_ptr.h"
#include "riegeli/base/maker.h"
#include "riegeli/base/memory_estimator.h"
#include "riegeli/base/new_aligned.h"
#include "riegeli/base/no_destructor.h"
#include "riegeli/base/shared_buffer.h"
#include "riegeli/base/sized_shared_buffer.h"
#include "riegeli/base/string_utils.h"
Expand Down Expand Up @@ -2715,9 +2715,9 @@ Chain ChainOfZeros(size_t length) {
const absl::string_view kArrayOfZeros = ArrayOfZeros();
Chain result;
while (length >= kArrayOfZeros.size()) {
static const NoDestructor<Chain> kChainBlockOfZeros(
Chain::FromExternal(riegeli::Maker<ZeroRef>(), kArrayOfZeros));
result.Append(*kChainBlockOfZeros);
result.Append(Global([] {
return Chain::FromExternal(riegeli::Maker<ZeroRef>(), ArrayOfZeros());
}));
length -= kArrayOfZeros.size();
}
if (length > 0) {
Expand Down
Loading

0 comments on commit 3498555

Please sign in to comment.