[libcxx-commits] [libcxx] [libc++] Optimize vector::erase (PR #213916)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Aug 24 07:36:43 PDT 2026
================
@@ -552,8 +552,39 @@ class _LIBCPP_WARN_UNUSED vector {
}
#endif
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __position);
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last);
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __position) {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __position != end(), "vector::erase(iterator) called with a non-dereferenceable iterator");
+ return erase(__position, __position + 1);
+ }
+
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator erase(const_iterator __first, const_iterator __last) {
+ _LIBCPP_ASSERT_VALID_INPUT_RANGE(__first <= __last, "vector::erase(first, last) called with invalid range");
+ pointer __pos = this->__layout_.__begin_ptr() + (__first - begin());
+ auto __hole_size = __last - __first;
+ if (__first != __last) {
+#ifndef _LIBCPP_CXX03_LANG
+ // If the value_type is trivially relocatable, we destroy the range being erased and we relocate the tail
+ // of the vector into the created gap. Otherwise, we use the standard technique with move-assignments.
+ //
+ // We really only need is_nothrow_reocatable, but vector::erase is currently overspecified to use assignment.
----------------
ldionne wrote:
```suggestion
// We really only need is_nothrow_relocatable, but vector::erase is currently overspecified to use assignment.
```
https://github.com/llvm/llvm-project/pull/213916
More information about the libcxx-commits
mailing list