dongjoon-hyun opened a new pull request, #60247: URL: https://github.com/apache/spark/pull/60247
### 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. an off-heap page size or a power of two set by `spark.buffer.pageSize`, gives 0, so nothing changes for it, like for its full pages. 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. The pages of a broadcast relation are on heap, on the driver and on every executor. Heap per relation in MiB on JDK 21.0.13 with G1 and `-Xmx8g`, by region size (G1 picks 4 MiB for an 8 GiB heap, 8 MiB for 16 GiB, 16 MiB for 32 GiB and 32 MiB for 64 GiB or more): | | 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 of maps. ### 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. They 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-61257: LongToUnsafeRowMap leaves room for the array header when the page size does`): with a page size of 64 MiB - 16, a `LongToUnsafeRowMap` starts with 1 MiB - 16 and grows to 2 MiB - 16. 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" 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 -- 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]
