[libcxx-commits] [libcxx] Optimize std::__tree::__assign_multi to insert the provided range at the end of the tree every time (PR #131030)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Mar 12 13:59:23 PDT 2025


https://github.com/higher-performance created https://github.com/llvm/llvm-project/pull/131030

This improves performance for the copy-assignment operators of associative containers such as `std::map`.

Note that this optimization already exists in other places, e.g.:

https://github.com/llvm/llvm-project/blob/15e6bb6224177805d8b6a8268f08a2b88ae4dd70/libcxx/include/map#L1217-L1218

>From 4f12629a16ca358ade8c25deb3f60f944fa2ba59 Mon Sep 17 00:00:00 2001
From: higher-performance <higher.performance.github at gmail.com>
Date: Wed, 12 Mar 2025 16:53:10 -0400
Subject: [PATCH] Optimize std::__tree::__assign_multi to insert the provided
 range at the end of the tree every time

This improves performance for the copy-assignment operators of associative containers such as std::map
---
 libcxx/include/__tree | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index c627641d5d86f..c07de5c95b0dc 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -1432,8 +1432,9 @@ void __tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _
       __cache.__advance();
     }
   }
+  const_iterator __e = end();
   for (; __first != __last; ++__first)
-    __insert_multi(_NodeTypes::__get_value(*__first));
+    __insert_multi(__e, _NodeTypes::__get_value(*__first));
 }
 
 template <class _Tp, class _Compare, class _Allocator>



More information about the libcxx-commits mailing list