Increase chunkSize to next power of 2 and at least 8
@@ -30,12 +30,17 @@ public class SpscArrayQueueUtil {
@NotNull
public static <E> Queue<E> create(int capacity, int chunkSize) {
+ chunkSize = nextPowerOf2(chunkSize);
+ chunkSize = Math.max(8, chunkSize);
if (capacity <= chunkSize) {
return new SpscArrayQueue<>(capacity);
}
- capacity = Math.max(16, capacity);
- chunkSize = Math.min(chunkSize, capacity / 2);
+ capacity = nextPowerOf2(capacity); // capacity is at least 9, so next power of two is at least 16
return new SpscChunkedArrayQueue<>(chunkSize, capacity);
}
+ private static int nextPowerOf2(final int value) {
+ return 1 << 32 - Integer.numberOfLeadingZeros(value - 1);
+ }
+
}
