This is an automated email from the ASF dual-hosted git repository.

Jackie-Jiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pinot.git


The following commit(s) were added to refs/heads/master by this push:
     new 6b18c926e7f Retune the bulk primitive-array thresholds to 96 values 
for reads and 256 for writes (#20249)
6b18c926e7f is described below

commit 6b18c926e7f34473ababf660aee8d86dfdfa544c
Author: Xiaotian (Jackie) Jiang <[email protected]>
AuthorDate: Mon Nov 16 16:30:41 2026 -0800

    Retune the bulk primitive-array thresholds to 96 values for reads and 256 
for writes (#20249)
---
 .../PercentileIntermediateResultSerDeTest.java     | 11 +++--
 .../perf/BenchmarkPercentileSerializedMerge.java   |  2 +-
 .../perf/BenchmarkPrimitiveListDeserialize.java    |  2 +-
 .../segment/spi/memory/PagedPinotOutputStream.java | 51 ++++++++++++----------
 .../pinot/segment/spi/memory/PrimitiveArrays.java  | 28 ++++++------
 .../spi/memory/PagedPinotOutputStreamTest.java     |  5 ++-
 .../segment/spi/memory/PrimitiveArraysTest.java    |  5 ++-
 7 files changed, 56 insertions(+), 48 deletions(-)

diff --git 
a/pinot-core/src/test/java/org/apache/pinot/core/query/aggregation/function/PercentileIntermediateResultSerDeTest.java
 
b/pinot-core/src/test/java/org/apache/pinot/core/query/aggregation/function/PercentileIntermediateResultSerDeTest.java
index 2834651e751..0b6176c4250 100644
--- 
a/pinot-core/src/test/java/org/apache/pinot/core/query/aggregation/function/PercentileIntermediateResultSerDeTest.java
+++ 
b/pinot-core/src/test/java/org/apache/pinot/core/query/aggregation/function/PercentileIntermediateResultSerDeTest.java
@@ -35,6 +35,7 @@ import 
org.apache.pinot.core.common.datablock.DataBlockBuilder;
 import org.apache.pinot.core.common.datatable.DataTableBuilder;
 import org.apache.pinot.core.common.datatable.DataTableBuilderFactory;
 import org.apache.pinot.segment.spi.memory.PagedPinotOutputStream;
+import org.apache.pinot.segment.spi.memory.PrimitiveArrays;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
@@ -108,9 +109,9 @@ public class PercentileIntermediateResultSerDeTest {
         // Fills the exact capacity, then grows past it.
         {new DoubleArrayList(new double[]{1, 2}), sequence(1)},
         {sequence(1000), sequence(3000)},
-        // Both sides of PrimitiveArrays.MIN_BULK_VALUES: read one value at a 
time, then in bulk.
-        {sequence(7), sequence(63)},
-        {sequence(7), sequence(64)},
+        // Both sides of PrimitiveArrays.MIN_BULK_READ_VALUES: read one value 
at a time, then in bulk.
+        {sequence(7), sequence(PrimitiveArrays.MIN_BULK_READ_VALUES - 1)},
+        {sequence(7), sequence(PrimitiveArrays.MIN_BULK_READ_VALUES)},
         {sequence(2), new DoubleArrayList(new 
double[]{Double.longBitsToDouble(0x7ff8_0000_0000_0123L), -0.0})}
     };
   }
@@ -143,9 +144,7 @@ public class PercentileIntermediateResultSerDeTest {
     // A NaN with a payload keeps its raw bits.
     DoubleArrayList specialValues = new DoubleArrayList(
         new double[]{Double.NaN, 
Double.longBitsToDouble(0x7ff8_0000_0000_0123L), -0.0, 
Double.NEGATIVE_INFINITY});
-    // Both sides of PrimitiveArrays.MIN_BULK_VALUES (63 and 64) switch from 
per-value puts to the bulk view.
-    for (DoubleArrayList values : List.of(sequence(0), sequence(1), 
sequence(63), sequence(64), sequence(1000),
-        sequence(5000), specialValues)) {
+    for (DoubleArrayList values : List.of(sequence(0), sequence(1), 
sequence(1000), sequence(5000), specialValues)) {
       AggregationFunction.SerializedResult expected = 
_byteArrayFunction.serializeIntermediateResult(values);
       AggregationFunction.SerializedResult actual = 
_function.serializeIntermediateResult(values);
 
diff --git 
a/pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkPercentileSerializedMerge.java
 
b/pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkPercentileSerializedMerge.java
index 166245024e8..3d73b0a0451 100644
--- 
a/pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkPercentileSerializedMerge.java
+++ 
b/pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkPercentileSerializedMerge.java
@@ -45,7 +45,7 @@ import org.openjdk.jmh.runner.options.OptionsBuilder;
 /// already holds `size` values:
 /// - `deserializeAndAddAll`: reads the incoming list one value at a time, 
then calls `merge` (`addAll`).
 /// - `mergeSerialized`: 
[PercentileAggregationFunction#mergeSerializedIntermediateResult], which reads 
the values
-///   straight into the merged list (in bulk from 
PrimitiveArrays.MIN_BULK_VALUES values up).
+///   straight into the merged list (in bulk from 
PrimitiveArrays.MIN_BULK_READ_VALUES values up).
 ///
 /// Each operation first truncates the merged list back to `size` values, so 
it never reallocates.
 @BenchmarkMode(Mode.AverageTime)
diff --git 
a/pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkPrimitiveListDeserialize.java
 
b/pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkPrimitiveListDeserialize.java
index 388562bca16..b53be3b3710 100644
--- 
a/pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkPrimitiveListDeserialize.java
+++ 
b/pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkPrimitiveListDeserialize.java
@@ -43,7 +43,7 @@ import org.openjdk.jmh.runner.options.OptionsBuilder;
 
 /// Compares reading a serialized primitive array list one value at a time 
(`perValue`, the loop the list SerDes used
 /// before) with the ObjectSerDeUtils list SerDes (`bulk`), from a heap or a 
direct buffer. The SerDes read in bulk from
-/// PrimitiveArrays.MIN_BULK_VALUES values up, and one value at a time below 
it.
+/// PrimitiveArrays.MIN_BULK_READ_VALUES values up, and one value at a time 
below it.
 @BenchmarkMode(Mode.Throughput)
 @OutputTimeUnit(TimeUnit.SECONDS)
 @Fork(3)
diff --git 
a/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PagedPinotOutputStream.java
 
b/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PagedPinotOutputStream.java
index 407a38c8d63..784e1d7e20d 100644
--- 
a/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PagedPinotOutputStream.java
+++ 
b/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PagedPinotOutputStream.java
@@ -18,6 +18,7 @@
  */
 package org.apache.pinot.segment.spi.memory;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import java.io.IOException;
 import java.nio.ByteBuffer;
@@ -41,6 +42,14 @@ import javax.annotation.concurrent.NotThreadSafe;
 /// as one [DataBuffer].
 @NotThreadSafe
 public class PagedPinotOutputStream extends PinotOutputStream {
+  /// [#writeInts] and [#writeFloats] copy at least this many values that go 
to one page through a typed view of the
+  /// page, and put fewer one at a time. With JMH on JDK 25.0.5, writing into 
heap and direct pages, the copy at 256
+  /// values was 2 to 11% faster than one value at a time on an Intel Xeon 
6973P-C, within 5% either way on an
+  /// AMD EPYC 7763, at most 5% slower on a Neoverse N2, and 8 to 11% slower 
for ints and 4 to 5% faster for floats on
+  /// an Apple M3 Max.
+  @VisibleForTesting
+  static final int MIN_BULK_WRITE_VALUES = 256;
+
   private final PageAllocator _allocator;
   private final int _pageSize;
   private final ArrayList<ByteBuffer> _pages;
@@ -214,9 +223,9 @@ public class PagedPinotOutputStream extends 
PinotOutputStream {
   }
 
   /// Puts as many values as fit in the rest of the current page straight into 
it, then continues on the next page.
-  /// When at least [PrimitiveArrays#MIN_BULK_VALUES] values go to one page, 
they are copied through a big-endian typed
-  /// view of it; fewer values are put one at a time, since the copy through a 
view has a fixed cost. The position
-  /// depends on everything written before, so a page boundary can fall inside 
a value: such a value is written with
+  /// When at least [#MIN_BULK_WRITE_VALUES] values go to one page, they are 
copied through a big-endian typed view of
+  /// it; fewer values are put one at a time, since the copy through a view 
has a fixed cost. The position depends on
+  /// everything written before, so a page boundary can fall inside a value: 
such a value is written with
   /// [#writeInt(int)], which splits it across the two pages.
   @Override
   public void writeInts(int[] values, int offset, int length)
@@ -229,7 +238,7 @@ public class PagedPinotOutputStream extends 
PinotOutputStream {
         writeInt(values[i++]);
         continue;
       }
-      if (numValues < PrimitiveArrays.MIN_BULK_VALUES) {
+      if (numValues < MIN_BULK_WRITE_VALUES) {
         int position = _offsetInPage;
         for (int j = i; j < i + numValues; j++, position += Integer.BYTES) {
           _currentPage.putInt(position, values[j]);
@@ -242,7 +251,10 @@ public class PagedPinotOutputStream extends 
PinotOutputStream {
     }
   }
 
-  /// Same as [#writeInts(int[], int, int)], for longs, with 
[#writeLong(long)] for a value split across two pages.
+  /// Same as [#writeInts(int[], int, int)], for longs, with 
[#writeLong(long)] for a value split across two pages,
+  /// except that every value is put one at a time: with JMH on JDK 25.0.5, a 
copy through a typed view of a heap or
+  /// direct page was slower at every length measured, up to 4096 longs or 
doubles, on Intel Xeon 6973P-C, AMD EPYC 7763
+  /// and Apple M3 Max machines. On a Neoverse N2 it was slower for longs, and 
6 to 20% faster from 1024 doubles up.
   @Override
   public void writeLongs(long[] values, int offset, int length)
       throws IOException {
@@ -254,20 +266,17 @@ public class PagedPinotOutputStream extends 
PinotOutputStream {
         writeLong(values[i++]);
         continue;
       }
-      if (numValues < PrimitiveArrays.MIN_BULK_VALUES) {
-        int position = _offsetInPage;
-        for (int j = i; j < i + numValues; j++, position += Long.BYTES) {
-          _currentPage.putLong(position, values[j]);
-        }
-      } else {
-        currentPageView().asLongBuffer().put(values, i, numValues);
+      int position = _offsetInPage;
+      for (int j = i; j < i + numValues; j++, position += Long.BYTES) {
+        _currentPage.putLong(position, values[j]);
       }
       i += numValues;
       advance(numValues * Long.BYTES);
     }
   }
 
-  /// Same as [#writeInts(int[], int, int)], for floats written as raw bits.
+  /// Same as [#writeInts(int[], int, int)], for floats written as raw bits, 
copied through a typed view from
+  /// [#MIN_BULK_WRITE_VALUES] values in one page up.
   @Override
   public void writeFloats(float[] values, int offset, int length)
       throws IOException {
@@ -280,7 +289,7 @@ public class PagedPinotOutputStream extends 
PinotOutputStream {
         writeInt(Float.floatToRawIntBits(values[i++]));
         continue;
       }
-      if (numValues < PrimitiveArrays.MIN_BULK_VALUES) {
+      if (numValues < MIN_BULK_WRITE_VALUES) {
         int position = _offsetInPage;
         for (int j = i; j < i + numValues; j++, position += Float.BYTES) {
           _currentPage.putInt(position, Float.floatToRawIntBits(values[j]));
@@ -293,7 +302,7 @@ public class PagedPinotOutputStream extends 
PinotOutputStream {
     }
   }
 
-  /// Same as [#writeInts(int[], int, int)], for doubles written as raw bits.
+  /// Same as [#writeLongs(long[], int, int)], for doubles written as raw bits.
   @Override
   public void writeDoubles(double[] values, int offset, int length)
       throws IOException {
@@ -302,17 +311,13 @@ public class PagedPinotOutputStream extends 
PinotOutputStream {
     while (i < end) {
       int numValues = Math.min(end - i, remainingInPage() / Double.BYTES);
       if (numValues == 0) {
-        // Raw bits, like the typed view (and ByteBuffer#putDouble): 
writeDouble() would canonicalize NaN.
+        // Raw bits, like ByteBuffer#putDouble: writeDouble() would 
canonicalize NaN.
         writeLong(Double.doubleToRawLongBits(values[i++]));
         continue;
       }
-      if (numValues < PrimitiveArrays.MIN_BULK_VALUES) {
-        int position = _offsetInPage;
-        for (int j = i; j < i + numValues; j++, position += Double.BYTES) {
-          _currentPage.putLong(position, 
Double.doubleToRawLongBits(values[j]));
-        }
-      } else {
-        currentPageView().asDoubleBuffer().put(values, i, numValues);
+      int position = _offsetInPage;
+      for (int j = i; j < i + numValues; j++, position += Double.BYTES) {
+        _currentPage.putLong(position, Double.doubleToRawLongBits(values[j]));
       }
       i += numValues;
       advance(numValues * Double.BYTES);
diff --git 
a/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PrimitiveArrays.java
 
b/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PrimitiveArrays.java
index 1880e10251a..8053330a185 100644
--- 
a/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PrimitiveArrays.java
+++ 
b/pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/PrimitiveArrays.java
@@ -21,24 +21,26 @@ package org.apache.pinot.segment.spi.memory;
 import java.nio.ByteBuffer;
 
 
-/// Bulk reads of primitive arrays from a [ByteBuffer] through its typed 
views, and the threshold below which one
-/// access per value beats a view. The class is stateless and thread-safe.
+/// Bulk reads of primitive arrays from a [ByteBuffer] through its typed 
views. The class is stateless and thread-safe.
+///
+/// On a little-endian CPU, a copy through a big-endian view 
(`asIntBuffer().get(int[])` and the like) swaps the bytes
+/// in a native call with a fixed cost of 20 to 40 ns, so a short read gets 
one value at a time instead. With JMH on
+/// JDK 25.0.5, reading from heap and direct buffers held in a field (as a 
deserializer gets them), the view was
+/// 1 to 16% faster than one value at a time at 96 longs or doubles on Intel 
Xeon 6973P-C, AMD EPYC 7763 and
+/// Neoverse N2 machines, and 39 to 46% slower on an Apple M3 Max, where it 
caught up at about 160. For ints and
+/// floats, one value at a time took several times as long in some JVM 
launches as in others on those three machines,
+/// so no threshold suits every launch; on the M3 the view caught up at 96 to 
128.
 public final class PrimitiveArrays {
   private PrimitiveArrays() {
   }
 
-  // TODO: Retune this threshold. JMH runs on aarch64 (Apple M3 Max, JDK 25) 
put the crossover at about 110-200 values
-  //   for reads, 300-500 for int writes and beyond 4096 for double writes, so 
a single threshold may not fit them all.
-  /// Below this number of values, reads and writes through a big-endian typed 
view (`asIntBuffer().get(int[])` and
-  /// the like) are slower than one access per value. On a little-endian CPU 
the bulk copy swaps the bytes in a native
-  /// call with a fixed cost of about 10-40 ns, while a single-value access 
compiles to an inlined byte swap. Measured
-  /// with BenchmarkPrimitiveListDeserialize and 
BenchmarkPagedPinotOutputStream on aarch64 and x86_64.
-  public static final int MIN_BULK_VALUES = 64;
+  /// The `read*` methods read at least this many values through a typed view, 
and fewer one at a time.
+  public static final int MIN_BULK_READ_VALUES = 96;
 
   /// Reads `length` ints from the current position of `buffer`, in its byte 
order, into `values` from `offset`,
   /// and advances the position past them.
   public static void readInts(ByteBuffer buffer, int[] values, int offset, int 
length) {
-    if (length < MIN_BULK_VALUES) {
+    if (length < MIN_BULK_READ_VALUES) {
       for (int i = offset; i < offset + length; i++) {
         values[i] = buffer.getInt();
       }
@@ -51,7 +53,7 @@ public final class PrimitiveArrays {
   /// Reads `length` longs from the current position of `buffer`, in its byte 
order, into `values` from `offset`,
   /// and advances the position past them.
   public static void readLongs(ByteBuffer buffer, long[] values, int offset, 
int length) {
-    if (length < MIN_BULK_VALUES) {
+    if (length < MIN_BULK_READ_VALUES) {
       for (int i = offset; i < offset + length; i++) {
         values[i] = buffer.getLong();
       }
@@ -64,7 +66,7 @@ public final class PrimitiveArrays {
   /// Reads `length` floats from the current position of `buffer`, in its byte 
order, into `values` from `offset`,
   /// and advances the position past them.
   public static void readFloats(ByteBuffer buffer, float[] values, int offset, 
int length) {
-    if (length < MIN_BULK_VALUES) {
+    if (length < MIN_BULK_READ_VALUES) {
       for (int i = offset; i < offset + length; i++) {
         values[i] = buffer.getFloat();
       }
@@ -77,7 +79,7 @@ public final class PrimitiveArrays {
   /// Reads `length` doubles from the current position of `buffer`, in its 
byte order, into `values` from `offset`,
   /// and advances the position past them.
   public static void readDoubles(ByteBuffer buffer, double[] values, int 
offset, int length) {
-    if (length < MIN_BULK_VALUES) {
+    if (length < MIN_BULK_READ_VALUES) {
       for (int i = offset; i < offset + length; i++) {
         values[i] = buffer.getDouble();
       }
diff --git 
a/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/memory/PagedPinotOutputStreamTest.java
 
b/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/memory/PagedPinotOutputStreamTest.java
index c7f52010b8e..5af2e1be69b 100644
--- 
a/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/memory/PagedPinotOutputStreamTest.java
+++ 
b/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/memory/PagedPinotOutputStreamTest.java
@@ -480,9 +480,10 @@ public class PagedPinotOutputStreamTest {
     int[] pageSizes = {3, 7, 13, 100, 
PagedPinotOutputStream.PageAllocator.MIN_RECOMMENDED_PAGE_SIZE};
     // Bytes written before the values, so the values start at different 
offsets in a page.
     int[] prefixes = {0, 1, 5};
-    // Lengths on both sides of PrimitiveArrays.MIN_BULK_VALUES (64), where 
the per-value path takes over.
+    // Lengths on both sides of PagedPinotOutputStream.MIN_BULK_WRITE_VALUES, 
where the per-value path takes over.
     int[][] ranges = {
-        {0, 0}, {7, 0}, {0, NUM_BULK_VALUES}, {3, NUM_BULK_VALUES - 10}, 
{NUM_BULK_VALUES - 1, 1}, {9, 63}, {9, 64}
+        {0, 0}, {7, 0}, {0, NUM_BULK_VALUES}, {3, NUM_BULK_VALUES - 10}, 
{NUM_BULK_VALUES - 1, 1},
+        {9, PagedPinotOutputStream.MIN_BULK_WRITE_VALUES - 1}, {9, 
PagedPinotOutputStream.MIN_BULK_WRITE_VALUES}
     };
     List<Object[]> cases = new ArrayList<>();
     for (BulkType type : BulkType.values()) {
diff --git 
a/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/memory/PrimitiveArraysTest.java
 
b/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/memory/PrimitiveArraysTest.java
index 333c353b693..cd5ee895e6b 100644
--- 
a/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/memory/PrimitiveArraysTest.java
+++ 
b/pinot-segment-spi/src/test/java/org/apache/pinot/segment/spi/memory/PrimitiveArraysTest.java
@@ -27,11 +27,12 @@ import java.util.Random;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
+import static 
org.apache.pinot.segment.spi.memory.PrimitiveArrays.MIN_BULK_READ_VALUES;
 import static org.testng.Assert.assertEquals;
 
 
 /// Checks that the bulk reads read the same values as one [ByteBuffer] get 
per value, from heap and direct buffers of
-/// either byte order, on both sides of [PrimitiveArrays#MIN_BULK_VALUES], 
into a range of the target array.
+/// either byte order, on both sides of 
[PrimitiveArrays#MIN_BULK_READ_VALUES], into a range of the target array.
 public class PrimitiveArraysTest {
   private static final int NUM_VALUES = 200;
   /// Bytes before the values in the buffer, so they are not aligned to their 
width.
@@ -44,7 +45,7 @@ public class PrimitiveArraysTest {
     List<Object[]> cases = new ArrayList<>();
     for (boolean direct : new boolean[]{false, true}) {
       for (ByteOrder order : new ByteOrder[]{ByteOrder.BIG_ENDIAN, 
ByteOrder.LITTLE_ENDIAN}) {
-        for (int length : new int[]{0, 1, 63, 64, NUM_VALUES}) {
+        for (int length : new int[]{0, 1, MIN_BULK_READ_VALUES - 1, 
MIN_BULK_READ_VALUES, NUM_VALUES}) {
           cases.add(new Object[]{direct, order, length});
         }
       }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to