[libcxx-commits] [libcxx] [libc++] Optimize copy construction and assignment of __tree (PR #151304)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Wed Jul 30 07:36:26 PDT 2025


================
@@ -1277,11 +1351,28 @@ __tree<_Tp, _Compare, _Allocator>::_DetachedTreeCache::__detach_next(__node_poin
 
 template <class _Tp, class _Compare, class _Allocator>
 __tree<_Tp, _Compare, _Allocator>& __tree<_Tp, _Compare, _Allocator>::operator=(const __tree& __t) {
-  if (this != std::addressof(__t)) {
-    value_comp() = __t.value_comp();
-    __copy_assign_alloc(__t);
-    __assign_multi(__t.begin(), __t.end());
+  if (this == std::addressof(__t))
+    return *this;
+
+  value_comp() = __t.value_comp();
+  __copy_assign_alloc(__t);
+
+  if (__t.size() == 0) {
+    clear();
+    return *this;
+  }
+
+  if (__size_ != 0) {
+    __end_node_.__left_ = static_cast<__node_base_pointer>(__copy_assign_tree(
+        static_cast<__node_pointer>(__end_node_.__left_), static_cast<__node_pointer>(__t.__end_node_.__left_)));
+  } else {
+    __end_node_.__left_ =
+        static_cast<__node_base_pointer>(__copy_construct_tree(static_cast<__node_pointer>(__t.__end_node_.__left_)));
+    __end_node_.__left_->__parent_ = __end_node();
   }
+  __begin_node_ = static_cast<__end_node_pointer>(std::__tree_min(static_cast<__node_base_pointer>(__end_node())));
+  __size_       = __t.__size_;
----------------
ldionne wrote:

```suggestion
  __size_       = __t.size();
```

Maybe? For consistency.

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


More information about the libcxx-commits mailing list