boostorg / container

STL-like containers from Boost
http://www.boost.org/libs/container/
Boost Software License 1.0
101 stars 113 forks source link

Remove useless allocator copy in map #49

Closed joker-eph closed 7 years ago

joker-eph commented 7 years ago

I never contributed to boost, I'm not sure what kind of test I should write for this? For now, I ran successfully b2 in boost/libs/container/test with this patch applied.

This patch adds a variant of map constructors to avoid useless extra allocator copy when using initializer list

Many existing constructors have this form:

map(std::initializer_list il, const Compare& comp = Compare(), const allocator_type& a = allocator_type())

The issue is that a temporary allocator_type is constructed, and passed to the base class where it is used to copy-constructed the rebound allocator. This temporary allocator_type here is always destroyed at the end of the map() constructor. For stateful allocators this is not desirable. The solution is to adopt what libc++ is doing and have to constructors:

map(std::initializer_list il, const Compare& comp = Compare(), const allocator_type& a)

and

map(std::initializer_list il, const Compare& comp = Compare())

This way, unless an allocator is provided by the client, no extra temporary creation/destruction occurs.

igaztanaga commented 7 years ago

Thanks for the patch. I applied it and added some additional changes after reviewing all associative (tree-based and flat_tree based) containers.