[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:25 PDT 2025
================
@@ -1213,6 +1213,80 @@ private:
__node_pointer __cache_root_;
__node_pointer __cache_elem_;
};
+
+ class __tree_deleter {
+ __node_allocator& __alloc_;
+
+ public:
+ using pointer = __node_pointer;
+
+ _LIBCPP_HIDE_FROM_ABI __tree_deleter(__node_allocator& __alloc) : __alloc_(__alloc) {}
+
+ void operator()(__node_pointer __ptr) {
+ if (!__ptr)
+ return;
+
+ (*this)(static_cast<__node_pointer>(__ptr->__left_));
+
+ auto __right = __ptr->__right_;
+
+ __node_traits::destroy(__alloc_, std::addressof(__ptr->__value_));
+ __node_traits::deallocate(__alloc_, __ptr, 1);
+
+ (*this)(static_cast<__node_pointer>(__right));
+ }
+ };
+
+ _LIBCPP_HIDE_FROM_ABI __node_pointer __copy_construct_tree(__node_pointer __src) {
----------------
ldionne wrote:
We should leave a comment behind that explains why this way of constructing the tree is correct. It basically boils down to the fact that the incoming tree has a proper balanced tree structure and so does the destination tree. So mirroring the incoming tree's structure onto the destination tree should be correct. It's simple but it's important, otherwise one might think that we don't end up with a properly structured r/b tree.
https://github.com/llvm/llvm-project/pull/151304
More information about the libcxx-commits
mailing list