bvolpato opened a new pull request, #41741:
URL: https://github.com/apache/beam/pull/41741

   Reduce per-element heap allocations, repeated window hashing, and redundant 
size estimation in `PrecombineGroupingTable`.
   
   For some context, `PrecombineGroupingTable` runs on every input element of 
lifted combines in the Java SDK harness. Previously, each `put()` allocated a 
key wrapper (`GloballyWindowedTableGroupingKey` or `WindowedGroupingTableKey`) 
and a capturing lambda for `lruMap.compute()`. Windowed lookups also re-hashed 
and iterated the window collection on every hit, `GroupingTableEntry`'s 
constructor called `sizer.estimateSize(accumulator)` twice on insertion, and 
`output()` re-weighed compacted accumulators right before discarding the entry.
   
   What changed:
   
   - For globally windowed tables, key `lruMap` directly by `structuralKey` and 
reuse a single `BiFunction` instance so `put()` does one map lookup with no 
wrapper or lambda allocation on cache hits, clearing the temporary `pending*` 
fields in a `finally` block.
   - For windowed tables, probe `lruMap` with a reusable mutable 
`WindowedGroupingTableKey` (cleared in `finally`, allocating a stored key only 
on cache misses), precompute key hash codes, cache the last-seen window 
collection's hash code, and fast-path indexed `List` window equality without 
allocating iterators (falling back to `Iterables.elementsEqual` for non-`List` 
collections).
   - Make `GroupingTableEntry` `static` and merge key weights into a single 
field (48 B vs 56 B), drop the redundant weight field from 
`WindowedGroupingTableKey` (24 B vs 32 B), and remove the now-unused 
`GloballyWindowedTableGroupingKey` wrapper.
   - Remove the duplicate `sizer.estimateSize(accumulator)` call in 
`GroupingTableEntry`'s constructor and skip re-weighing accumulators during 
`output()` compaction when entries are immediately emitted and removed.
   
   Why this is safe:
   
   - `PrecombineGroupingTable` is already `@NotThreadSafe` and strictly 
single-threaded per bundle for `put()` and `flush()`. Both the reusable 
`pending*` fields and the mutable `lookupKey` probe are cleared in `finally` on 
every `put()` so large inputs or evicted keys are never retained between calls 
or on exceptions, and `lookupKey` is never inserted into `lruMap`.
   - Windowed cache misses still weigh the stored `WindowedGroupingTableKey` 
via `sizer.estimateSize(groupingKey)` (preserving JAMM memory accounting for 
keys and windows), while globally windowed misses weigh `structuralKey` 
directly since no wrapper object is stored.
   - Memory weight tracking during eviction and flush remains consistent 
because entries compacted for `output()` are immediately removed from `lruMap`.
   
   Benchmarks (JMH `PrecombineGroupingTableBenchmark.sumIntegerBinaryCombine`, 
`-f 2 -wi 4 -i 4 -w 1s -r 1s -t 16 -prof gc`, `-Xmx4g` with JAMM agent, OpenJDK 
21.0.11, AMD Ryzen 9 9950X3D, matched back-to-back against `master` 
`1c20e381bc8`):
   
   | Distribution | Globally Windowed | Before (ops/s) | After (ops/s) | 
Speedup | Alloc Before (MB/op) | Alloc After (MB/op) | Alloc Delta |
   | --- | :---: | ---: | ---: | ---: | ---: | ---: | ---: |
   | `uniform` | `true` | 350.3 ± 14.8 | 504.1 ± 21.7 | 1.44x | 64.52 | 16.55 | 
-74.4% |
   | `uniform` | `false` | 324.5 ± 10.1 | 456.9 ± 33.9 | 1.41x | 72.52 | 16.98 
| -76.6% |
   | `normal` | `true` | 254.5 ± 32.8 | 366.9 ± 20.1 | 1.44x | 65.79 | 17.76 | 
-73.0% |
   | `normal` | `false` | 234.7 ± 28.1 | 344.0 ± 14.9 | 1.47x | 73.79 | 18.27 | 
-75.2% |
   | `hotKey` | `true` | 359.7 ± 9.7 | 510.6 ± 51.5 | 1.42x | 64.52 | 16.55 | 
-74.3% |
   | `hotKey` | `false` | 320.9 ± 14.8 | 462.9 ± 39.5 | 1.44x | 72.52 | 17.02 | 
-76.5% |
   | `uniqueKeys` | `true` | 77.6 ± 3.5 | 86.0 ± 14.4 | 1.11x | 249.31 | 193.27 
| -22.5% |
   | `uniqueKeys` | `false` | 74.9 ± 1.6 | 75.9 ± 11.4 | 1.01x | 257.31 | 
219.03 | -14.9% |
   
   Validation:
   
   Verified locally against `PrecombineGroupingTableTest` and 
`CombineRunnersTest` (including new coverage for global-window key weight 
eviction, windowed grouping across distinct `List` and `Set` window 
collections, and verifying key/accumulator sizing counts on insert, hit, and 
output compaction), plus Spotless, Checkstyle, and SpotBugs on 
`:sdks:java:harness`. Full `:javaPreCommit` and runner post-commit suites were 
left for CI.
   


-- 
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]

Reply via email to