Skip to content

Commit d6bcc44

Browse files
anonrigtargos
authored andcommitted
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 c7ed2ff commit d6bcc44

Some content is hidden

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

54 files changed

+292
-223
lines changed

.cpplint

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ BaseObject::~BaseObject() {
3232
realm()->modify_base_object_count(-1);
3333
realm()->RemoveCleanupHook(DeleteMe, static_cast<void*>(this));
3434

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

src/cares_wrap.cc

Lines changed: 26 additions & 17 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 14 additions & 12 deletions
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());
@@ -782,8 +783,9 @@ void CipherBase::SetAAD(const FunctionCallbackInfo<Value>& args) {
782783
int plaintext_len = args[1].As<Int32>()->Value();
783784
ArrayBufferOrViewContents<unsigned char> buf(args[0]);
784785

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

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

862-
if (UNLIKELY(size > INT_MAX))
864+
if (size > INT_MAX) [[unlikely]] {
863865
return THROW_ERR_OUT_OF_RANGE(env, "data is too long");
864-
866+
}
865867
UpdateResult r = cipher->Update(data, size, &out);
866868

867869
if (r != kSuccess) {
@@ -1064,9 +1066,9 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
10641066
return;
10651067

10661068
ArrayBufferOrViewContents<unsigned char> buf(args[offset]);
1067-
if (UNLIKELY(!buf.CheckSizeInt32()))
1069+
if (!buf.CheckSizeInt32()) [[unlikely]] {
10681070
return THROW_ERR_OUT_OF_RANGE(env, "buffer is too long");
1069-
1071+
}
10701072
uint32_t padding;
10711073
if (!args[offset + 1]->Uint32Value(env->context()).To(&padding)) return;
10721074

@@ -1106,9 +1108,9 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo<Value>& args) {
11061108

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

src/crypto/crypto_context.cc

Lines changed: 2 additions & 2 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy