Skip to content

Commit ff03a50

Browse files
committed
Add some noexcept optimizations removing the need for _Backout
1 parent 7c522bd commit ff03a50

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

stl/inc/memory

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,11 @@ namespace ranges {
272272
_CSTD memset(_OFirst, static_cast<unsigned char>(_Val), static_cast<size_t>(_OFinal - _OFirst));
273273
return _OFinal;
274274
}
275+
} else if constexpr (is_nothrow_constructible_v<iter_value_t<_It>, const _Ty&>) {
276+
for (; _OFirst != _OLast; ++_OFirst) {
277+
_Construct_in_place(*_OFirst, _Val);
278+
}
279+
return _OFirst;
275280
} else {
276281
_Uninitialized_backout _Backout{_STD move(_OFirst)};
277282

@@ -362,11 +367,17 @@ namespace ranges {
362367
// clang-format on
363368
_Algorithm_int_t<iter_difference_t<_It>> _Count = _Count_raw;
364369
if (_Count > 0) {
370+
auto _UFirst = _Get_unwrapped_n(_STD move(_First), _Count);
365371
if constexpr (_Fill_memset_is_safe<_It, _Ty>) {
366-
_CSTD memset(_Get_unwrapped_n(_STD move(_First), _Count), static_cast<unsigned char>(_Val), _Count);
372+
_CSTD memset(_STD move(_UFirst), static_cast<unsigned char>(_Val), _Count);
367373
_RANGES advance(_First, _Count);
374+
} else if constexpr (is_nothrow_constructible_v<iter_value_t<_It>, const _Ty&>) {
375+
for (; 0 < _Count; ++_UFirst, (void) --_Count) {
376+
_Construct_in_place(*_UFirst, _Val);
377+
}
378+
_Seek_wrapped(_First, _STD move(_UFirst));
368379
} else {
369-
_Uninitialized_backout _Backout{_Get_unwrapped_n(_STD move(_First), _Count)};
380+
_Uninitialized_backout _Backout{_STD move(_UFirst)};
370381

371382
for (; 0 < _Count; --_Count) {
372383
_Backout._Emplace_back(_Val);
@@ -497,6 +508,12 @@ namespace ranges {
497508
} else {
498509
return _RANGES next(_OFirst, _STD move(_OLast));
499510
}
511+
} else if constexpr (is_nothrow_default_constructible_v<_Ty>) {
512+
for (; _OFirst != _OLast; ++_OFirst) {
513+
::new (static_cast<void*>(_Unfancy(_OFirst))) _Ty;
514+
}
515+
516+
return _OFirst;
500517
} else {
501518
_Uninitialized_backout _Backout{_STD move(_OFirst)};
502519

@@ -553,6 +570,13 @@ namespace ranges {
553570
if (_Count > 0) {
554571
if constexpr (is_trivially_default_constructible_v<_Ty>) {
555572
_RANGES advance(_First, _Count);
573+
} else if constexpr (is_nothrow_default_constructible_v<_Ty>) {
574+
auto _UFirst = _Get_unwrapped_n(_STD move(_First));
575+
for (; 0 < _Count; ++_UFirst, (void) --_Count) {
576+
::new (static_cast<void*>(_Unfancy(_UFirst))) _Ty;
577+
}
578+
579+
_Seek_wrapped(_First, _STD move(_UFirst));
556580
} else {
557581
_Uninitialized_backout _Backout{_Get_unwrapped_n(_STD move(_First), _Count)};
558582

@@ -654,8 +678,15 @@ namespace ranges {
654678
_STL_INTERNAL_STATIC_ASSERT(_No_throw_sentinel_for<_Se, _It>);
655679
_STL_INTERNAL_STATIC_ASSERT(default_initializable<iter_value_t<_It>>);
656680

681+
using _Ty = iter_value_t<_It>;
657682
if constexpr (_Use_memset_value_construct_v<_It>) {
658683
return _RANGES _Zero_range(_OFirst, _OLast);
684+
} else if constexpr (is_nothrow_default_constructible_v<_Ty>) {
685+
for (; _OFirst != _OLast; ++_OFirst) {
686+
::new (static_cast<void*>(_Unfancy(_OFirst))) _Ty{};
687+
}
688+
689+
return _OFirst;
659690
} else {
660691
_Uninitialized_backout _Backout{_STD move(_OFirst)};
661692

@@ -697,12 +728,20 @@ namespace ranges {
697728
requires default_initializable<iter_value_t<_It>>
698729
_It operator()(_It _First, const iter_difference_t<_It> _Count_raw) const {
699730
// clang-format on
731+
using _Ty = iter_value_t<_It>;
700732
_Algorithm_int_t<iter_difference_t<_It>> _Count = _Count_raw;
701733
if (_Count > 0) {
734+
auto _UFirst = _Get_unwrapped_n(_STD move(_First));
702735
if constexpr (_Use_memset_value_construct_v<_It>) {
703-
return _RANGES _Zero_range(_OFirst, _OFirst + _Count);
736+
_Seek_wrapped(_First, _RANGES _Zero_range(_UFirst, _UFirst + _Count));
737+
} else if constexpr (is_nothrow_default_constructible_v<_Ty>) {
738+
for (; 0 < _Count; ++_UFirst, (void) --_Count) {
739+
::new (static_cast<void*>(_Unfancy(_UFirst))) _Ty{};
740+
}
741+
742+
_Seek_wrapped(_First, _STD move(_UFirst));
704743
} else {
705-
_Uninitialized_backout _Backout{_Get_unwrapped_n(_STD move(_First), _Count)};
744+
_Uninitialized_backout _Backout{_STD move(_UFirst)};
706745

707746
for (; 0 < _Count; --_Count) {
708747
_Backout._Emplace_back();

0 commit comments

Comments
 (0)