Closed user202729 closed 2 months ago
Hi, beginning with Boost 1.85, Boost.Unordered has an optimization by which emplace(key, value)
does not construct any intermediate object when the type of key
is key_type
(or a const reference to that etc.) Does this address your issue?
As explained in https://github.com/boostorg/unordered/issues/241 , if the object is movable, the current implementation of emplace() first construct the key-value pair on the stack, then move it to the heap if the key did not exist.
This usually improve the performance, but it's not always the case.
sizeof(key)+sizeof(value)
↓ \ Key already exists →move()
is better (saves a new/delete pair)move()
is negligibly better (cost of new/delete is negligible compared to actually constructing the pair)move()
is expensive)Suggestion: if
sizeof(key)+sizeof(value)
is larger than a certain threshold (say, 60 bytes — often, at this rate the compiler's optimizer give up and the key/value actually touch the memory instead of staying in registers? Or let the user configure this?) then switch to always allocate on the heap.What do you think?