Skip to content

Commit 2314e1a

Browse files
Remove usage of [[likely]] and [[unlikely]] (#5255)
1 parent d655c17 commit 2314e1a

File tree

8 files changed

+49
-77
lines changed

8 files changed

+49
-77
lines changed

stl/inc/__msvc_chrono.hpp

+2-16
Original file line numberDiff line numberDiff line change
@@ -653,16 +653,6 @@ namespace chrono {
653653
using time_point = _CHRONO time_point<steady_clock>;
654654
static constexpr bool is_steady = true;
655655

656-
#if defined(_M_ARM) || defined(_M_ARM64) // vvv ARM or ARM64 arch vvv
657-
#define _LIKELY_ARM_ARM64 _LIKELY
658-
#define _LIKELY_X86_X64
659-
#elif defined(_M_IX86) || defined(_M_X64) // ^^^ ARM or ARM64 arch / x86 or x64 arch vvv
660-
#define _LIKELY_ARM_ARM64
661-
#define _LIKELY_X86_X64 _LIKELY
662-
#else // ^^^ x86 or x64 arch / other arch vvv
663-
#define _LIKELY_ARM_ARM64
664-
#define _LIKELY_X86_X64
665-
#endif // ^^^ other arch ^^^
666656
_NODISCARD static time_point now() noexcept { // get current time
667657
const long long _Freq = _Query_perf_frequency(); // doesn't change after system boot
668658
const long long _Ctr = _Query_perf_counter();
@@ -671,15 +661,14 @@ namespace chrono {
671661
// multiplies instead of divides to calculate the nanosecond value.
672662
constexpr long long _TenMHz = 10'000'000;
673663
constexpr long long _TwentyFourMHz = 24'000'000;
674-
// clang-format off
675-
if (_Freq == _TenMHz) _LIKELY_X86_X64 {
664+
if (_Freq == _TenMHz) {
676665
// 10 MHz is a very common QPC frequency on modern x86/x64 PCs. Optimizing for
677666
// this specific frequency can double the performance of this function by
678667
// avoiding the expensive frequency conversion path.
679668
static_assert(period::den % _TenMHz == 0, "It should never fail.");
680669
constexpr long long _Multiplier = period::den / _TenMHz;
681670
return time_point(duration(_Ctr * _Multiplier));
682-
} else if (_Freq == _TwentyFourMHz) _LIKELY_ARM_ARM64 {
671+
} else if (_Freq == _TwentyFourMHz) {
683672
// 24 MHz is a common frequency on ARM/ARM64, including cases where it emulates x86/x64.
684673
const long long _Whole = (_Ctr / _TwentyFourMHz) * period::den;
685674
const long long _Part = (_Ctr % _TwentyFourMHz) * period::den / _TwentyFourMHz;
@@ -694,10 +683,7 @@ namespace chrono {
694683
const long long _Part = (_Ctr % _Freq) * period::den / _Freq;
695684
return time_point(duration(_Whole + _Part));
696685
}
697-
// clang-format on
698686
}
699-
#undef _LIKELY_ARM_ARM64
700-
#undef _LIKELY_X86_X64
701687
};
702688
} // namespace chrono
703689
_STD_END

stl/inc/__msvc_ranges_tuple_formatter.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ struct _Range_default_formatter<_Kind, _Rng, _CharT> {
10811081
if constexpr (_RANGES contiguous_range<_Range_type>) {
10821082
const auto _Size = _STD _To_unsigned_like(_RANGES distance(_Rx));
10831083

1084-
if (!_STD in_range<size_t>(_Size)) [[unlikely]] {
1084+
if (!_STD in_range<size_t>(_Size)) {
10851085
_Throw_format_error("Formatted range is too long.");
10861086
}
10871087

stl/inc/ostream

+14-14
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ ios_base::iostate _Print_noformat_nonunicode(ostream& _Ostr, const string_view _
8787
_TRY_IO_BEGIN
8888
const auto _Characters_written = _Ostr.rdbuf()->sputn(_Str.data(), static_cast<streamsize>(_Str.size()));
8989
const bool _Was_insertion_successful = static_cast<size_t>(_Characters_written) == _Str.size();
90-
if (!_Was_insertion_successful) [[unlikely]] {
90+
if (!_Was_insertion_successful) {
9191
_State |= ios_base::badbit;
9292
}
9393
_CATCH_IO_(ios_base, _Ostr)
@@ -105,7 +105,7 @@ ios_base::iostate _Print_newline_only_nonunicode(ostream& _Ostr) {
105105

106106
_TRY_IO_BEGIN
107107
const bool _Was_insertion_successful = _Ostr.rdbuf()->sputc('\n') == '\n';
108-
if (!_Was_insertion_successful) [[unlikely]] {
108+
if (!_Was_insertion_successful) {
109109
_State |= ios_base::badbit;
110110
}
111111
_CATCH_IO_(ios_base, _Ostr)
@@ -119,9 +119,9 @@ void _Vprint_nonunicode_impl(
119119
const ostream::sentry _Ok(_Ostr);
120120
ios_base::iostate _State = ios_base::goodbit;
121121

122-
if (!_Ok) [[unlikely]] {
122+
if (!_Ok) {
123123
_State |= ios_base::badbit;
124-
} else [[likely]] {
124+
} else {
125125
// This is intentionally kept outside of the try/catch block in _Print_noformat_nonunicode()
126126
// (see N4950 [ostream.formatted.print]/3.2).
127127
string _Output_str = _STD vformat(_Ostr.getloc(), _Fmt_str, _Fmt_args);
@@ -178,24 +178,24 @@ ios_base::iostate _Do_on_maybe_unicode_console(
178178
break;
179179

180180
case __std_win_error::_Not_supported:
181-
[[unlikely]] return _State;
181+
return _State;
182182

183183
default:
184-
[[unlikely]] return ios_base::failbit;
184+
return ios_base::failbit;
185185
}
186186
#pragma warning(pop)
187187

188188
if (_Is_unicode_console) {
189189
_TRY_IO_BEGIN
190190
const bool _Was_flush_successful = _Ostr.rdbuf()->pubsync() != -1;
191-
if (!_Was_flush_successful) [[unlikely]] {
191+
if (!_Was_flush_successful) {
192192
_State |= ios_base::badbit;
193193
return _State;
194194
}
195195

196196
const __std_win_error _Unicode_console_print_result =
197197
_Unicode_console_func(_Unicode_console_retrieval_result._Console_handle);
198-
if (_Unicode_console_print_result != __std_win_error::_Success) [[unlikely]] {
198+
if (_Unicode_console_print_result != __std_win_error::_Success) {
199199
_State |= ios_base::badbit;
200200
}
201201
_CATCH_IO_(ios_base, _Ostr)
@@ -234,9 +234,9 @@ void _Vprint_unicode_impl(
234234
const ostream::sentry _Ok(_Ostr);
235235
ios_base::iostate _State = ios_base::goodbit;
236236

237-
if (!_Ok) [[unlikely]] {
237+
if (!_Ok) {
238238
_State |= ios_base::badbit;
239-
} else [[likely]] {
239+
} else {
240240
// This is intentionally kept outside of the try/catch block in _Print_noformat_unicode()
241241
// (see N4950 [ostream.formatted.print]/3.2).
242242
string _Output_str = _STD vformat(_Ostr.getloc(), _Fmt_str, _Fmt_args);
@@ -255,9 +255,9 @@ void _Print_noformat(ostream& _Ostr, const string_view _Str) {
255255
const ostream::sentry _Ok(_Ostr);
256256
ios_base::iostate _State = ios_base::goodbit;
257257

258-
if (!_Ok) [[unlikely]] {
258+
if (!_Ok) {
259259
_State |= ios_base::badbit;
260-
} else [[likely]] {
260+
} else {
261261
if constexpr (_STD _Is_ordinary_literal_encoding_utf8()) {
262262
_State |= _STD _Print_noformat_unicode(_Ostr, _Str);
263263
} else {
@@ -302,9 +302,9 @@ void println(ostream& _Ostr) {
302302
const ostream::sentry _Ok(_Ostr);
303303
ios_base::iostate _State = ios_base::goodbit;
304304

305-
if (!_Ok) [[unlikely]] {
305+
if (!_Ok) {
306306
_State |= ios_base::badbit;
307-
} else [[likely]] {
307+
} else {
308308
if constexpr (_STD _Is_ordinary_literal_encoding_utf8()) {
309309
_State |= _STD _Print_newline_only_unicode(_Ostr);
310310
} else {

stl/inc/print

+9-9
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private:
6161
inline void _Print_noformat_nonunicode_nonlocking(FILE* const _Stream, const string_view _Str) {
6262
const bool _Was_write_successful = _CSTD _fwrite_nolock(_Str.data(), 1, _Str.size(), _Stream) == _Str.size();
6363

64-
if (!_Was_write_successful) [[unlikely]] {
64+
if (!_Was_write_successful) {
6565
_Throw_system_error(static_cast<errc>(errno));
6666
}
6767
}
@@ -99,7 +99,7 @@ inline void _Print_noformat_unicode_to_console_nonlocking(
9999
const __std_unicode_console_handle _Console_handle, const string_view _Str) {
100100
const __std_win_error _Console_print_result =
101101
__std_print_to_unicode_console(_Console_handle, _Str.data(), _Str.size());
102-
if (_Console_print_result != __std_win_error::_Success) [[unlikely]] {
102+
if (_Console_print_result != __std_win_error::_Success) {
103103
_STD _Throw_system_error_from_std_win_error(_Console_print_result);
104104
}
105105
}
@@ -160,10 +160,10 @@ void _Do_on_maybe_unicode_console(
160160
break;
161161

162162
case __std_win_error::_Not_supported:
163-
[[unlikely]] return;
163+
return;
164164

165165
default:
166-
[[unlikely]] _STD _Throw_system_error_from_std_win_error(_Unicode_console_retrieval_result._Error);
166+
_STD _Throw_system_error_from_std_win_error(_Unicode_console_retrieval_result._Error);
167167
}
168168
#pragma warning(pop)
169169

@@ -180,7 +180,7 @@ inline void _Vprint_unicode_noformat_impl(FILE* const _Stream, const string_view
180180
const _Stream_lock_guard _Guard{_Stream};
181181

182182
const bool _Was_flush_successful = _CSTD _fflush_nolock(_Stream) == 0;
183-
if (!_Was_flush_successful) [[unlikely]] {
183+
if (!_Was_flush_successful) {
184184
_Throw_system_error(static_cast<errc>(errno));
185185
}
186186

@@ -195,20 +195,20 @@ inline void _Vprint_unicode_noformat_impl(FILE* const _Stream, const string_view
195195
inline void _Fputc_newline(FILE* const _Stream) {
196196
const bool _Was_write_successful = _CSTD fputc('\n', _Stream) == '\n';
197197

198-
if (!_Was_write_successful) [[unlikely]] {
198+
if (!_Was_write_successful) {
199199
_Throw_system_error(static_cast<errc>(errno));
200200
}
201201
}
202202

203203
inline void _Print_newline_only_unicode(FILE* const _Stream) {
204204
const auto _Unicode_console = [&](const __std_unicode_console_handle _Console_handle) {
205205
const bool _Was_flush_successful = _CSTD fflush(_Stream) == 0;
206-
if (!_Was_flush_successful) [[unlikely]] {
206+
if (!_Was_flush_successful) {
207207
_Throw_system_error(static_cast<errc>(errno));
208208
}
209209

210210
const __std_win_error _Console_print_result = __std_print_newline_only_to_unicode_console(_Console_handle);
211-
if (_Console_print_result != __std_win_error::_Success) [[unlikely]] {
211+
if (_Console_print_result != __std_win_error::_Success) {
212212
_STD _Throw_system_error_from_std_win_error(_Console_print_result);
213213
}
214214
};
@@ -234,7 +234,7 @@ inline void _Vprint_unicode_impl(
234234
const _Stream_lock_guard _Guard{_Stream};
235235

236236
const bool _Was_flush_successful = _CSTD _fflush_nolock(_Stream) == 0;
237-
if (!_Was_flush_successful) [[unlikely]] {
237+
if (!_Was_flush_successful) {
238238
_Throw_system_error(static_cast<errc>(errno));
239239
}
240240

stl/inc/yvals_core.h

-14
Original file line numberDiff line numberDiff line change
@@ -564,20 +564,6 @@
564564
#define _FALLTHROUGH
565565
#endif
566566

567-
#ifndef __has_cpp_attribute // vvv no attributes vvv
568-
#define _LIKELY
569-
#define _UNLIKELY
570-
#elif __has_cpp_attribute(likely) >= 201803L && __has_cpp_attribute(unlikely) >= 201803L // ^^^ no attr / C++20 attr vvv
571-
#define _LIKELY [[likely]]
572-
#define _UNLIKELY [[unlikely]]
573-
#elif defined(__clang__) // ^^^ C++20 attributes / clang attributes and C++17 or C++14 vvv
574-
#define _LIKELY [[__likely__]]
575-
#define _UNLIKELY [[__unlikely__]]
576-
#else // ^^^ clang attributes and C++17 or C++14 / C1XX attributes and C++17 or C++14 vvv
577-
#define _LIKELY
578-
#define _UNLIKELY
579-
#endif // ^^^ C1XX attributes and C++17 or C++14 ^^^
580-
581567
// _HAS_NODISCARD (in vcruntime.h) controls:
582568
// [[nodiscard]] attributes on STL functions
583569

stl/src/print.cpp

+16-16
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ extern "C" {
1616

1717
[[nodiscard]] _Success_(return._Error == __std_win_error::_Success) __std_unicode_console_retrieval_result
1818
__stdcall __std_get_unicode_console_handle_from_file_stream(_In_ FILE* const _Stream) noexcept {
19-
if (_Stream == nullptr) [[unlikely]] {
19+
if (_Stream == nullptr) {
2020
return __std_unicode_console_retrieval_result{._Error = __std_win_error::_Invalid_parameter};
2121
}
2222

2323
const int _Fd = _fileno(_Stream);
2424

25-
if (_Fd == -2) [[unlikely]] {
25+
if (_Fd == -2) {
2626
// According to https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fileno?view=msvc-170 ,
2727
// _fileno() returns -2 if _Stream refers to either stdout or stderr and there is no associated output stream.
2828
// In that case, there is also no associated console HANDLE. (We haven't observed this happening in practice.)
2929
return __std_unicode_console_retrieval_result{._Error = __std_win_error::_Not_supported};
30-
} else if (_Fd == -1) [[unlikely]] {
30+
} else if (_Fd == -1) {
3131
return __std_unicode_console_retrieval_result{._Error = __std_win_error::_Invalid_parameter};
3232
}
3333

3434
const HANDLE _Console_handle = reinterpret_cast<HANDLE>(_get_osfhandle(_Fd));
3535

36-
if (_Console_handle == INVALID_HANDLE_VALUE) [[unlikely]] {
36+
if (_Console_handle == INVALID_HANDLE_VALUE) {
3737
return __std_unicode_console_retrieval_result{._Error = __std_win_error::_Invalid_parameter};
3838
}
3939

@@ -89,7 +89,7 @@ namespace {
8989

9090
::new (&_Str) _Heap_string(_malloc_crt_t(wchar_t, _Capacity)); // Activate _Str
9191

92-
if (!_Str) [[unlikely]] {
92+
if (!_Str) {
9393
_Str_capacity = _Buffer_size;
9494
_Buffer[0] = L'\0'; // Activate _Buffer
9595
return false;
@@ -149,7 +149,7 @@ namespace {
149149
const char* const _Str, const size_t _Str_size) noexcept {
150150
constexpr size_t _Max_str_segment_size = 8192;
151151

152-
if (_Str_size <= _Max_str_segment_size) [[likely]] {
152+
if (_Str_size <= _Max_str_segment_size) {
153153
return _Minimal_string_view{_Str, _Str_size};
154154
}
155155

@@ -210,7 +210,7 @@ namespace {
210210
[[nodiscard]] _Transcode_result _Transcode_utf8_string(
211211
_Allocated_string& _Dst_str, const _Minimal_string_view _Src_str) noexcept {
212212
// MultiByteToWideChar() fails if strLength == 0.
213-
if (_Src_str._Empty()) [[unlikely]] {
213+
if (_Src_str._Empty()) {
214214
return {};
215215
}
216216

@@ -220,19 +220,19 @@ namespace {
220220
const int32_t _Num_chars_required =
221221
MultiByteToWideChar(CP_UTF8, 0, _Src_str._Data(), static_cast<int>(_Src_str._Size()), nullptr, 0);
222222

223-
if (_Num_chars_required == 0) [[unlikely]] {
223+
if (_Num_chars_required == 0) {
224224
return static_cast<__std_win_error>(GetLastError());
225225
}
226226

227227
const bool _Has_space = _Dst_str._Grow(static_cast<size_t>(_Num_chars_required));
228-
if (!_Has_space) [[unlikely]] {
228+
if (!_Has_space) {
229229
return __std_win_error::_Not_enough_memory;
230230
}
231231

232232
const int32_t _Conversion_result = MultiByteToWideChar(CP_UTF8, 0, _Src_str._Data(),
233233
static_cast<int>(_Src_str._Size()), _Dst_str._Data(), static_cast<int>(_Dst_str._Capacity()));
234234

235-
if (_Conversion_result == 0) [[unlikely]] {
235+
if (_Conversion_result == 0) {
236236
// This shouldn't happen...
237237
_CSTD abort();
238238
}
@@ -245,7 +245,7 @@ namespace {
245245
const BOOL _Write_result =
246246
WriteConsoleW(_Console_handle, _Wide_str._Data(), static_cast<DWORD>(_Wide_str._Size()), nullptr, nullptr);
247247

248-
if (!_Write_result) [[unlikely]] {
248+
if (!_Write_result) {
249249
return static_cast<__std_win_error>(GetLastError());
250250
}
251251

@@ -258,7 +258,7 @@ extern "C" {
258258
[[nodiscard]] _Success_(return == __std_win_error::_Success) __std_win_error
259259
__stdcall __std_print_to_unicode_console(_In_ const __std_unicode_console_handle _Console_handle,
260260
_In_reads_(_Str_size) const char* const _Str, _In_ const size_t _Str_size) noexcept {
261-
if (_Console_handle == __std_unicode_console_handle::_Invalid || _Str == nullptr) [[unlikely]] {
261+
if (_Console_handle == __std_unicode_console_handle::_Invalid || _Str == nullptr) {
262262
return __std_win_error::_Invalid_parameter;
263263
}
264264

@@ -277,13 +277,13 @@ extern "C" {
277277
_Curr_str_segment = _Get_next_utf8_string_segment(_Remaining_str, _Remaining_str_size);
278278
_Transcoded_str = _Transcode_utf8_string(_Allocated_str, _Curr_str_segment);
279279

280-
if (!_Transcoded_str._Has_value()) [[unlikely]] {
280+
if (!_Transcoded_str._Has_value()) {
281281
return _Transcoded_str._Error();
282282
}
283283

284284
const __std_win_error _Write_result = _Write_console(_Actual_console_handle, _Transcoded_str._Value());
285285

286-
if (_Write_result != __std_win_error::_Success) [[unlikely]] {
286+
if (_Write_result != __std_win_error::_Success) {
287287
return _Write_result;
288288
}
289289

@@ -300,15 +300,15 @@ extern "C" {
300300
[[nodiscard]] _Success_(return == __std_win_error::_Success) __std_win_error
301301
__stdcall __std_print_newline_only_to_unicode_console(
302302
_In_ const __std_unicode_console_handle _Console_handle) noexcept {
303-
if (_Console_handle == __std_unicode_console_handle::_Invalid) [[unlikely]] {
303+
if (_Console_handle == __std_unicode_console_handle::_Invalid) {
304304
return __std_win_error::_Invalid_parameter;
305305
}
306306

307307
const auto _Actual_console_handle = reinterpret_cast<HANDLE>(_Console_handle);
308308

309309
const BOOL _Write_result = WriteConsoleW(_Actual_console_handle, L"\n", 1, nullptr, nullptr);
310310

311-
if (!_Write_result) [[unlikely]] {
311+
if (!_Write_result) {
312312
return static_cast<__std_win_error>(GetLastError());
313313
}
314314

stl/src/vector_algorithms.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5310,7 +5310,7 @@ namespace {
53105310
_Size_convert = _Size_bits;
53115311

53125312
for (size_t _Ix = _Size_bits; _Ix < _Size_chars; ++_Ix) {
5313-
if (const _Elem _Cur = _Src[_Ix]; _Cur != _Elem0 && _Cur != _Elem1) [[unlikely]] {
5313+
if (const _Elem _Cur = _Src[_Ix]; _Cur != _Elem0 && _Cur != _Elem1) {
53145314
return false;
53155315
}
53165316
}
@@ -5321,7 +5321,7 @@ namespace {
53215321
for (size_t _Ix = 0; _Ix != _Size_convert; ++_Ix) {
53225322
const _Elem _Cur = _Src[_Size_convert - _Ix - 1];
53235323

5324-
if (_Cur != _Elem0 && _Cur != _Elem1) [[unlikely]] {
5324+
if (_Cur != _Elem0 && _Cur != _Elem1) {
53255325
return false;
53265326
}
53275327

0 commit comments

Comments
 (0)