[libcxx-commits] [libcxx] [libc++] Use relocation in vector::emplace_back (PR #159365)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 17 09:57:35 PDT 2025


================
@@ -1188,19 +1188,43 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 inline
     void
 #endif
     vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
-  pointer __end = this->__end_;
-  std::__if_likely_else(
-      __end < this->__cap_,
-      [&] {
-        __emplace_back_assume_capacity(std::forward<_Args>(__args)...);
-        ++__end;
-      },
-      [&] { __end = __emplace_back_slow_path(std::forward<_Args>(__args)...); });
-
-  this->__end_ = __end;
+  if constexpr (__libcpp_is_trivially_relocatable<value_type>::value &&
+                __allocator_has_trivial_move_construct_v<allocator_type, value_type> &&
+                __allocator_has_trivial_destroy_v<allocator_type, value_type>) {
+    union _Tmp {
----------------
ldionne wrote:

I would like to have a comment explaining why things are written exactly this way. There has been a lot of thought put into the exact placement of everything here, and we should leave traces so that future refactorings don't break the codegen.

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


More information about the libcxx-commits mailing list