In the constructor of unordered_map
, we can define the allocated bucket count. I had thought I can use to reduce rehashing times. However, this could also hurt performance at some case. The rehash happens at insert when
Rehashing occurs only if the new number of elements is greater than
max_load_factor()*bucket_count()
. If the insertion is successful, pointers and references to the element obtained while it is held in the node handle are invalidated, and pointers and references obtained to that element before it was extracted become valid. (since C++17)
The above doc is from std::unordered_map
. I guess boost is similar? But its doc does not state the condition of rehashing.
If I initialize the bucket count to 100, and there is a bucket that contains all 100 elements, then rehashing would not happen until the 101 element is inserted... If I use the default bucket count, I assume it is << 100, rehashing can happen much earlier.
If so, when do we want to initialize bucket count?