Skip to content

Commit

Permalink
src: modernize likely/unlikely hints
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 28, 2024
1 parent e6427e1 commit 97535a9
Show file tree
Hide file tree
Showing 52 changed files with 286 additions and 218 deletions.
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
12 changes: 6 additions & 6 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void New(const FunctionCallbackInfo<Value>& args) {
// or an ArrayBuffer or ArrayBufferView with the generator.

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

BignumPointer bn_p(reinterpret_cast<uint8_t*>(arg0.data()), arg0.size());
Expand All @@ -142,7 +142,7 @@ void New(const FunctionCallbackInfo<Value>& args) {
}
} else {
ArrayBufferOrViewContents<char> arg1(args[1]);
if (UNLIKELY(!arg1.CheckSizeInt32()))
if (!arg1.CheckSizeInt32()) [[unlikely]]
return THROW_ERR_OUT_OF_RANGE(env, "generator is too big");
bn_g = BignumPointer(reinterpret_cast<uint8_t*>(arg1.data()), arg1.size());
if (!bn_g) {
Expand Down Expand Up @@ -253,7 +253,7 @@ void ComputeSecret(const FunctionCallbackInfo<Value>& args) {

CHECK_EQ(args.Length(), 1);
ArrayBufferOrViewContents<unsigned char> key_buf(args[0]);
if (UNLIKELY(!key_buf.CheckSizeInt32()))
if (!key_buf.CheckSizeInt32()) [[unlikely]]
return THROW_ERR_OUT_OF_RANGE(env, "secret is too big");
BignumPointer key(key_buf.data(), key_buf.size());

Expand Down Expand Up @@ -286,7 +286,7 @@ void SetPublicKey(const FunctionCallbackInfo<Value>& args) {
DHPointer& dh = *diffieHellman;
CHECK_EQ(args.Length(), 1);
ArrayBufferOrViewContents<unsigned char> buf(args[0]);
if (UNLIKELY(!buf.CheckSizeInt32()))
if (!buf.CheckSizeInt32()) [[unlikely]]
return THROW_ERR_OUT_OF_RANGE(env, "buf is too big");
BignumPointer num(buf.data(), buf.size());
CHECK(num);
Expand All @@ -300,7 +300,7 @@ void SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
DHPointer& dh = *diffieHellman;
CHECK_EQ(args.Length(), 1);
ArrayBufferOrViewContents<unsigned char> buf(args[0]);
if (UNLIKELY(!buf.CheckSizeInt32()))
if (!buf.CheckSizeInt32()) [[unlikely]]
return THROW_ERR_OUT_OF_RANGE(env, "buf is too big");
BignumPointer num(buf.data(), buf.size());
CHECK(num);
Expand Down Expand Up @@ -368,7 +368,7 @@ Maybe<void> DhKeyGenTraits::AdditionalConfig(
params->params.prime = size;
} else {
ArrayBufferOrViewContents<unsigned char> input(args[*offset]);
if (UNLIKELY(!input.CheckSizeInt32())) {
if (!input.CheckSizeInt32()) [[unlikely]] {
THROW_ERR_OUT_OF_RANGE(env, "prime is too big");
return Nothing<void>();
}
Expand Down
Loading

0 comments on commit 97535a9

Please sign in to comment.