Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: modernize likely/unlikely hints #55155

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .cpplint
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
set noparent
filter=-build/include_alpha,-build/include_subdir,-build/include_what_you_use,-legal/copyright,-readability/nolint
filter=-build/include_alpha,-build/include_subdir,-build/include_what_you_use,-legal/copyright,-readability/nolint,-readability/braces
linelength=80
2 changes: 1 addition & 1 deletion src/api/callback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ InternalCallbackScope::InternalCallbackScope(Environment* env,
// We first check `env->context() != current_context` because the contexts
// likely *are* the same, in which case we can skip the slightly more
// expensive Environment::GetCurrent() call.
if (UNLIKELY(env->context() != current_context)) {
if (env->context() != current_context) [[unlikely]] {
CHECK_EQ(Environment::GetCurrent(isolate), env);
}

Expand Down
6 changes: 4 additions & 2 deletions src/api/environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,17 @@ void* NodeArrayBufferAllocator::Allocate(size_t size) {
ret = allocator_->Allocate(size);
else
ret = allocator_->AllocateUninitialized(size);
if (LIKELY(ret != nullptr))
if (ret != nullptr) [[likely]] {
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
}
return ret;
}

void* NodeArrayBufferAllocator::AllocateUninitialized(size_t size) {
void* ret = allocator_->AllocateUninitialized(size);
if (LIKELY(ret != nullptr))
if (ret != nullptr) [[likely]] {
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
}
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion src/base_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ BaseObject::BaseObject(Realm* realm, Local<Object> object)
BaseObject::~BaseObject() {
realm()->UntrackBaseObject(this);

if (UNLIKELY(has_pointer_data())) {
if (has_pointer_data()) [[unlikely]] {
PointerData* metadata = pointer_data();
CHECK_EQ(metadata->strong_ptr_count, 0);
metadata->self = nullptr;
Expand Down
43 changes: 26 additions & 17 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -892,8 +892,9 @@ int SoaTraits::Send(QueryWrap<SoaTraits>* wrap, const char* name) {
int AnyTraits::Parse(
QueryAnyWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;
}

unsigned char* buf = response->buf.data;
int len = response->buf.size;
Expand Down Expand Up @@ -1059,8 +1060,9 @@ int AnyTraits::Parse(
int ATraits::Parse(
QueryAWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;
}

unsigned char* buf = response->buf.data;
int len = response->buf.size;
Expand Down Expand Up @@ -1093,8 +1095,9 @@ int ATraits::Parse(
int AaaaTraits::Parse(
QueryAaaaWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;
}

unsigned char* buf = response->buf.data;
int len = response->buf.size;
Expand Down Expand Up @@ -1127,8 +1130,9 @@ int AaaaTraits::Parse(
int CaaTraits::Parse(
QueryCaaWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;
}

unsigned char* buf = response->buf.data;
int len = response->buf.size;
Expand All @@ -1149,8 +1153,9 @@ int CaaTraits::Parse(
int CnameTraits::Parse(
QueryCnameWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;
}

unsigned char* buf = response->buf.data;
int len = response->buf.size;
Expand All @@ -1172,8 +1177,9 @@ int CnameTraits::Parse(
int MxTraits::Parse(
QueryMxWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;
}

unsigned char* buf = response->buf.data;
int len = response->buf.size;
Expand All @@ -1195,8 +1201,9 @@ int MxTraits::Parse(
int NsTraits::Parse(
QueryNsWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;
}

unsigned char* buf = response->buf.data;
int len = response->buf.size;
Expand All @@ -1218,8 +1225,9 @@ int NsTraits::Parse(
int TxtTraits::Parse(
QueryTxtWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;
}

unsigned char* buf = response->buf.data;
int len = response->buf.size;
Expand All @@ -1240,8 +1248,9 @@ int TxtTraits::Parse(
int SrvTraits::Parse(
QuerySrvWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;
}

unsigned char* buf = response->buf.data;
int len = response->buf.size;
Expand All @@ -1262,9 +1271,9 @@ int SrvTraits::Parse(
int PtrTraits::Parse(
QueryPtrWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;

}
unsigned char* buf = response->buf.data;
int len = response->buf.size;

Expand All @@ -1286,9 +1295,9 @@ int PtrTraits::Parse(
int NaptrTraits::Parse(
QueryNaptrWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;

}
unsigned char* buf = response->buf.data;
int len = response->buf.size;

Expand All @@ -1308,9 +1317,9 @@ int NaptrTraits::Parse(
int SoaTraits::Parse(
QuerySoaWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(response->is_host))
if (response->is_host) [[unlikely]] {
return ARES_EBADRESP;

}
unsigned char* buf = response->buf.data;
int len = response->buf.size;

Expand Down Expand Up @@ -1388,9 +1397,9 @@ int ReverseTraits::Send(GetHostByAddrWrap* wrap, const char* name) {
int ReverseTraits::Parse(
GetHostByAddrWrap* wrap,
const std::unique_ptr<ResponseData>& response) {
if (UNLIKELY(!response->is_host))
if (!response->is_host) [[unlikely]] {
return ARES_EBADRESP;

}
struct hostent* host = response->host.get();

Environment* env = wrap->env();
Expand Down
10 changes: 5 additions & 5 deletions src/compile_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ uint32_t GetCacheKey(std::string_view filename, CachedCodeType type) {
template <typename... Args>
inline void CompileCacheHandler::Debug(const char* format,
Args&&... args) const {
if (UNLIKELY(is_debug_)) {
if (is_debug_) [[unlikely]] {
FPrintF(stderr, format, std::forward<Args>(args)...);
}
}
Expand Down Expand Up @@ -458,20 +458,20 @@ CompileCacheEnableResult CompileCacheHandler::Enable(Environment* env,
cache_tag,
cache_dir_with_tag);

if (UNLIKELY(!env->permission()->is_granted(
if (!env->permission()->is_granted(
env,
permission::PermissionScope::kFileSystemWrite,
cache_dir_with_tag))) {
cache_dir_with_tag)) [[unlikely]] {
result.message = "Skipping compile cache because write permission for " +
cache_dir_with_tag + " is not granted";
result.status = CompileCacheEnableStatus::FAILED;
return result;
}

if (UNLIKELY(!env->permission()->is_granted(
if (!env->permission()->is_granted(
env,
permission::PermissionScope::kFileSystemRead,
cache_dir_with_tag))) {
cache_dir_with_tag)) [[unlikely]] {
result.message = "Skipping compile cache because read permission for " +
cache_dir_with_tag + " is not granted";
result.status = CompileCacheEnableStatus::FAILED;
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/crypto_aes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ bool ValidateIV(
Local<Value> value,
AESCipherConfig* params) {
ArrayBufferOrViewContents<char> iv(value);
if (UNLIKELY(!iv.CheckSizeInt32())) {
if (!iv.CheckSizeInt32()) [[unlikely]] {
THROW_ERR_OUT_OF_RANGE(env, "iv is too big");
return false;
}
Expand Down Expand Up @@ -377,7 +377,7 @@ bool ValidateAuthTag(
return false;
}
ArrayBufferOrViewContents<char> tag_contents(value);
if (UNLIKELY(!tag_contents.CheckSizeInt32())) {
if (!tag_contents.CheckSizeInt32()) [[unlikely]] {
THROW_ERR_OUT_OF_RANGE(env, "tagLength is too big");
return false;
}
Expand Down Expand Up @@ -412,7 +412,7 @@ bool ValidateAdditionalData(
// Additional Data
if (IsAnyBufferSource(value)) {
ArrayBufferOrViewContents<char> additional(value);
if (UNLIKELY(!additional.CheckSizeInt32())) {
if (!additional.CheckSizeInt32()) [[unlikely]] {
THROW_ERR_OUT_OF_RANGE(env, "additionalData is too big");
return false;
}
Expand Down
26 changes: 14 additions & 12 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,16 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
// raw bytes and proceed...
const ByteSource key_buf = ByteSource::FromSecretKeyBytes(env, args[1]);

if (UNLIKELY(key_buf.size() > INT_MAX))
if (key_buf.size() > INT_MAX) [[unlikely]] {
return THROW_ERR_OUT_OF_RANGE(env, "key is too big");
}

ArrayBufferOrViewContents<unsigned char> iv_buf(
!args[2]->IsNull() ? args[2] : Local<Value>());

if (UNLIKELY(!iv_buf.CheckSizeInt32()))
if (!iv_buf.CheckSizeInt32()) [[unlikely]] {
return THROW_ERR_OUT_OF_RANGE(env, "iv is too big");

}
// Don't assign to cipher->auth_tag_len_ directly; the value might not
// represent a valid length at this point.
unsigned int auth_tag_len;
Expand Down Expand Up @@ -672,9 +673,9 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
}

ArrayBufferOrViewContents<char> auth_tag(args[0]);
if (UNLIKELY(!auth_tag.CheckSizeInt32()))
if (!auth_tag.CheckSizeInt32()) [[unlikely]] {
return THROW_ERR_OUT_OF_RANGE(env, "buffer is too big");

}
unsigned int tag_len = auth_tag.size();

const int mode = EVP_CIPHER_CTX_mode(cipher->ctx_.get());
Expand Down Expand Up @@ -781,8 +782,9 @@ void CipherBase::SetAAD(const FunctionCallbackInfo<Value>& args) {
int plaintext_len = args[1].As<Int32>()->Value();
ArrayBufferOrViewContents<unsigned char> buf(args[0]);

if (UNLIKELY(!buf.CheckSizeInt32()))
if (!buf.CheckSizeInt32()) [[unlikely]] {
return THROW_ERR_OUT_OF_RANGE(env, "buffer is too big");
}
args.GetReturnValue().Set(cipher->SetAAD(buf, plaintext_len));
}

Expand Down Expand Up @@ -858,9 +860,9 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
std::unique_ptr<BackingStore> out;
Environment* env = Environment::GetCurrent(args);

if (UNLIKELY(size > INT_MAX))
if (size > INT_MAX) [[unlikely]] {
return THROW_ERR_OUT_OF_RANGE(env, "data is too long");

}
UpdateResult r = cipher->Update(data, size, &out);

if (r != kSuccess) {
Expand Down Expand Up @@ -1063,9 +1065,9 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
return;

ArrayBufferOrViewContents<unsigned char> buf(args[offset]);
if (UNLIKELY(!buf.CheckSizeInt32()))
if (!buf.CheckSizeInt32()) [[unlikely]] {
return THROW_ERR_OUT_OF_RANGE(env, "buffer is too long");

}
uint32_t padding;
if (!args[offset + 1]->Uint32Value(env->context()).To(&padding)) return;

Expand Down Expand Up @@ -1105,9 +1107,9 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {

ArrayBufferOrViewContents<unsigned char> oaep_label(
!args[offset + 3]->IsUndefined() ? args[offset + 3] : Local<Value>());
if (UNLIKELY(!oaep_label.CheckSizeInt32()))
if (!oaep_label.CheckSizeInt32()) [[unlikely]] {
return THROW_ERR_OUT_OF_RANGE(env, "oaepLabel is too big");

}
std::unique_ptr<BackingStore> out;
if (!Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
env, pkey, padding, digest, oaep_label, buf, &out)) {
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ void SecureContext::SetEngineKey(const FunctionCallbackInfo<Value>& args) {

CHECK_EQ(args.Length(), 2);

if (UNLIKELY(env->permission()->enabled())) {
if (env->permission()->enabled()) [[unlikely]] {
return THROW_ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED(
env,
"Programmatic selection of OpenSSL engines is unsupported while the "
Expand Down Expand Up @@ -1198,7 +1198,7 @@ void SecureContext::SetClientCertEngine(
// support multiple calls to SetClientCertEngine.
CHECK(!sc->client_cert_engine_provided_);

if (UNLIKELY(env->permission()->enabled())) {
if (env->permission()->enabled()) [[unlikely]] {
return THROW_ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED(
env,
"Programmatic selection of OpenSSL engines is unsupported while the "
Expand Down
Loading
Loading