diff --git a/src/util.rs b/src/util.rs index 61b91b22c..cd4cc2d64 100644 --- a/src/util.rs +++ b/src/util.rs @@ -535,17 +535,12 @@ impl RcSliceBuilder { // Normalize the allocation size to a power of 2 or the halfway point // between to powers of 2. - let leading_zeros = size.leading_zeros(); - let pow2 = (1usize << (usize::BITS - 1)) >> leading_zeros; - let size = if size == pow2 { - size + let next_pow2 = size.next_power_of_two(); + let size = if next_pow2 <= 2048 { + next_pow2 } else { - let halfway = (3usize << (usize::BITS - 2)) >> leading_zeros; - if size > halfway { - pow2 << 1 - } else { - halfway - } + let gap = next_pow2 / 16 - 1; + (size + gap) & !gap }; (Layout::from_size_align(size, align).unwrap(), offset) }