Skip to content

Commit 317d245

Browse files
authored
src: modernize likely/unlikely hints
PR-URL: #55155 Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 3111ed7 commit 317d245

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+287
-219
lines changed

.cpplint

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
set noparent
2-
filter=-build/include_alpha,-build/include_subdir,-build/include_what_you_use,-legal/copyright,-readability/nolint
2+
filter=-build/include_alpha,-build/include_subdir,-build/include_what_you_use,-legal/copyright,-readability/nolint,-readability/braces
33
linelength=80

src/api/callback.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ InternalCallbackScope::InternalCallbackScope(Environment* env,
7474
// We first check `env->context() != current_context` because the contexts
7575
// likely *are* the same, in which case we can skip the slightly more
7676
// expensive Environment::GetCurrent() call.
77-
if (UNLIKELY(env->context() != current_context)) {
77+
if (env->context() != current_context) [[unlikely]] {
7878
CHECK_EQ(Environment::GetCurrent(isolate), env);
7979
}
8080

src/api/environment.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,17 @@ void* NodeArrayBufferAllocator::Allocate(size_t size) {
108108
ret = allocator_->Allocate(size);
109109
else
110110
ret = allocator_->AllocateUninitialized(size);
111-
if (LIKELY(ret != nullptr))
111+
if (ret != nullptr) [[likely]] {
112112
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
113+
}
113114
return ret;
114115
}
115116

116117
void* NodeArrayBufferAllocator::AllocateUninitialized(size_t size) {
117118
void* ret = allocator_->AllocateUninitialized(size);
118-
if (LIKELY(ret != nullptr))
119+
if (ret != nullptr) [[likely]] {
119120
total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
121+
}
120122
return ret;
121123
}
122124

src/base_object.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ BaseObject::BaseObject(Realm* realm, Local<Object> object)
3131
BaseObject::~BaseObject() {
3232
realm()->UntrackBaseObject(this);
3333

34-
if (UNLIKELY(has_pointer_data())) {
34+
if (has_pointer_data()) [[unlikely]] {
3535
PointerData* metadata = pointer_data();
3636
CHECK_EQ(metadata->strong_ptr_count, 0);
3737
metadata->self = nullptr;

src/cares_wrap.cc

+26-17
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,9 @@ int SoaTraits::Send(QueryWrap<SoaTraits>* wrap, const char* name) {
892892
int AnyTraits::Parse(
893893
QueryAnyWrap* wrap,
894894
const std::unique_ptr<ResponseData>& response) {
895-
if (UNLIKELY(response->is_host))
895+
if (response->is_host) [[unlikely]] {
896896
return ARES_EBADRESP;
897+
}
897898

898899
unsigned char* buf = response->buf.data;
899900
int len = response->buf.size;
@@ -1059,8 +1060,9 @@ int AnyTraits::Parse(
10591060
int ATraits::Parse(
10601061
QueryAWrap* wrap,
10611062
const std::unique_ptr<ResponseData>& response) {
1062-
if (UNLIKELY(response->is_host))
1063+
if (response->is_host) [[unlikely]] {
10631064
return ARES_EBADRESP;
1065+
}
10641066

10651067
unsigned char* buf = response->buf.data;
10661068
int len = response->buf.size;
@@ -1093,8 +1095,9 @@ int ATraits::Parse(
10931095
int AaaaTraits::Parse(
10941096
QueryAaaaWrap* wrap,
10951097
const std::unique_ptr<ResponseData>& response) {
1096-
if (UNLIKELY(response->is_host))
1098+
if (response->is_host) [[unlikely]] {
10971099
return ARES_EBADRESP;
1100+
}
10981101

10991102
unsigned char* buf = response->buf.data;
11001103
int len = response->buf.size;
@@ -1127,8 +1130,9 @@ int AaaaTraits::Parse(
11271130
int CaaTraits::Parse(
11281131
QueryCaaWrap* wrap,
11291132
const std::unique_ptr<ResponseData>& response) {
1130-
if (UNLIKELY(response->is_host))
1133+
if (response->is_host) [[unlikely]] {
11311134
return ARES_EBADRESP;
1135+
}
11321136

11331137
unsigned char* buf = response->buf.data;
11341138
int len = response->buf.size;
@@ -1149,8 +1153,9 @@ int CaaTraits::Parse(
11491153
int CnameTraits::Parse(
11501154
QueryCnameWrap* wrap,
11511155
const std::unique_ptr<ResponseData>& response) {
1152-
if (UNLIKELY(response->is_host))
1156+
if (response->is_host) [[unlikely]] {
11531157
return ARES_EBADRESP;
1158+
}
11541159

11551160
unsigned char* buf = response->buf.data;
11561161
int len = response->buf.size;
@@ -1172,8 +1177,9 @@ int CnameTraits::Parse(
11721177
int MxTraits::Parse(
11731178
QueryMxWrap* wrap,
11741179
const std::unique_ptr<ResponseData>& response) {
1175-
if (UNLIKELY(response->is_host))
1180+
if (response->is_host) [[unlikely]] {
11761181
return ARES_EBADRESP;
1182+
}
11771183

11781184
unsigned char* buf = response->buf.data;
11791185
int len = response->buf.size;
@@ -1195,8 +1201,9 @@ int MxTraits::Parse(
11951201
int NsTraits::Parse(
11961202
QueryNsWrap* wrap,
11971203
const std::unique_ptr<ResponseData>& response) {
1198-
if (UNLIKELY(response->is_host))
1204+
if (response->is_host) [[unlikely]] {
11991205
return ARES_EBADRESP;
1206+
}
12001207

12011208
unsigned char* buf = response->buf.data;
12021209
int len = response->buf.size;
@@ -1218,8 +1225,9 @@ int NsTraits::Parse(
12181225
int TxtTraits::Parse(
12191226
QueryTxtWrap* wrap,
12201227
const std::unique_ptr<ResponseData>& response) {
1221-
if (UNLIKELY(response->is_host))
1228+
if (response->is_host) [[unlikely]] {
12221229
return ARES_EBADRESP;
1230+
}
12231231

12241232
unsigned char* buf = response->buf.data;
12251233
int len = response->buf.size;
@@ -1240,8 +1248,9 @@ int TxtTraits::Parse(
12401248
int SrvTraits::Parse(
12411249
QuerySrvWrap* wrap,
12421250
const std::unique_ptr<ResponseData>& response) {
1243-
if (UNLIKELY(response->is_host))
1251+
if (response->is_host) [[unlikely]] {
12441252
return ARES_EBADRESP;
1253+
}
12451254

12461255
unsigned char* buf = response->buf.data;
12471256
int len = response->buf.size;
@@ -1262,9 +1271,9 @@ int SrvTraits::Parse(
12621271
int PtrTraits::Parse(
12631272
QueryPtrWrap* wrap,
12641273
const std::unique_ptr<ResponseData>& response) {
1265-
if (UNLIKELY(response->is_host))
1274+
if (response->is_host) [[unlikely]] {
12661275
return ARES_EBADRESP;
1267-
1276+
}
12681277
unsigned char* buf = response->buf.data;
12691278
int len = response->buf.size;
12701279

@@ -1286,9 +1295,9 @@ int PtrTraits::Parse(
12861295
int NaptrTraits::Parse(
12871296
QueryNaptrWrap* wrap,
12881297
const std::unique_ptr<ResponseData>& response) {
1289-
if (UNLIKELY(response->is_host))
1298+
if (response->is_host) [[unlikely]] {
12901299
return ARES_EBADRESP;
1291-
1300+
}
12921301
unsigned char* buf = response->buf.data;
12931302
int len = response->buf.size;
12941303

@@ -1308,9 +1317,9 @@ int NaptrTraits::Parse(
13081317
int SoaTraits::Parse(
13091318
QuerySoaWrap* wrap,
13101319
const std::unique_ptr<ResponseData>& response) {
1311-
if (UNLIKELY(response->is_host))
1320+
if (response->is_host) [[unlikely]] {
13121321
return ARES_EBADRESP;
1313-
1322+
}
13141323
unsigned char* buf = response->buf.data;
13151324
int len = response->buf.size;
13161325

@@ -1388,9 +1397,9 @@ int ReverseTraits::Send(GetHostByAddrWrap* wrap, const char* name) {
13881397
int ReverseTraits::Parse(
13891398
GetHostByAddrWrap* wrap,
13901399
const std::unique_ptr<ResponseData>& response) {
1391-
if (UNLIKELY(!response->is_host))
1400+
if (!response->is_host) [[unlikely]] {
13921401
return ARES_EBADRESP;
1393-
1402+
}
13941403
struct hostent* host = response->host.get();
13951404

13961405
Environment* env = wrap->env();

src/compile_cache.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ uint32_t GetCacheKey(std::string_view filename, CachedCodeType type) {
5959
template <typename... Args>
6060
inline void CompileCacheHandler::Debug(const char* format,
6161
Args&&... args) const {
62-
if (UNLIKELY(is_debug_)) {
62+
if (is_debug_) [[unlikely]] {
6363
FPrintF(stderr, format, std::forward<Args>(args)...);
6464
}
6565
}
@@ -458,20 +458,20 @@ CompileCacheEnableResult CompileCacheHandler::Enable(Environment* env,
458458
cache_tag,
459459
cache_dir_with_tag);
460460

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

471-
if (UNLIKELY(!env->permission()->is_granted(
471+
if (!env->permission()->is_granted(
472472
env,
473473
permission::PermissionScope::kFileSystemRead,
474-
cache_dir_with_tag))) {
474+
cache_dir_with_tag)) [[unlikely]] {
475475
result.message = "Skipping compile cache because read permission for " +
476476
cache_dir_with_tag + " is not granted";
477477
result.status = CompileCacheEnableStatus::FAILED;

src/crypto/crypto_aes.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ bool ValidateIV(
339339
Local<Value> value,
340340
AESCipherConfig* params) {
341341
ArrayBufferOrViewContents<char> iv(value);
342-
if (UNLIKELY(!iv.CheckSizeInt32())) {
342+
if (!iv.CheckSizeInt32()) [[unlikely]] {
343343
THROW_ERR_OUT_OF_RANGE(env, "iv is too big");
344344
return false;
345345
}
@@ -377,7 +377,7 @@ bool ValidateAuthTag(
377377
return false;
378378
}
379379
ArrayBufferOrViewContents<char> tag_contents(value);
380-
if (UNLIKELY(!tag_contents.CheckSizeInt32())) {
380+
if (!tag_contents.CheckSizeInt32()) [[unlikely]] {
381381
THROW_ERR_OUT_OF_RANGE(env, "tagLength is too big");
382382
return false;
383383
}
@@ -412,7 +412,7 @@ bool ValidateAdditionalData(
412412
// Additional Data
413413
if (IsAnyBufferSource(value)) {
414414
ArrayBufferOrViewContents<char> additional(value);
415-
if (UNLIKELY(!additional.CheckSizeInt32())) {
415+
if (!additional.CheckSizeInt32()) [[unlikely]] {
416416
THROW_ERR_OUT_OF_RANGE(env, "additionalData is too big");
417417
return false;
418418
}

src/crypto/crypto_cipher.cc

+14-12
Original file line numberDiff line numberDiff line change
@@ -522,15 +522,16 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
522522
// raw bytes and proceed...
523523
const ByteSource key_buf = ByteSource::FromSecretKeyBytes(env, args[1]);
524524

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

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

531-
if (UNLIKELY(!iv_buf.CheckSizeInt32()))
532+
if (!iv_buf.CheckSizeInt32()) [[unlikely]] {
532533
return THROW_ERR_OUT_OF_RANGE(env, "iv is too big");
533-
534+
}
534535
// Don't assign to cipher->auth_tag_len_ directly; the value might not
535536
// represent a valid length at this point.
536537
unsigned int auth_tag_len;
@@ -672,9 +673,9 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
672673
}
673674

674675
ArrayBufferOrViewContents<char> auth_tag(args[0]);
675-
if (UNLIKELY(!auth_tag.CheckSizeInt32()))
676+
if (!auth_tag.CheckSizeInt32()) [[unlikely]] {
676677
return THROW_ERR_OUT_OF_RANGE(env, "buffer is too big");
677-
678+
}
678679
unsigned int tag_len = auth_tag.size();
679680

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

784-
if (UNLIKELY(!buf.CheckSizeInt32()))
785+
if (!buf.CheckSizeInt32()) [[unlikely]] {
785786
return THROW_ERR_OUT_OF_RANGE(env, "buffer is too big");
787+
}
786788
args.GetReturnValue().Set(cipher->SetAAD(buf, plaintext_len));
787789
}
788790

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

861-
if (UNLIKELY(size > INT_MAX))
863+
if (size > INT_MAX) [[unlikely]] {
862864
return THROW_ERR_OUT_OF_RANGE(env, "data is too long");
863-
865+
}
864866
UpdateResult r = cipher->Update(data, size, &out);
865867

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

10651067
ArrayBufferOrViewContents<unsigned char> buf(args[offset]);
1066-
if (UNLIKELY(!buf.CheckSizeInt32()))
1068+
if (!buf.CheckSizeInt32()) [[unlikely]] {
10671069
return THROW_ERR_OUT_OF_RANGE(env, "buffer is too long");
1068-
1070+
}
10691071
uint32_t padding;
10701072
if (!args[offset + 1]->Uint32Value(env->context()).To(&padding)) return;
10711073

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

11061108
ArrayBufferOrViewContents<unsigned char> oaep_label(
11071109
!args[offset + 3]->IsUndefined() ? args[offset + 3] : Local<Value>());
1108-
if (UNLIKELY(!oaep_label.CheckSizeInt32()))
1110+
if (!oaep_label.CheckSizeInt32()) [[unlikely]] {
11091111
return THROW_ERR_OUT_OF_RANGE(env, "oaepLabel is too big");
1110-
1112+
}
11111113
std::unique_ptr<BackingStore> out;
11121114
if (!Cipher<operation, EVP_PKEY_cipher_init, EVP_PKEY_cipher>(
11131115
env, pkey, padding, digest, oaep_label, buf, &out)) {

src/crypto/crypto_context.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ void SecureContext::SetEngineKey(const FunctionCallbackInfo<Value>& args) {
685685

686686
CHECK_EQ(args.Length(), 2);
687687

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

1201-
if (UNLIKELY(env->permission()->enabled())) {
1201+
if (env->permission()->enabled()) [[unlikely]] {
12021202
return THROW_ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED(
12031203
env,
12041204
"Programmatic selection of OpenSSL engines is unsupported while the "

0 commit comments

Comments
 (0)