[libcxx-commits] [libcxx] [libc++] Optimize vector::erase (PR #213916)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 12 07:20:35 PDT 2026


================
@@ -552,8 +553,32 @@ 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 constexpr (__is_nothrow_relocatable_v<value_type> &&
----------------
ldionne wrote:

```suggestion
      // We technically only need is_nothrow_relocatable, but vector::erase is overspecified to
      // use assignment. We use is_trivially_relocatable until that is relaxed.
      if constexpr (__is_trivially_relocatable_v<value_type> &&
```

https://github.com/llvm/llvm-project/pull/213916


More information about the libcxx-commits mailing list