Skip to content

Commit

Permalink
Remove usage of [[likely]] and [[unlikely]] (microsoft#5255)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanTLavavej authored Jan 29, 2025
1 parent d655c17 commit 2314e1a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 77 deletions.
18 changes: 2 additions & 16 deletions stl/inc/__msvc_chrono.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,16 +653,6 @@ namespace chrono {
using time_point = _CHRONO time_point<steady_clock>;
static constexpr bool is_steady = true;

#if defined(_M_ARM) || defined(_M_ARM64) // vvv ARM or ARM64 arch vvv
#define _LIKELY_ARM_ARM64 _LIKELY
#define _LIKELY_X86_X64
#elif defined(_M_IX86) || defined(_M_X64) // ^^^ ARM or ARM64 arch / x86 or x64 arch vvv
#define _LIKELY_ARM_ARM64
#define _LIKELY_X86_X64 _LIKELY
#else // ^^^ x86 or x64 arch / other arch vvv
#define _LIKELY_ARM_ARM64
#define _LIKELY_X86_X64
#endif // ^^^ other arch ^^^
_NODISCARD static time_point now() noexcept { // get current time
const long long _Freq = _Query_perf_frequency(); // doesn't change after system boot
const long long _Ctr = _Query_perf_counter();
Expand All @@ -671,15 +661,14 @@ namespace chrono {
// multiplies instead of divides to calculate the nanosecond value.
constexpr long long _TenMHz = 10'000'000;
constexpr long long _TwentyFourMHz = 24'000'000;
// clang-format off
if (_Freq == _TenMHz) _LIKELY_X86_X64 {
if (_Freq == _TenMHz) {
// 10 MHz is a very common QPC frequency on modern x86/x64 PCs. Optimizing for
// this specific frequency can double the performance of this function by
// avoiding the expensive frequency conversion path.
static_assert(period::den % _TenMHz == 0, "It should never fail.");
constexpr long long _Multiplier = period::den / _TenMHz;
return time_point(duration(_Ctr * _Multiplier));
} else if (_Freq == _TwentyFourMHz) _LIKELY_ARM_ARM64 {
} else if (_Freq == _TwentyFourMHz) {
// 24 MHz is a common frequency on ARM/ARM64, including cases where it emulates x86/x64.
const long long _Whole = (_Ctr / _TwentyFourMHz) * period::den;
const long long _Part = (_Ctr % _TwentyFourMHz) * period::den / _TwentyFourMHz;
Expand All @@ -694,10 +683,7 @@ namespace chrono {
const long long _Part = (_Ctr % _Freq) * period::den / _Freq;
return time_point(duration(_Whole + _Part));
}
// clang-format on
}
#undef _LIKELY_ARM_ARM64
#undef _LIKELY_X86_X64
};
} // namespace chrono
_STD_END
Expand Down
2 changes: 1 addition & 1 deletion stl/inc/__msvc_ranges_tuple_formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ struct _Range_default_formatter<_Kind, _Rng, _CharT> {
if constexpr (_RANGES contiguous_range<_Range_type>) {
const auto _Size = _STD _To_unsigned_like(_RANGES distance(_Rx));

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

Expand Down
28 changes: 14 additions & 14 deletions stl/inc/ostream
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ ios_base::iostate _Print_noformat_nonunicode(ostream& _Ostr, const string_view _
_TRY_IO_BEGIN
const auto _Characters_written = _Ostr.rdbuf()->sputn(_Str.data(), static_cast<streamsize>(_Str.size()));
const bool _Was_insertion_successful = static_cast<size_t>(_Characters_written) == _Str.size();
if (!_Was_insertion_successful) [[unlikely]] {
if (!_Was_insertion_successful) {
_State |= ios_base::badbit;
}
_CATCH_IO_(ios_base, _Ostr)
Expand All @@ -105,7 +105,7 @@ ios_base::iostate _Print_newline_only_nonunicode(ostream& _Ostr) {

_TRY_IO_BEGIN
const bool _Was_insertion_successful = _Ostr.rdbuf()->sputc('\n') == '\n';
if (!_Was_insertion_successful) [[unlikely]] {
if (!_Was_insertion_successful) {
_State |= ios_base::badbit;
}
_CATCH_IO_(ios_base, _Ostr)
Expand All @@ -119,9 +119,9 @@ void _Vprint_nonunicode_impl(
const ostream::sentry _Ok(_Ostr);
ios_base::iostate _State = ios_base::goodbit;

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

case __std_win_error::_Not_supported:
[[unlikely]] return _State;
return _State;

default:
[[unlikely]] return ios_base::failbit;
return ios_base::failbit;
}
#pragma warning(pop)

if (_Is_unicode_console) {
_TRY_IO_BEGIN
const bool _Was_flush_successful = _Ostr.rdbuf()->pubsync() != -1;
if (!_Was_flush_successful) [[unlikely]] {
if (!_Was_flush_successful) {
_State |= ios_base::badbit;
return _State;
}

const __std_win_error _Unicode_console_print_result =
_Unicode_console_func(_Unicode_console_retrieval_result._Console_handle);
if (_Unicode_console_print_result != __std_win_error::_Success) [[unlikely]] {
if (_Unicode_console_print_result != __std_win_error::_Success) {
_State |= ios_base::badbit;
}
_CATCH_IO_(ios_base, _Ostr)
Expand Down Expand Up @@ -234,9 +234,9 @@ void _Vprint_unicode_impl(
const ostream::sentry _Ok(_Ostr);
ios_base::iostate _State = ios_base::goodbit;

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

if (!_Ok) [[unlikely]] {
if (!_Ok) {
_State |= ios_base::badbit;
} else [[likely]] {
} else {
if constexpr (_STD _Is_ordinary_literal_encoding_utf8()) {
_State |= _STD _Print_noformat_unicode(_Ostr, _Str);
} else {
Expand Down Expand Up @@ -302,9 +302,9 @@ void println(ostream& _Ostr) {
const ostream::sentry _Ok(_Ostr);
ios_base::iostate _State = ios_base::goodbit;

if (!_Ok) [[unlikely]] {
if (!_Ok) {
_State |= ios_base::badbit;
} else [[likely]] {
} else {
if constexpr (_STD _Is_ordinary_literal_encoding_utf8()) {
_State |= _STD _Print_newline_only_unicode(_Ostr);
} else {
Expand Down
18 changes: 9 additions & 9 deletions stl/inc/print
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private:
inline void _Print_noformat_nonunicode_nonlocking(FILE* const _Stream, const string_view _Str) {
const bool _Was_write_successful = _CSTD _fwrite_nolock(_Str.data(), 1, _Str.size(), _Stream) == _Str.size();

if (!_Was_write_successful) [[unlikely]] {
if (!_Was_write_successful) {
_Throw_system_error(static_cast<errc>(errno));
}
}
Expand Down Expand Up @@ -99,7 +99,7 @@ inline void _Print_noformat_unicode_to_console_nonlocking(
const __std_unicode_console_handle _Console_handle, const string_view _Str) {
const __std_win_error _Console_print_result =
__std_print_to_unicode_console(_Console_handle, _Str.data(), _Str.size());
if (_Console_print_result != __std_win_error::_Success) [[unlikely]] {
if (_Console_print_result != __std_win_error::_Success) {
_STD _Throw_system_error_from_std_win_error(_Console_print_result);
}
}
Expand Down Expand Up @@ -160,10 +160,10 @@ void _Do_on_maybe_unicode_console(
break;

case __std_win_error::_Not_supported:
[[unlikely]] return;
return;

default:
[[unlikely]] _STD _Throw_system_error_from_std_win_error(_Unicode_console_retrieval_result._Error);
_STD _Throw_system_error_from_std_win_error(_Unicode_console_retrieval_result._Error);
}
#pragma warning(pop)

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

const bool _Was_flush_successful = _CSTD _fflush_nolock(_Stream) == 0;
if (!_Was_flush_successful) [[unlikely]] {
if (!_Was_flush_successful) {
_Throw_system_error(static_cast<errc>(errno));
}

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

if (!_Was_write_successful) [[unlikely]] {
if (!_Was_write_successful) {
_Throw_system_error(static_cast<errc>(errno));
}
}

inline void _Print_newline_only_unicode(FILE* const _Stream) {
const auto _Unicode_console = [&](const __std_unicode_console_handle _Console_handle) {
const bool _Was_flush_successful = _CSTD fflush(_Stream) == 0;
if (!_Was_flush_successful) [[unlikely]] {
if (!_Was_flush_successful) {
_Throw_system_error(static_cast<errc>(errno));
}

const __std_win_error _Console_print_result = __std_print_newline_only_to_unicode_console(_Console_handle);
if (_Console_print_result != __std_win_error::_Success) [[unlikely]] {
if (_Console_print_result != __std_win_error::_Success) {
_STD _Throw_system_error_from_std_win_error(_Console_print_result);
}
};
Expand All @@ -234,7 +234,7 @@ inline void _Vprint_unicode_impl(
const _Stream_lock_guard _Guard{_Stream};

const bool _Was_flush_successful = _CSTD _fflush_nolock(_Stream) == 0;
if (!_Was_flush_successful) [[unlikely]] {
if (!_Was_flush_successful) {
_Throw_system_error(static_cast<errc>(errno));
}

Expand Down
14 changes: 0 additions & 14 deletions stl/inc/yvals_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,20 +564,6 @@
#define _FALLTHROUGH
#endif

#ifndef __has_cpp_attribute // vvv no attributes vvv
#define _LIKELY
#define _UNLIKELY
#elif __has_cpp_attribute(likely) >= 201803L && __has_cpp_attribute(unlikely) >= 201803L // ^^^ no attr / C++20 attr vvv
#define _LIKELY [[likely]]
#define _UNLIKELY [[unlikely]]
#elif defined(__clang__) // ^^^ C++20 attributes / clang attributes and C++17 or C++14 vvv
#define _LIKELY [[__likely__]]
#define _UNLIKELY [[__unlikely__]]
#else // ^^^ clang attributes and C++17 or C++14 / C1XX attributes and C++17 or C++14 vvv
#define _LIKELY
#define _UNLIKELY
#endif // ^^^ C1XX attributes and C++17 or C++14 ^^^

// _HAS_NODISCARD (in vcruntime.h) controls:
// [[nodiscard]] attributes on STL functions

Expand Down
32 changes: 16 additions & 16 deletions stl/src/print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ extern "C" {

[[nodiscard]] _Success_(return._Error == __std_win_error::_Success) __std_unicode_console_retrieval_result
__stdcall __std_get_unicode_console_handle_from_file_stream(_In_ FILE* const _Stream) noexcept {
if (_Stream == nullptr) [[unlikely]] {
if (_Stream == nullptr) {
return __std_unicode_console_retrieval_result{._Error = __std_win_error::_Invalid_parameter};
}

const int _Fd = _fileno(_Stream);

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

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

if (_Console_handle == INVALID_HANDLE_VALUE) [[unlikely]] {
if (_Console_handle == INVALID_HANDLE_VALUE) {
return __std_unicode_console_retrieval_result{._Error = __std_win_error::_Invalid_parameter};
}

Expand Down Expand Up @@ -89,7 +89,7 @@ namespace {

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

if (!_Str) [[unlikely]] {
if (!_Str) {
_Str_capacity = _Buffer_size;
_Buffer[0] = L'\0'; // Activate _Buffer
return false;
Expand Down Expand Up @@ -149,7 +149,7 @@ namespace {
const char* const _Str, const size_t _Str_size) noexcept {
constexpr size_t _Max_str_segment_size = 8192;

if (_Str_size <= _Max_str_segment_size) [[likely]] {
if (_Str_size <= _Max_str_segment_size) {
return _Minimal_string_view{_Str, _Str_size};
}

Expand Down Expand Up @@ -210,7 +210,7 @@ namespace {
[[nodiscard]] _Transcode_result _Transcode_utf8_string(
_Allocated_string& _Dst_str, const _Minimal_string_view _Src_str) noexcept {
// MultiByteToWideChar() fails if strLength == 0.
if (_Src_str._Empty()) [[unlikely]] {
if (_Src_str._Empty()) {
return {};
}

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

if (_Num_chars_required == 0) [[unlikely]] {
if (_Num_chars_required == 0) {
return static_cast<__std_win_error>(GetLastError());
}

const bool _Has_space = _Dst_str._Grow(static_cast<size_t>(_Num_chars_required));
if (!_Has_space) [[unlikely]] {
if (!_Has_space) {
return __std_win_error::_Not_enough_memory;
}

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

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

if (!_Write_result) [[unlikely]] {
if (!_Write_result) {
return static_cast<__std_win_error>(GetLastError());
}

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

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

if (!_Transcoded_str._Has_value()) [[unlikely]] {
if (!_Transcoded_str._Has_value()) {
return _Transcoded_str._Error();
}

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

if (_Write_result != __std_win_error::_Success) [[unlikely]] {
if (_Write_result != __std_win_error::_Success) {
return _Write_result;
}

Expand All @@ -300,15 +300,15 @@ extern "C" {
[[nodiscard]] _Success_(return == __std_win_error::_Success) __std_win_error
__stdcall __std_print_newline_only_to_unicode_console(
_In_ const __std_unicode_console_handle _Console_handle) noexcept {
if (_Console_handle == __std_unicode_console_handle::_Invalid) [[unlikely]] {
if (_Console_handle == __std_unicode_console_handle::_Invalid) {
return __std_win_error::_Invalid_parameter;
}

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

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

if (!_Write_result) [[unlikely]] {
if (!_Write_result) {
return static_cast<__std_win_error>(GetLastError());
}

Expand Down
4 changes: 2 additions & 2 deletions stl/src/vector_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5310,7 +5310,7 @@ namespace {
_Size_convert = _Size_bits;

for (size_t _Ix = _Size_bits; _Ix < _Size_chars; ++_Ix) {
if (const _Elem _Cur = _Src[_Ix]; _Cur != _Elem0 && _Cur != _Elem1) [[unlikely]] {
if (const _Elem _Cur = _Src[_Ix]; _Cur != _Elem0 && _Cur != _Elem1) {
return false;
}
}
Expand All @@ -5321,7 +5321,7 @@ namespace {
for (size_t _Ix = 0; _Ix != _Size_convert; ++_Ix) {
const _Elem _Cur = _Src[_Size_convert - _Ix - 1];

if (_Cur != _Elem0 && _Cur != _Elem1) [[unlikely]] {
if (_Cur != _Elem0 && _Cur != _Elem1) {
return false;
}

Expand Down
Loading

0 comments on commit 2314e1a

Please sign in to comment.