[libcxx-commits] [libcxx] [libc++] Optimize __tree::__erase_unique (PR #152370)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Aug 11 12:53:39 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

<details>
<summary>Changes</summary>

```
-----------------------------------------------------------------------------------
Benchmark                                                          old          new
-----------------------------------------------------------------------------------
std::map<int, int>::erase(key) (existent)/0                    24.6 ns      24.5 ns
std::map<int, int>::erase(key) (existent)/32                   42.6 ns      31.5 ns
std::map<int, int>::erase(key) (existent)/1024                 68.7 ns      53.2 ns
std::map<int, int>::erase(key) (existent)/8192                 88.7 ns      60.0 ns
std::map<int, int>::erase(key) (non-existent)/0               0.321 ns     0.318 ns
std::map<int, int>::erase(key) (non-existent)/32               8.89 ns      4.63 ns
std::map<int, int>::erase(key) (non-existent)/1024             26.3 ns      11.2 ns
std::map<int, int>::erase(key) (non-existent)/8192             36.1 ns      16.7 ns
std::map<std::string, int>::erase(key) (existent)/0            76.9 ns      89.8 ns
std::map<std::string, int>::erase(key) (existent)/32            167 ns      94.1 ns
std::map<std::string, int>::erase(key) (existent)/1024          198 ns       100 ns
std::map<std::string, int>::erase(key) (existent)/8192          236 ns       157 ns
std::map<std::string, int>::erase(key) (non-existent)/0       0.749 ns     0.310 ns
std::map<std::string, int>::erase(key) (non-existent)/32       43.0 ns      41.2 ns
std::map<std::string, int>::erase(key) (non-existent)/1024      119 ns      96.4 ns
std::map<std::string, int>::erase(key) (non-existent)/8192      189 ns       135 ns
```



---
Full diff: https://github.com/llvm/llvm-project/pull/152370.diff


1 Files Affected:

- (modified) libcxx/include/__tree (+4-3) 


``````````diff
diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index 6ca1a623536f2..1a3d4afa2d8fc 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -2062,10 +2062,11 @@ template <class _Tp, class _Compare, class _Allocator>
 template <class _Key>
 typename __tree<_Tp, _Compare, _Allocator>::size_type
 __tree<_Tp, _Compare, _Allocator>::__erase_unique(const _Key& __k) {
-  iterator __i = find(__k);
-  if (__i == end())
+  __end_node_pointer __parent;
+  __node_base_pointer __i = __find_equal(__parent, __k);
+  if (__i == nullptr)
     return 0;
-  erase(__i);
+  erase(iterator(static_cast<__node_pointer>(__i)));
   return 1;
 }
 

``````````

</details>


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


More information about the libcxx-commits mailing list