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

dongjoon-hyun pushed a commit to branch branch-4.x
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/branch-4.x by this push:
     new 5e959dfd30ae [SPARK-61257][SQL] Align the growing on-heap pages of 
hash join maps with heap regions
5e959dfd30ae is described below

commit 5e959dfd30aeabbd4805fcf2d3787cf82af3d1d8
Author: Dongjoon Hyun <[email protected]>
AuthorDate: Mon Nov 9 15:24:29 2026 -0800

    [SPARK-61257][SQL] Align the growing on-heap pages of hash join maps with 
heap regions
    
    ### What changes were proposed in this pull request?
    
    This PR makes each growing page of `LongToUnsafeRowMap` and of the 
`BytesToBytesMap` in `UnsafeHashedRelation` leave room for the header of the 
`long[]` that backs an on-heap page, when the page size leaves that room.
    
    - `TaskMemoryManager.growingPageHeaderBytes(pageSizeBytes)` returns 
`Platform.LONG_ARRAY_OFFSET` if `pageSizeBytes` is that much less than a power 
of two, and 0 otherwise. Under G1, ZGC and Shenandoah, the default on-heap page 
size is such a size (SPARK-37593).
    - `BytesToBytesMap` counts each growing page together with that header. 
With the default page size of 64 MiB - 16 bytes, the data pages of 
`UnsafeHashedRelation` become 1, 2, 4, 8, 16 and 32 MiB minus 16 bytes each, 
then 1 MiB - 16 for the cut page, and then 64 MiB - 16 as before. With their 
headers, the growing pages still add up to exactly one page. A map whose pages 
do not grow, e.g. for hash aggregation, does not change.
    - `LongToUnsafeRowMap` starts with a page of 1 MiB - 16 bytes and doubles 
it together with its header (2 MiB - 16, 4 MiB - 16, ...). A deserialized map 
still allocates exactly what it reads.
    
    Any other page size, e.g. a power of two set by `spark.buffer.pageSize` or 
the default page size in off-heap mode, gives 0, so nothing changes for it, 
like for its full pages. That includes a broadcast `UnsafeHashedRelation` in 
off-heap mode: it allocates its pages on heap with the off-heap page size of 
`SparkEnv`, so its full pages already lack the room, and this PR leaves that 
path as it is. The page sizes depend only on the page size that the map gets, 
not on the GC or the memory mode.
    
    ### Why are the changes needed?
    
    SPARK-60998 and SPARK-61082 made both maps grow their pages from 1 MiB 
instead of rounding them up to a full page. These growing pages are exact 
powers of two. On heap, `HeapMemoryAllocator` backs each page with a `long[]`, 
whose 16-byte header makes such a page take one more G1 region if it is at 
least as large as a region, or a whole region if it is half a region. Before 
SPARK-61082, `UnsafeHashedRelation` used only pages of the page size, which 
SPARK-37593 reduces by the header.
    
    A broadcast `UnsafeHashedRelation` is on heap, and every executor rebuilds 
its `BytesToBytesMap` with growing pages when it reads the relation and keeps 
it for the lifetime of the broadcast. `LongToUnsafeRowMap` grows its page only 
while it is built, i.e. in a shuffled hash join in on-heap mode and on the 
driver for a broadcast relation. Executors allocate exactly what they read, and 
the driver drops its copy after broadcasting it (SPARK-39983), so its gain is 
limited to that build. H [...]
    
    | | 1m | 2m | 4m | 8m | 16m | 32m |
    |---|---|---|---|---|---|---|
    | `BytesToBytesMap` like `UnsafeHashedRelation` with 40 MiB of data, before 
| 72.0 | 78.0 | 84.7 | 92.0 | 104.4 | 112.8 |
    | after | 66.0 | 67.0 | 67.1 | 65.0 | 65.2 | 66.4 |
    | before SPARK-61082 (one page of 64 MiB - 16) | 67.0 | 68.0 | 68.1 | 64.8 
| 65.6 | 66.4 |
    | `LongToUnsafeRowMap` page of 64 MiB, before | 65.0 | 66.0 | 67.9 | 71.9 | 
79.7 | 95.6 |
    | after (64 MiB - 16) | 64.0 | 64.0 | 63.9 | 63.9 | 63.8 | 63.8 |
    
    The `BytesToBytesMap` rows are 40 real maps of 40,000 records with a 
1016-byte value, built with `lookup` and `append` like `UnsafeHashedRelation` 
and with the page size of a `UnifiedMemoryManager`. They include the 2 MiB 
pointer array of each map. The `LongToUnsafeRowMap` rows allocate its page as 
`HeapMemoryAllocator` does until the heap is exhausted, and JDK 25.0.5 gives 
the same numbers. In both cases the heap is the old and humongous regions after 
a full GC, divided by the number [...]
    
    ### Does this PR introduce _any_ user-facing change?
    
    No. The hash join maps use less heap under G1, ZGC and Shenandoah in 
on-heap mode, and their results do not change.
    
    ### How was this patch tested?
    
    Pass the CIs with the newly added test cases. The map tests pass a page 
size of a power of two minus `Platform.LONG_ARRAY_OFFSET` explicitly, so they 
do not depend on the GC or the memory mode of the test JVM, and they run in 
both the on-heap and the off-heap suites.
    
    - `AbstractBytesToBytesMapSuite.growingDataPagesLeaveRoomForArrayHeader`: 
with a page size of 8 KiB - 16 and an initial page size of 1 KiB, the pages are 
1, 2 and 4 KiB, the cut page 1 KiB, and then 8 KiB, all minus 16 bytes. A map 
whose pages do not grow keeps pages of the page size.
    - `HashedRelationSuite` (`SPARK-60998: LongToUnsafeRowMap does not round 
its page up to the page size`) now also runs with a page size of 64 MiB - 16, 
where a `LongToUnsafeRowMap` starts with 1 MiB - 16 and grows to 2 MiB - 16.
    - `UnifiedMemoryManagerSuite` (`SPARK-61257: growing pages leave room for 
the array header with the default page size`): with the default on-heap page 
size, `TaskMemoryManager.growingPageHeaderBytes` gives 
`Platform.LONG_ARRAY_OFFSET` under G1, ZGC and Shenandoah (SPARK-37593), and 0 
otherwise.
    
    The existing tests that check exact page sizes pass a page size without 
that room, so they do not change.
    
    ```
    build/sbt "core/testOnly org.apache.spark.unsafe.map.* 
org.apache.spark.memory.TaskMemoryManagerSuite 
org.apache.spark.memory.UnifiedMemoryManagerSuite"
    build/sbt "sql/testOnly 
org.apache.spark.sql.execution.joins.HashedRelationOnHeapSuite 
org.apache.spark.sql.execution.joins.HashedRelationOffHeapSuite"
    ```
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    Generated-by: Claude Opus 5.5
    
    Closes #60247 from dongjoon-hyun/SPARK-61257.
    
    Authored-by: Dongjoon Hyun <[email protected]>
    Signed-off-by: Dongjoon Hyun <[email protected]>
    
    (cherry picked from commit 59ca65d963a5544d6af117935d1e0b8d5fc4acbc)
    Signed-off-by: Dongjoon Hyun <[email protected]>
    
    
    skip-checks: true
---
 .../org/apache/spark/memory/TaskMemoryManager.java | 20 ++++++++++
 .../apache/spark/unsafe/map/BytesToBytesMap.java   | 34 ++++++++++++-----
 .../org/apache/spark/memory/MemoryManager.scala    |  2 +
 .../unsafe/map/AbstractBytesToBytesMapSuite.java   | 26 +++++++++++++
 .../spark/memory/UnifiedMemoryManagerSuite.scala   | 13 +++++++
 .../spark/sql/execution/joins/HashedRelation.scala | 18 +++++++--
 .../sql/execution/joins/HashedRelationSuite.scala  | 44 ++++++++++++----------
 7 files changed, 124 insertions(+), 33 deletions(-)

diff --git a/core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java 
b/core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java
index 3f00e425f374..389a36fc99b1 100644
--- a/core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java
+++ b/core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java
@@ -29,6 +29,7 @@ import org.apache.spark.internal.SparkLogger;
 import org.apache.spark.internal.SparkLoggerFactory;
 import org.apache.spark.internal.LogKeys;
 import org.apache.spark.internal.MDC;
+import org.apache.spark.unsafe.Platform;
 import org.apache.spark.unsafe.memory.MemoryAllocator;
 import org.apache.spark.unsafe.memory.MemoryBlock;
 import org.apache.spark.util.Utils;
@@ -618,6 +619,25 @@ public class TaskMemoryManager {
     return memoryManager.pageSizeBytes();
   }
 
+  /**
+   * Returns the bytes that pages growing up to {@code pageSizeBytes} leave 
for the header of the
+   * {@code long[]} that backs an on-heap page: {@link 
Platform#LONG_ARRAY_OFFSET} if
+   * {@code pageSizeBytes} is that much less than a power of two, and 0 
otherwise.
+   * <p>
+   * Under G1, ZGC and Shenandoah, the default on-heap page size leaves room 
for this header, so
+   * that a page fills whole heap regions (SPARK-37593). A page of a power of 
two bytes takes one
+   * more region instead, or a whole region if it is half a region. Growing 
pages that leave the
+   * same room fill whole regions as well. Any other page size gives 0, so the 
growing pages stay
+   * powers of two like the full pages of that size, e.g. a power of two set by
+   * {@code spark.buffer.pageSize} or the default page size in off-heap mode. 
The latter gives 0
+   * even for a map that allocates its pages on heap with it, e.g. a broadcast 
hash join relation,
+   * whose full pages lack the room as well.
+   */
+  public static long growingPageHeaderBytes(long pageSizeBytes) {
+    final long heapBytes = pageSizeBytes + Platform.LONG_ARRAY_OFFSET;
+    return (heapBytes & (heapBytes - 1)) == 0 ? Platform.LONG_ARRAY_OFFSET : 0;
+  }
+
   /**
    * Allocate a block of memory that will be tracked in the MemoryManager's 
page table; this is
    * intended for allocating large blocks of Tungsten memory that will be 
shared between operators.
diff --git 
a/core/src/main/java/org/apache/spark/unsafe/map/BytesToBytesMap.java 
b/core/src/main/java/org/apache/spark/unsafe/map/BytesToBytesMap.java
index 850ff34219c5..c3ffb267d5b7 100644
--- a/core/src/main/java/org/apache/spark/unsafe/map/BytesToBytesMap.java
+++ b/core/src/main/java/org/apache/spark/unsafe/map/BytesToBytesMap.java
@@ -177,20 +177,28 @@ public final class BytesToBytesMap extends MemoryConsumer 
{
   private final long pageSizeBytes;
 
   /**
-   * The size of the first data page, at most {@link #pageSizeBytes}.
+   * The size of the first data page plus {@link #pageHeaderBytes}, at most 
{@link #pageSizeBytes}.
    */
   private final long initialPageSizeBytes;
 
   /**
-   * The size of the next data page while the map grows. It starts at
-   * {@link #initialPageSizeBytes} and doubles with every new page, up to 
{@link #pageSizeBytes}.
+   * The bytes that each growing data page leaves for the header of an on-heap 
page, see
+   * {@link TaskMemoryManager#growingPageHeaderBytes}. 0 if the data pages do 
not grow.
+   */
+  private final long pageHeaderBytes;
+
+  /**
+   * The size of the next data page plus {@link #pageHeaderBytes} while the 
map grows. It starts at
+   * {@link #initialPageSizeBytes} and doubles with every new page, up to 
{@link #pageSizeBytes}
+   * plus {@link #pageHeaderBytes}.
    */
   private long nextPageSizeBytes;
 
   /**
-   * The total size requested for the data pages so far. While it is below
-   * {@link #pageSizeBytes}, a new page is at most what is left to reach it, 
so the growing pages
-   * add up to exactly one page size before the pages of {@link 
#pageSizeBytes} begin.
+   * The total size requested for the data pages so far, plus {@link 
#pageHeaderBytes} for each
+   * of them. While it is below {@link #pageSizeBytes} plus {@link 
#pageHeaderBytes}, a new page is
+   * at most what is left to reach it, so the growing pages add up to exactly 
one page before the
+   * pages of {@link #pageSizeBytes} begin.
    */
   private long requestedDataPagesBytes;
 
@@ -302,6 +310,8 @@ public final class BytesToBytesMap extends MemoryConsumer {
       throw new IllegalArgumentException("Initial page size must be greater 
than 0");
     }
     this.initialPageSizeBytes = Math.min(initialPageSizeBytes, pageSizeBytes);
+    this.pageHeaderBytes = this.initialPageSizeBytes < pageSizeBytes ?
+      TaskMemoryManager.growingPageHeaderBytes(pageSizeBytes) : 0;
     this.nextPageSizeBytes = this.initialPageSizeBytes;
     this.initialCapacity = initialCapacity;
     allocate(initialCapacity);
@@ -312,6 +322,9 @@ public final class BytesToBytesMap extends MemoryConsumer {
    * whose later pages double in size until they add up to `pageSizeBytes`. 
The last of them is cut
    * to what is left, and all pages after them have `pageSizeBytes`. A map 
holding little data then
    * does not reserve a full page. A record larger than the next page gets a 
page of its size.
+   * If `pageSizeBytes` leaves room for the header of an on-heap page, each 
growing page leaves the
+   * same room, and the sizes above include it (see
+   * {@link TaskMemoryManager#growingPageHeaderBytes}).
    */
   public BytesToBytesMap(
       TaskMemoryManager taskMemoryManager,
@@ -1104,9 +1117,10 @@ public final class BytesToBytesMap extends 
MemoryConsumer {
    * @return whether there is enough space to allocate the new page.
    */
   private boolean acquireNewPage(long required) {
-    final long remainingGrowthBytes = pageSizeBytes - requestedDataPagesBytes;
+    final long remainingGrowthBytes = pageSizeBytes + pageHeaderBytes - 
requestedDataPagesBytes;
     final long size = Math.max(
-      remainingGrowthBytes > 0 ? Math.min(nextPageSizeBytes, 
remainingGrowthBytes) : pageSizeBytes,
+      remainingGrowthBytes > 0 ?
+        Math.min(nextPageSizeBytes, remainingGrowthBytes) - pageHeaderBytes : 
pageSizeBytes,
       required);
     final MemoryBlock page;
     try {
@@ -1120,8 +1134,8 @@ public final class BytesToBytesMap extends MemoryConsumer 
{
       freePage(page);
       return false;
     }
-    requestedDataPagesBytes += size;
-    nextPageSizeBytes = Math.min(pageSizeBytes, nextPageSizeBytes * 2);
+    requestedDataPagesBytes += size + pageHeaderBytes;
+    nextPageSizeBytes = Math.min(pageSizeBytes + pageHeaderBytes, 
nextPageSizeBytes * 2);
     currentPage = page;
     dataPages.add(currentPage);
     UnsafeAlignedOffset.putSize(currentPage.getBaseObject(), 
currentPage.getBaseOffset(), 0);
diff --git a/core/src/main/scala/org/apache/spark/memory/MemoryManager.scala 
b/core/src/main/scala/org/apache/spark/memory/MemoryManager.scala
index 88d973756e21..176cb7679261 100644
--- a/core/src/main/scala/org/apache/spark/memory/MemoryManager.scala
+++ b/core/src/main/scala/org/apache/spark/memory/MemoryManager.scala
@@ -259,6 +259,8 @@ private[spark] abstract class MemoryManager(
    * SPARK-37593 If we are using G1GC, ZGC or ShenandoahGC, it's better to 
take the
    * LONG_ARRAY_OFFSET into consideration so that the requested memory size is 
power of 2
    * and can be divided by heap region size to reduce memory waste.
+   * `TaskMemoryManager.growingPageHeaderBytes` recognizes such a page size, 
so that the growing
+   * pages of hash join maps leave the same room.
    */
   private lazy val defaultPageSizeBytes = {
     val minPageSize = 1L * 1024 * 1024   // 1MB
diff --git 
a/core/src/test/java/org/apache/spark/unsafe/map/AbstractBytesToBytesMapSuite.java
 
b/core/src/test/java/org/apache/spark/unsafe/map/AbstractBytesToBytesMapSuite.java
index 6612e4cb6755..e55ce976e77c 100644
--- 
a/core/src/test/java/org/apache/spark/unsafe/map/AbstractBytesToBytesMapSuite.java
+++ 
b/core/src/test/java/org/apache/spark/unsafe/map/AbstractBytesToBytesMapSuite.java
@@ -853,6 +853,32 @@ public abstract class AbstractBytesToBytesMapSuite {
     }
   }
 
+  @Test
+  public void growingDataPagesLeaveRoomForArrayHeader() {
+    // SPARK-61257: Like the default on-heap page size under G1 (SPARK-37593), 
this page size
+    // leaves room for the header of a long[], and so does each growing page. 
With the header, the
+    // pages double from 1 KiB, and the last growing page is cut to the 1 KiB 
left.
+    final long header = Platform.LONG_ARRAY_OFFSET;
+    final BytesToBytesMap map = new BytesToBytesMap(taskMemoryManager, 1, 8192 
- header, 1024);
+    try {
+      assertEquals(1024 - header, appendUntilNewDataPage(map, 1));
+      assertEquals(2048 - header, appendUntilNewDataPage(map, 1));
+      assertEquals(4096 - header, appendUntilNewDataPage(map, 1));
+      assertEquals(1024 - header, appendUntilNewDataPage(map, 1));
+      assertEquals(8192 - header, appendUntilNewDataPage(map, 1));
+    } finally {
+      map.free();
+    }
+
+    // A map whose pages do not grow keeps pages of the page size.
+    final BytesToBytesMap fullPageMap = new BytesToBytesMap(taskMemoryManager, 
1, 8192 - header);
+    try {
+      assertEquals(8192 - header, appendUntilNewDataPage(fullPageMap, 1));
+    } finally {
+      fullPageMap.free();
+    }
+  }
+
   /**
    * Appends records with a value of `valueWords` longs until the map 
allocates a new data page, and
    * returns the size of that page. All records share one key, so the pointer 
array does not grow
diff --git 
a/core/src/test/scala/org/apache/spark/memory/UnifiedMemoryManagerSuite.scala 
b/core/src/test/scala/org/apache/spark/memory/UnifiedMemoryManagerSuite.scala
index 580972b7621b..03645c1875f5 100644
--- 
a/core/src/test/scala/org/apache/spark/memory/UnifiedMemoryManagerSuite.scala
+++ 
b/core/src/test/scala/org/apache/spark/memory/UnifiedMemoryManagerSuite.scala
@@ -24,6 +24,7 @@ import org.apache.spark.internal.config._
 import org.apache.spark.internal.config.Tests._
 import org.apache.spark.storage.TestBlockId
 import org.apache.spark.storage.memory.MemoryStore
+import org.apache.spark.unsafe.Platform
 import org.apache.spark.util.Utils
 
 class UnifiedMemoryManagerSuite extends MemoryManagerSuite with 
PrivateMethodTester {
@@ -380,6 +381,18 @@ class UnifiedMemoryManagerSuite extends MemoryManagerSuite 
with PrivateMethodTes
     UnifiedMemoryManager.shutdownUnmanagedMemoryPoller()
   }
 
+  test("SPARK-61257: growing pages leave room for the array header with the 
default page size") {
+    // Under G1, ZGC and Shenandoah, the default on-heap page size leaves room 
for the header of a
+    // long[] (SPARK-37593), and the growing pages of hash join maps leave the 
same room.
+    val mm = createMemoryManager(1000L, isOffHeapEnabled = false)
+    val expected = if (Utils.isG1GC || Utils.isZGC || Utils.isShenandoahGC) {
+      Platform.LONG_ARRAY_OFFSET.toLong
+    } else {
+      0L
+    }
+    assert(TaskMemoryManager.growingPageHeaderBytes(mm.pageSizeBytes) === 
expected)
+  }
+
   test("unmanaged memory tracking with memory mode separation") {
     val maxMemory = 1000L
     val taskAttemptId = 0L
diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala
 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala
index ae5e3cda25c8..ca0333c93af4 100644
--- 
a/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala
+++ 
b/sql/core/src/main/scala/org/apache/spark/sql/execution/joins/HashedRelation.scala
@@ -458,7 +458,9 @@ private[joins] object UnsafeHashedRelation {
 
   /**
    * The size of the first data page of the map. Later pages double in size 
until they add up to
-   * the page size, and then all pages have the page size.
+   * the page size, and then all pages have the page size. If the page size 
leaves room for the
+   * header of an on-heap page, so does each growing page, and these sizes 
include that header (see
+   * `TaskMemoryManager.growingPageHeaderBytes`).
    *
    * Otherwise `BytesToBytesMap` rounds its first page up to the page size 
(`spark.buffer.pageSize`
    * or a default of up to 64 MiB), so even a few rows reserve a full page. A 
task that keeps many
@@ -581,6 +583,10 @@ private[execution] final class LongToUnsafeRowMap private (
   // Current write cursor in the page.
   private var cursor: Long = -1
 
+  // The bytes that the page leaves for the header of an on-heap page while it 
grows, see
+  // `TaskMemoryManager.growingPageHeaderBytes`.
+  private val pageHeaderBytes = 
TaskMemoryManager.growingPageHeaderBytes(mm.pageSizeBytes())
+
   // The number of bits for size in address
   private val SIZE_BITS = 28
   private val SIZE_MASK = 0xfffffff
@@ -618,7 +624,7 @@ private[execution] final class LongToUnsafeRowMap private (
       array = allocateArray(n * 2)
       array.zeroOut()
       mask = n * 2 - 2
-      page = allocatePage(LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES)
+      page = allocatePage(LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES - 
pageHeaderBytes)
       cursor = page.getBaseOffset
     }
   }
@@ -849,7 +855,9 @@ private[execution] final class LongToUnsafeRowMap private (
       if (neededNumWords > (1 << 30)) {
         throw QueryExecutionErrors.cannotBuildHashedRelationLargerThan8GError()
       }
-      val newNumWords = math.max(neededNumWords, math.min(page.size() / 8 * 2, 
1 << 30))
+      // The page doubles together with its header.
+      val doubledNumWords = ((page.size() + pageHeaderBytes) * 2 - 
pageHeaderBytes) / 8
+      val newNumWords = math.max(neededNumWords, math.min(doubledNumWords, 1 
<< 30))
       // newNumWords is a Long up to 1 << 30. Multiplying by 8 must stay in 
Long
       // arithmetic; `newNumWords.toInt * 8` (Int * Int) overflows to 0 at the
       // upper bound, causing `allocatePage(0)` to return a page of only
@@ -1025,7 +1033,9 @@ private[execution] final class LongToUnsafeRowMap private 
(
 
 private[execution] object LongToUnsafeRowMap {
   /**
-   * The size of the page that a new map starts with. The page grows by 
doubling from here.
+   * The size of the page that a new map starts with. The page grows by 
doubling from here. If the
+   * page size of the `TaskMemoryManager` leaves room for the header of an 
on-heap page, so does the
+   * page, and these sizes include that header (see 
`TaskMemoryManager.growingPageHeaderBytes`).
    */
   val INITIAL_PAGE_SIZE_BYTES: Long = 1L << 20
 
diff --git 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/joins/HashedRelationSuite.scala
 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/joins/HashedRelationSuite.scala
index f81203ed17f9..61d98a573a31 100644
--- 
a/sql/core/src/test/scala/org/apache/spark/sql/execution/joins/HashedRelationSuite.scala
+++ 
b/sql/core/src/test/scala/org/apache/spark/sql/execution/joins/HashedRelationSuite.scala
@@ -36,6 +36,7 @@ import org.apache.spark.sql.catalyst.expressions._
 import org.apache.spark.sql.internal.SQLConf
 import org.apache.spark.sql.test.SharedSparkSession
 import org.apache.spark.sql.types._
+import org.apache.spark.unsafe.Platform
 import org.apache.spark.unsafe.map.BytesToBytesMap
 import org.apache.spark.unsafe.types.UTF8String
 import org.apache.spark.util.ArrayImplicits._
@@ -401,26 +402,31 @@ abstract class HashedRelationSuite extends 
SharedSparkSession {
   }
 
   test("SPARK-60998: LongToUnsafeRowMap does not round its page up to the page 
size") {
-    val largeUmm = newMemoryManager(ByteUnit.MiB.toBytes(64), 
ByteUnit.GiB.toBytes(1))
-    val largeMm = new TaskMemoryManager(largeUmm, 0)
-    assert(largeMm.pageSizeBytes() === ByteUnit.MiB.toBytes(64))
-
-    // Capacity 16 gives an array of 32 longs, which does not grow for a 
single key.
-    val arrayBytes = 32 * 8L
-    val map = new LongToUnsafeRowMap(largeMm, 16)
-    assert(map.getMode === largeMm.getTungstenMemoryMode)
-    assert(map.getUsed === arrayBytes + 
LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES)
+    // SPARK-61257: Like the default on-heap page size under G1 (SPARK-37593), 
a page size that
+    // leaves room for the header of a long[] makes the page leave the same 
room.
+    Seq(0L, Platform.LONG_ARRAY_OFFSET.toLong).foreach { header =>
+      val largeUmm = newMemoryManager(ByteUnit.MiB.toBytes(64) - header, 
ByteUnit.GiB.toBytes(1))
+      val largeMm = new TaskMemoryManager(largeUmm, 0)
+      assert(largeMm.pageSizeBytes() === ByteUnit.MiB.toBytes(64) - header)
+
+      // Capacity 16 gives an array of 32 longs, which does not grow for a 
single key.
+      val arrayBytes = 32 * 8L
+      val map = new LongToUnsafeRowMap(largeMm, 16)
+      assert(map.getMode === largeMm.getTungstenMemoryMode)
+      assert(map.getUsed === arrayBytes + 
LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES - header)
+
+      // A value larger than the initial page doubles the page together with 
its header, to
+      // 2 MiB, not 64 MiB.
+      val unsafeProj = UnsafeProjection.create(Array[DataType](StringType))
+      val bigStr = UTF8String.fromString("x".repeat(3 << 19))
+      map.append(0L, unsafeProj(InternalRow(bigStr)))
+      assert(map.getUsed === arrayBytes + 2 * 
LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES - header)
 
-    // A value larger than the initial page doubles the page to 2 MiB, not 64 
MiB.
-    val unsafeProj = UnsafeProjection.create(Array[DataType](StringType))
-    val bigStr = UTF8String.fromString("x".repeat(3 << 19))
-    map.append(0L, unsafeProj(InternalRow(bigStr)))
-    assert(map.getUsed === arrayBytes + 2 * 
LongToUnsafeRowMap.INITIAL_PAGE_SIZE_BYTES)
-
-    val resultRow = new UnsafeRow(1)
-    assert(map.getValue(0L, resultRow).getUTF8String(0) === bigStr)
-    map.free()
-    assert(largeUmm.executionMemoryUsed === 0)
+      val resultRow = new UnsafeRow(1)
+      assert(map.getValue(0L, resultRow).getUTF8String(0) === bigStr)
+      map.free()
+      assert(largeUmm.executionMemoryUsed === 0)
+    }
   }
 
   test("SPARK-60998: many LongToUnsafeRowMaps fit in one task when each holds 
little data") {


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

Reply via email to