8203279: Faster rounding up to nearest power of two

Reviewed-by: martin, redestad
This commit is contained in:
Ivan Gerasimov 2018-05-21 12:49:03 -07:00
parent 088dbd5ce8
commit ee601ac627
3 changed files with 3 additions and 18 deletions

View file

@ -376,12 +376,7 @@ public class HashMap<K,V> extends AbstractMap<K,V>
* Returns a power of two size for the given target capacity.
*/
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
int n = -1 >>> Integer.numberOfLeadingZeros(cap - 1);
return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}