Skip to content

Commit

Permalink
Use ABSL_PREDICT_TRUE|FALSE instead of PROTOBUF_PREDICT_TRUE|FALSE.
Browse files Browse the repository at this point in the history
Considering that protobuf depends on absl already, we don't need protobuf's
version of PREDICT_TRUE|FALSE. This CL shrinks port_def.inc.

PiperOrigin-RevId: 694015588
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 7, 2024
1 parent b66792e commit fd47730
Show file tree
Hide file tree
Showing 51 changed files with 372 additions and 377 deletions.
17 changes: 0 additions & 17 deletions csharp/src/Google.Protobuf/Reflection/FeatureSetDescriptor.g.cs

This file was deleted.

8 changes: 4 additions & 4 deletions python/google/protobuf/pyext/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ void OutOfRangeError(PyObject* arg) {

template <class RangeType, class ValueType>
bool VerifyIntegerCastAndRange(PyObject* arg, ValueType value) {
if (PROTOBUF_PREDICT_FALSE(value == -1 && PyErr_Occurred())) {
if (ABSL_PREDICT_FALSE(value == -1 && PyErr_Occurred())) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
// Replace it with the same ValueError as pure python protos instead of
// the default one.
Expand All @@ -500,7 +500,7 @@ bool VerifyIntegerCastAndRange(PyObject* arg, ValueType value) {
} // Otherwise propagate existing error.
return false;
}
if (PROTOBUF_PREDICT_FALSE(!IsValidNumericCast<RangeType>(value))) {
if (ABSL_PREDICT_FALSE(!IsValidNumericCast<RangeType>(value))) {
OutOfRangeError(arg);
return false;
}
Expand All @@ -514,7 +514,7 @@ bool CheckAndGetInteger(PyObject* arg, T* value) {
// This definition includes everything with a valid __index__() implementation
// and shouldn't cast the net too wide.
if (!strcmp(Py_TYPE(arg)->tp_name, "numpy.ndarray") ||
PROTOBUF_PREDICT_FALSE(!PyIndex_Check(arg))) {
ABSL_PREDICT_FALSE(!PyIndex_Check(arg))) {
FormatTypeError(arg, "int");
return false;
}
Expand Down Expand Up @@ -558,7 +558,7 @@ template bool CheckAndGetInteger<uint64_t>(PyObject*, uint64_t*);
bool CheckAndGetDouble(PyObject* arg, double* value) {
*value = PyFloat_AsDouble(arg);
if (!strcmp(Py_TYPE(arg)->tp_name, "numpy.ndarray") ||
PROTOBUF_PREDICT_FALSE(*value == -1 && PyErr_Occurred())) {
ABSL_PREDICT_FALSE(*value == -1 && PyErr_Occurred())) {
FormatTypeError(arg, "int, float");
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion src/google/protobuf/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,10 @@ cc_library(
"//:__subpackages__",
"//src/google/protobuf:__subpackages__",
],
deps = [":port"],
deps = [
":port",
"@com_google_absl//absl/base:core_headers",
],
)

cc_test(
Expand Down
7 changes: 4 additions & 3 deletions src/google/protobuf/arena.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "absl/base/attributes.h"
#include "absl/base/dynamic_annotations.h"
#include "absl/base/optimization.h"
#include "absl/base/prefetch.h"
#include "absl/container/internal/layout.h"
#include "absl/log/absl_check.h"
Expand Down Expand Up @@ -643,7 +644,7 @@ uint64_t ThreadSafeArena::GetNextLifeCycleId() {
ThreadCache& tc = thread_cache();
uint64_t id = tc.next_lifecycle_id;
constexpr uint64_t kInc = ThreadCache::kPerThreadIds;
if (PROTOBUF_PREDICT_FALSE((id & (kInc - 1)) == 0)) {
if (ABSL_PREDICT_FALSE((id & (kInc - 1)) == 0)) {
// On platforms that don't support uint64_t atomics we can certainly not
// afford to increment by large intervals and expect uniqueness due to
// wrapping, hence we only add by 1.
Expand Down Expand Up @@ -815,7 +816,7 @@ uint64_t ThreadSafeArena::Reset() {
void* ThreadSafeArena::AllocateAlignedWithCleanup(size_t n, size_t align,
void (*destructor)(void*)) {
SerialArena* arena;
if (PROTOBUF_PREDICT_TRUE(GetSerialArenaFast(&arena))) {
if (ABSL_PREDICT_TRUE(GetSerialArenaFast(&arena))) {
return arena->AllocateAlignedWithCleanup(n, align, destructor);
} else {
return AllocateAlignedWithCleanupFallback(n, align, destructor);
Expand All @@ -828,7 +829,7 @@ void ThreadSafeArena::AddCleanup(void* elem, void (*cleanup)(void*)) {

SerialArena* ThreadSafeArena::GetSerialArena() {
SerialArena* arena;
if (PROTOBUF_PREDICT_FALSE(!GetSerialArenaFast(&arena))) {
if (ABSL_PREDICT_FALSE(!GetSerialArenaFast(&arena))) {
arena = GetSerialArenaFallback(kMaxCleanupNodeSize);
}
return arena;
Expand Down
10 changes: 5 additions & 5 deletions src/google/protobuf/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
},
// Non arena-constructable
[arena](auto&&... args) {
if (PROTOBUF_PREDICT_FALSE(arena == nullptr)) {
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
return new T(std::forward<Args>(args)...);
}
return new (arena->AllocateInternal<T>())
Expand Down Expand Up @@ -277,7 +277,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
"CreateArray requires a trivially destructible type");
ABSL_CHECK_LE(num_elements, std::numeric_limits<size_t>::max() / sizeof(T))
<< "Requested size is too large to fit into size_t.";
if (PROTOBUF_PREDICT_FALSE(arena == nullptr)) {
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
return new T[num_elements];
} else {
// We count on compiler to realize that if sizeof(T) is a multiple of
Expand Down Expand Up @@ -518,7 +518,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
Args&&... args) {
static_assert(is_arena_constructable<T>::value,
"Can only construct types that are ArenaConstructable");
if (PROTOBUF_PREDICT_FALSE(arena == nullptr)) {
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
return new T(nullptr, static_cast<Args&&>(args)...);
} else {
return arena->DoCreateMessage<T>(static_cast<Args&&>(args)...);
Expand All @@ -532,7 +532,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
PROTOBUF_NDEBUG_INLINE static T* CreateArenaCompatible(Arena* arena) {
static_assert(is_arena_constructable<T>::value,
"Can only construct types that are ArenaConstructable");
if (PROTOBUF_PREDICT_FALSE(arena == nullptr)) {
if (ABSL_PREDICT_FALSE(arena == nullptr)) {
// Generated arena constructor T(Arena*) is protected. Call via
// InternalHelper.
return InternalHelper<T>::New();
Expand Down Expand Up @@ -583,7 +583,7 @@ class PROTOBUF_EXPORT PROTOBUF_ALIGNAS(8) Arena final {
static void CreateInArenaStorage(T* ptr, Arena* arena, Args&&... args) {
CreateInArenaStorageInternal(ptr, arena, is_arena_constructable<T>(),
std::forward<Args>(args)...);
if (PROTOBUF_PREDICT_TRUE(arena != nullptr)) {
if (ABSL_PREDICT_TRUE(arena != nullptr)) {
RegisterDestructorInternal(ptr, arena, is_destructor_skippable<T>());
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/google/protobuf/arena_cleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <vector>

#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "absl/base/prefetch.h"

// Must be included last.
Expand Down Expand Up @@ -60,7 +61,7 @@ class ChunkList {
public:
PROTOBUF_ALWAYS_INLINE void Add(void* elem, void (*destructor)(void*),
SerialArena& arena) {
if (PROTOBUF_PREDICT_TRUE(next_ < limit_)) {
if (ABSL_PREDICT_TRUE(next_ < limit_)) {
AddFromExisting(elem, destructor);
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/google/protobuf/arenastring.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <cstddef>

#include "absl/base/optimization.h"
#include "absl/log/absl_check.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/arenastring.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PROTOBUF_EXPORT LazyString {
const std::string& get() const {
// This check generates less code than a call-once invocation.
auto* res = inited_.load(std::memory_order_acquire);
if (PROTOBUF_PREDICT_FALSE(res == nullptr)) return Init();
if (ABSL_PREDICT_FALSE(res == nullptr)) return Init();
return *res;
}

Expand Down
4 changes: 3 additions & 1 deletion src/google/protobuf/arenaz_sampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <limits>
#include <utility>

#include "absl/base/optimization.h"


// Must be included last.
#include "google/protobuf/port_def.inc"
Expand Down Expand Up @@ -129,7 +131,7 @@ ThreadSafeArenaStats* SampleSlow(SamplingState& sampling_state) {
// We will only be negative on our first count, so we should just retry in
// that case.
if (first) {
if (PROTOBUF_PREDICT_TRUE(--sampling_state.next_sample > 0)) return nullptr;
if (ABSL_PREDICT_TRUE(--sampling_state.next_sample > 0)) return nullptr;
return SampleSlow(sampling_state);
}

Expand Down
8 changes: 4 additions & 4 deletions src/google/protobuf/arenaz_sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ struct ThreadSafeArenaStats
void* stack[kMaxStackDepth];
static void RecordAllocateStats(ThreadSafeArenaStats* info, size_t used,
size_t allocated, size_t wasted) {
if (PROTOBUF_PREDICT_TRUE(info == nullptr)) return;
if (ABSL_PREDICT_TRUE(info == nullptr)) return;
RecordAllocateSlow(info, used, allocated, wasted);
}

Expand Down Expand Up @@ -117,7 +117,7 @@ class ThreadSafeArenaStatsHandle {
: info_(info) {}

~ThreadSafeArenaStatsHandle() {
if (PROTOBUF_PREDICT_TRUE(info_ == nullptr)) return;
if (ABSL_PREDICT_TRUE(info_ == nullptr)) return;
UnsampleSlow(info_);
}

Expand All @@ -126,7 +126,7 @@ class ThreadSafeArenaStatsHandle {

ThreadSafeArenaStatsHandle& operator=(
ThreadSafeArenaStatsHandle&& other) noexcept {
if (PROTOBUF_PREDICT_FALSE(info_ != nullptr)) {
if (ABSL_PREDICT_FALSE(info_ != nullptr)) {
UnsampleSlow(info_);
}
info_ = std::exchange(other.info_, nullptr);
Expand Down Expand Up @@ -154,7 +154,7 @@ extern PROTOBUF_THREAD_LOCAL SamplingState global_sampling_state;
// Returns an RAII sampling handle that manages registration and unregistation
// with the global sampler.
inline ThreadSafeArenaStatsHandle Sample() {
if (PROTOBUF_PREDICT_TRUE(--global_sampling_state.next_sample > 0)) {
if (ABSL_PREDICT_TRUE(--global_sampling_state.next_sample > 0)) {
return ThreadSafeArenaStatsHandle(nullptr);
}
return ThreadSafeArenaStatsHandle(SampleSlow(global_sampling_state));
Expand Down
21 changes: 10 additions & 11 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ bool MaybeEmitHaswordsCheck(ChunkIterator it, ChunkIterator end,
}
}}},
R"cc(
if (PROTOBUF_PREDICT_FALSE($cond$)) {
if (ABSL_PREDICT_FALSE($cond$)) {
)cc");
p->Indent();
return true;
Expand Down Expand Up @@ -1238,7 +1238,7 @@ void MessageGenerator::GenerateFieldClear(const FieldDescriptor* field,
// TODO: figure out if early return breaks tracking
if (ShouldSplit(field, options_)) {
p->Emit(R"cc(
if (PROTOBUF_PREDICT_TRUE(IsSplitMessageDefault()))
if (ABSL_PREDICT_TRUE(IsSplitMessageDefault()))
return;
)cc");
}
Expand Down Expand Up @@ -2518,7 +2518,7 @@ void MessageGenerator::GenerateClassMethods(io::Printer* p) {
DefaultInstanceName(descriptor_, options_, /*split=*/false)}},
R"cc(
void $classname$::PrepareSplitMessageForWrite() {
if (PROTOBUF_PREDICT_TRUE(IsSplitMessageDefault())) {
if (ABSL_PREDICT_TRUE(IsSplitMessageDefault())) {
void* chunk = $pbi$::CreateSplitMessageGeneric(
GetArena(), &$split_default$, sizeof(Impl_::Split), this,
&$default$);
Expand Down Expand Up @@ -2964,7 +2964,7 @@ void MessageGenerator::GenerateSharedDestructorCode(io::Printer* p) {
[&] { emit_field_dtors(/* split_fields= */ true); }},
},
R"cc(
if (PROTOBUF_PREDICT_FALSE(!this_.IsSplitMessageDefault())) {
if (ABSL_PREDICT_FALSE(!this_.IsSplitMessageDefault())) {
auto* $cached_split_ptr$ = this_.$split$;
$split_field_dtors_impl$;
delete $cached_split_ptr$;
Expand Down Expand Up @@ -3056,8 +3056,7 @@ void MessageGenerator::GenerateArenaDestructorCode(io::Printer* p) {
[&] { emit_field_dtors(/* split_fields= */ true); }},
},
R"cc(
if (PROTOBUF_PREDICT_FALSE(
!_this->IsSplitMessageDefault())) {
if (ABSL_PREDICT_FALSE(!_this->IsSplitMessageDefault())) {
$split_field_dtors_impl$;
}
)cc");
Expand Down Expand Up @@ -3311,7 +3310,7 @@ void MessageGenerator::GenerateCopyInitFields(io::Printer* p) const {
if (ShouldSplit(descriptor_, options_)) {
p->Emit({{"copy_split_fields", generate_copy_split_fields}},
R"cc(
if (PROTOBUF_PREDICT_FALSE(!from.IsSplitMessageDefault())) {
if (ABSL_PREDICT_FALSE(!from.IsSplitMessageDefault())) {
PrepareSplitMessageForWrite();
$copy_split_fields$;
}
Expand Down Expand Up @@ -4252,7 +4251,7 @@ void MessageGenerator::GenerateClassSpecificMergeImpl(io::Printer* p) {

if (ShouldSplit(descriptor_, options_)) {
format(
"if (PROTOBUF_PREDICT_FALSE(!from.IsSplitMessageDefault())) {\n"
"if (ABSL_PREDICT_FALSE(!from.IsSplitMessageDefault())) {\n"
" _this->PrepareSplitMessageForWrite();\n"
"}\n");
}
Expand Down Expand Up @@ -4913,7 +4912,7 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBody(io::Printer* p) {
(void)cached_has_bits;

$handle_lazy_fields$;
if (PROTOBUF_PREDICT_FALSE(this_.$have_unknown_fields$)) {
if (ABSL_PREDICT_FALSE(this_.$have_unknown_fields$)) {
$handle_unknown_fields$;
}
)cc");
Expand Down Expand Up @@ -5008,7 +5007,7 @@ void MessageGenerator::GenerateSerializeWithCachedSizesBodyShuffled(
}
}
}
if (PROTOBUF_PREDICT_FALSE(this_.$have_unknown_fields$)) {
if (ABSL_PREDICT_FALSE(this_.$have_unknown_fields$)) {
$handle_unknown_fields$;
}
)cc");
Expand Down Expand Up @@ -5252,7 +5251,7 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
// even relaxed memory order might have perf impact to replace it
// with ordinary loads and stores.
p->Emit(R"cc(
if (PROTOBUF_PREDICT_FALSE(this_.$have_unknown_fields$)) {
if (ABSL_PREDICT_FALSE(this_.$have_unknown_fields$)) {
total_size += this_.$unknown_fields$.size();
}
this_.$cached_size$.Set(::_pbi::ToCachedSize(total_size));
Expand Down
2 changes: 1 addition & 1 deletion src/google/protobuf/compiler/java/java_features.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/google/protobuf/compiler/plugin.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/google/protobuf/cpp_features.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fd47730

Please sign in to comment.