Boost logo

Boost :

Subject: Re: [boost] [interprocess][multi_index] emplace() is not sufficient for maps
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2009-09-25 05:27:08


Howard Hinnant skrev:
> On Sep 24, 2009, at 4:26 PM, Ion Gaztañaga wrote:
>
>> Howard Hinnant escribió:
>>
>>> This is effectively the same thing as:
>>> typedef ... map;
>>> map m;
>>> map::node_ptr p = m.remove(m.emplace_hint(m.begin(),
>>> my_special_cheap_key_value, mv1, mv2));
>>> p->first = modify( p->second );
>>> m.insert( boost::move(p) );
>>
>> Removing something just after inserting it seems a bit useless. Even
>> if we insert in the first position we need to do a comparison to make
>> sure the hint is correct.Why not:
>>
>> map::node_ptr p = m.create_node(my_special_cheap_key_value, mv1, mv2);
>> //We would need to unconst-cast...
>> const_cast<Key&>(p->first) = modify( p->second );
>> m.insert( boost::move(p) );

FWIW, I like this. My only worry is about the const_cast: is it not
undefined behavior to do that here (since the .first member is
constructed as a const object)?

> One could do that but the node allocation and construction is typically
> the expensive part of the insertion. Once you've got that done, and if
> especially if your hint is correct, the insertion is very fast. So it
> seems to me like m.create_node would be interface aimed at a very rare
> use case and for an optimization that is going to be quite minor
> (percent-wise), perhaps not even measurable.

In my case it is not possible to provide a hint fot the new node. But
you are right, that emplace/remove and create_node might not be that
different in performace. I find create_node slightly more to the point.

-Thorsten


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk