-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[libc++] Optimize multi{map,set}::insert(InputIterator, InputIterator) #152691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -970,6 +970,35 @@ public: | |
__emplace_hint_multi(__p, std::move(__value)); | ||
} | ||
|
||
template <class _InIter, class _Sent> | ||
_LIBCPP_HIDE_FROM_ABI void __insert_range_multi(_InIter __first, _Sent __last) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also suggest taking a tag here and handling the (very similar) unique/multi cases uniformly with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It turns out that adding a unique implementation will be a lot more complex, since we have a lot more complex handling in |
||
if (__first == __last) | ||
return; | ||
|
||
if (__root() == nullptr) { // Make sure we always have a root node | ||
__node_holder __node = __construct_node(*__first); | ||
__insert_node_at(__end_node(), __end_node()->__left_, static_cast<__node_base_pointer>(__node.release())); | ||
++__first; | ||
} | ||
|
||
auto __max_node = static_cast<__node_pointer>(std::__tree_max(static_cast<__node_base_pointer>(__root()))); | ||
|
||
for (; __first != __last; ++__first) { | ||
__node_holder __node = __construct_node(*__first); | ||
// Always check the max node first. This optimizes for sorted ranges inserted at the end. | ||
if (!value_comp()(__node->__get_value(), __max_node->__get_value())) { // __node >= __max_val | ||
__insert_node_at(static_cast<__end_node_pointer>(__max_node), | ||
__max_node->__right_, | ||
static_cast<__node_base_pointer>(__node.get())); | ||
__max_node = __node.release(); | ||
} else { | ||
__end_node_pointer __parent; | ||
__node_base_pointer& __child = __find_leaf_high(__parent, __node->__value_); | ||
__insert_node_at(__parent, __child, static_cast<__node_base_pointer>(__node.release())); | ||
} | ||
} | ||
} | ||
|
||
_LIBCPP_HIDE_FROM_ABI pair<iterator, bool> __node_assign_unique(const value_type& __v, __node_pointer __dest); | ||
|
||
_LIBCPP_HIDE_FROM_ABI iterator __node_insert_multi(__node_pointer __nd); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's document what this function does and in particular what optimizations it performs (i.e. it is optimized for already-sorted inputs).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What sort of documentation do you want? I'm not sure there is much to say beyond "inserts the range [first, last) into *this", which I think is rather obvious.