[ADT] Fix the initial size calculation of SmallDenseMap (#158458)
The initial size calculation of SmallDenseMap is strange in several
ways:
- SmallDenseMap(unsigned) seems to want to take the number of initial
buckets as far as I can tell from the variable name NumInitBuckets.
In contrast, DenseMap(unsigned) seems to want to take the number of
initial entries as far as I can tell from the comment:
/// Create a DenseMap with an optional \p InitialReserve that guarantee
that
/// this number of elements can be inserted in the map without grow()
- SmallDenseMap(unsigned) uses llvm::bit_ceil to obtain a power of
two. SmallDenseMap(I, E) uses NextPowerOf2 to obtain a power of
two.
- Presumably, the init() call is to ensure that we won't call grow()
while populating the initial elements [I, E). However,
NextPowerOf2(std::distance(I, E)) does not ensure that a rehash
won't happen. For example, if the number of initial elements is
50, we need 128 buckets, but NextPowerOf2(std::distance(I, E)) would
return 64.
This patch fixes all these inconsistencies by teaching
SmallDenseMap::init to call BaseT::getMinBucketToReserveForEntries
just like DenseMap::init.
With this patch, all constructors of SmallDenseMap are textually
identical to their respective counterparts in DenseMap.