viirya commented on code in PR #60247:
URL: https://github.com/apache/spark/pull/60247#discussion_r4516745413


##########
core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java:
##########
@@ -618,6 +619,23 @@ public long pageSizeBytes() {
     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, e.g. an 
off-heap page size or a
+   * power of two set by {@code spark.buffer.pageSize}, gives 0, so the 
growing pages stay powers
+   * of two like the pages of that size.
+   */
+  public static long growingPageHeaderBytes(long pageSizeBytes) {

Review Comment:
   The new tests all pass a `2^k - 16` page size explicitly, so nothing ties 
this check to `MemoryManager.defaultPageSizeBytes`. If that stops subtracting 
the header, this silently returns 0 and every test still passes. Could we 
assert that a default on-heap `UnifiedMemoryManager` gets `LONG_ARRAY_OFFSET` 
from this under G1/ZGC/Shenandoah, or at least add a cross-reference at 
`defaultPageSizeBytes`?



##########
core/src/main/java/org/apache/spark/memory/TaskMemoryManager.java:
##########
@@ -618,6 +619,23 @@ public long pageSizeBytes() {
     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, e.g. an 
off-heap page size or a

Review Comment:
   With `spark.memory.offHeap.enabled=true`, a broadcast `UnsafeHashedRelation` 
is still on heap (`read()` and the default `mm` in `HashedRelation.apply` are 
on-heap), but its page size comes from `SparkEnv.get.memoryManager`, which is a 
plain power of two in that mode. These on-heap pages get 0 here and keep the 
extra region, while `LongToUnsafeRowMap` on the same path uses its own on-heap 
`mm` and does leave the room.
   
   Is that intended? Using the map's own TMM isn't a drop-in fix, because 
`read()` builds it from `new SparkConf()` and wouldn't see 
`spark.buffer.pageSize`. If this is out of scope, could the description and 
this javadoc say so? "an off-heap page size" reads as if such a page were never 
on heap.



##########
core/src/main/java/org/apache/spark/unsafe/map/BytesToBytesMap.java:
##########
@@ -1120,7 +1133,7 @@ private boolean acquireNewPage(long required) {
       freePage(page);
       return false;
     }
-    requestedDataPagesBytes += size;
+    requestedDataPagesBytes += size + pageHeaderBytes;
     nextPageSizeBytes = Math.min(pageSizeBytes, nextPageSizeBytes * 2);

Review Comment:
   nit: `nextPageSizeBytes` now includes the header, but its cap doesn't. The 
result is the same because of the cut to `remainingGrowthBytes`, but 
`pageSizeBytes + pageHeaderBytes` would keep the units consistent.



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/joins/HashedRelationSuite.scala:
##########
@@ -423,6 +424,28 @@ abstract class HashedRelationSuite extends 
SharedSparkSession {
     assert(largeUmm.executionMemoryUsed === 0)
   }
 
+  test("SPARK-61257: LongToUnsafeRowMap leaves room for the array header when 
the page size does") {

Review Comment:
   nit: this is nearly the SPARK-60998 test above with `- header` added. Could 
we fold the two into one test over both page sizes?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to