Skip to content

Commit

Permalink
Replace C++20 branch predictor hints with C++17-compliant Abseil vers…
Browse files Browse the repository at this point in the history
…ions.

PiperOrigin-RevId: 733038863
  • Loading branch information
fniksic authored and copybara-github committed Mar 7, 2025
1 parent f8185ff commit dd9776b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
9 changes: 5 additions & 4 deletions centipede/runner_interceptors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <cstring>

#include "absl/base/nullability.h"
#include "absl/base/optimization.h"
#include "./centipede/runner.h"

using centipede::tls;
Expand Down Expand Up @@ -143,7 +144,7 @@ static NO_SANITIZE int memcmp_fallback(const void *s1, const void *s2,
extern "C" NO_SANITIZE int memcmp(const void *s1, const void *s2, size_t n) {
const int result =
memcmp_orig ? memcmp_orig(s1, s2, n) : memcmp_fallback(s1, s2, n);
if (!tls.started) [[unlikely]] {
if (ABSL_PREDICT_FALSE(!tls.started)) {
return result;
}
tls.TraceMemCmp(reinterpret_cast<uintptr_t>(__builtin_return_address(0)),
Expand All @@ -167,7 +168,7 @@ extern "C" NO_SANITIZE int strcmp(const char *s1, const char *s2) {
// Need to include one more byte than the shorter string length
// when falling back to memcmp e.g. "foo" < "foobar".
strcmp_orig ? strcmp_orig(s1, s2) : memcmp_fallback(s1, s2, len + 1);
if (!tls.started) [[unlikely]] {
if (ABSL_PREDICT_FALSE(!tls.started)) {
return result;
}
// Pass `len` here to avoid storing the trailing '\0' in the dictionary.
Expand All @@ -190,7 +191,7 @@ extern "C" NO_SANITIZE int strncmp(const char *s1, const char *s2, size_t n) {
if (n > len + 1) n = len + 1;
const int result =
strncmp_orig ? strncmp_orig(s1, s2, n) : memcmp_fallback(s1, s2, n);
if (!tls.started) [[unlikely]] {
if (ABSL_PREDICT_FALSE(!tls.started)) {
return result;
}
// Pass `len` here to avoid storing the trailing '\0' in the dictionary.
Expand All @@ -206,7 +207,7 @@ extern "C" int pthread_create(absl::Nonnull<pthread_t *> thread,
absl::Nullable<const pthread_attr_t *> attr,
void *(*start_routine)(void *),
absl::Nullable<void *> arg) {
if (!tls.started) [[unlikely]] {
if (ABSL_PREDICT_FALSE(!tls.started)) {
return REAL(pthread_create)(thread, attr, start_routine, arg);
}
// Wrap the arguments. Will be deleted in MyThreadStart.
Expand Down
1 change: 1 addition & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ cc_library(
":logging",
":remote_file",
":status_macros",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/base:nullability",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
Expand Down
1 change: 1 addition & 0 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fuzztest_cc_library(
fuzztest::remote_file
fuzztest::status_macros
absl::check
absl::core_headers
absl::log
absl::nullability
absl::status
Expand Down
11 changes: 5 additions & 6 deletions common/blob_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <vector>

#include "absl/base/nullability.h"
#include "absl/base/optimization.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "absl/status/status.h"
Expand Down Expand Up @@ -179,11 +180,11 @@ class DefaultBlobFileReader : public BlobFileReader {
ASSIGN_OR_RETURN_IF_NOT_OK(std::unique_ptr<riegeli::Reader> new_reader,
CreateRiegeliFileReader(path));
riegeli_reader_.Reset(std::move(new_reader));
if (riegeli_reader_.CheckFileFormat()) [[likely]] {
if (ABSL_PREDICT_TRUE(riegeli_reader_.CheckFileFormat())) {
// File could be opened and is in the Riegeli format.
return absl::OkStatus();
}
if (!riegeli_reader_.src()->ok()) [[unlikely]] {
if (ABSL_PREDICT_FALSE(!riegeli_reader_.src()->ok())) {
// File could not be opened.
return riegeli_reader_.src()->status();
}
Expand All @@ -210,8 +211,7 @@ class DefaultBlobFileReader : public BlobFileReader {
else
return absl::FailedPreconditionError("no reader open");
#else
if (legacy_reader_) [[unlikely]]
return legacy_reader_->Read(blob);
if (ABSL_PREDICT_FALSE(legacy_reader_)) return legacy_reader_->Read(blob);

absl::string_view record;
if (!riegeli_reader_.ReadRecord(record)) {
Expand All @@ -230,8 +230,7 @@ class DefaultBlobFileReader : public BlobFileReader {
legacy_reader_ = nullptr;
return absl::OkStatus();
#else
// NOLINTNEXTLINE(readability/braces). Similar to b/278586863.
if (legacy_reader_) [[unlikely]] {
if (ABSL_PREDICT_FALSE(legacy_reader_)) {
legacy_reader_ = nullptr;
return absl::OkStatus();
}
Expand Down

0 comments on commit dd9776b

Please sign in to comment.