https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118850
Bug ID: 118850 Summary: _Rb_tree emplace unique functions should avoid allocating node when emplacing value_type Product: gcc Version: 15.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: redi at gcc dot gnu.org Target Milestone: --- template<typename... _Args> auto _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>:: _M_emplace_unique(_Args&&... __args) -> pair<iterator, bool> { _Auto_node __z(*this, std::forward<_Args>(__args)...); auto __res = _M_get_insert_unique_pos(__z._M_key()); if (__res.second) return {__z._M_insert(__res), true}; return {iterator(__res.first), false}; } We can avoid using an _Auto_node if sizeof...(_Args) == 1 and remove_cvref_t<_Args> is value_type. We can get the key directly from the value_type and decide whether we need to insert anything. Similarly for _M_emplace_hint_unique For std::map we could also optimize the case where sizeof...(_Args) == 2 and the first arg is the key_type.