Skip to content

Commit 1bafd02

Browse files
authored
[libc++] Mark __{emplace,push}_back_slow_path as noinline (#94379)
These are almost certainly intended to not be inlined. This significantly reduces code size when `push_back` and `emplace_back` are used heavily. Fixes #94360
1 parent 9e1d656 commit 1bafd02

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

libcxx/include/__vector/vector.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,24 @@ vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args) {
11421142
return this->__end_;
11431143
}
11441144

1145+
// This makes the compiler inline `__else()` if `__cond` is known to be false. Currently LLVM doesn't do that without
1146+
// the `__builtin_constant_p`, since it considers `__else` unlikely even through it's known to be run.
1147+
// See https://llvm.org/PR154292
1148+
template <class _If, class _Else>
1149+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __if_likely_else(bool __cond, _If __if, _Else __else) {
1150+
if (__builtin_constant_p(__cond)) {
1151+
if (__cond)
1152+
__if();
1153+
else
1154+
__else();
1155+
} else {
1156+
if (__cond) [[__likely__]]
1157+
__if();
1158+
else
1159+
__else();
1160+
}
1161+
}
1162+
11451163
template <class _Tp, class _Allocator>
11461164
template <class... _Args>
11471165
_LIBCPP_CONSTEXPR_SINCE_CXX20 inline
@@ -1152,12 +1170,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 inline
11521170
#endif
11531171
vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
11541172
pointer __end = this->__end_;
1155-
if (__end < this->__cap_) {
1156-
__emplace_back_assume_capacity(std::forward<_Args>(__args)...);
1157-
++__end;
1158-
} else {
1159-
__end = __emplace_back_slow_path(std::forward<_Args>(__args)...);
1160-
}
1173+
std::__if_likely_else(
1174+
__end < this->__cap_,
1175+
[&] {
1176+
__emplace_back_assume_capacity(std::forward<_Args>(__args)...);
1177+
++__end;
1178+
},
1179+
[&] { __end = __emplace_back_slow_path(std::forward<_Args>(__args)...); });
1180+
11611181
this->__end_ = __end;
11621182
#if _LIBCPP_STD_VER >= 17
11631183
return *(__end - 1);

0 commit comments

Comments
 (0)