Repository navigation
Conversation
Eliminate per-element key wrapper and lambda allocations, repeated window hashing, and redundant size estimation in PrecombineGroupingTable.put() and flush(): - Key lruMap directly by structuralKey for globally windowed tables with a reusable BiFunction so put() performs a single map lookup with no wrapper or lambda allocation on hits, clearing temporary pending fields in finally. - Probe lruMap with a reusable mutable WindowedGroupingTableKey for windowed tables (cleared in finally, allocating a stored key only on misses), precompute key hashCode, cache the last-seen windows hashCode, and fast-path indexed List window equality without allocating iterators. - Make GroupingTableEntry static and merge key weights into one field (48 B vs 56 B), drop the redundant weight field from WindowedGroupingTableKey (24 B vs 32 B), and remove the 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 discarded. Matched JMH PrecombineGroupingTableBenchmark (-f 2 -wi 4 -i 4 -t 16 -prof gc): - Key-hit workloads (uniform, normal, hotKey): 1.41x-1.47x throughput, -73.0% to -76.6% allocations (64.5-73.8 MB/op -> 16.5-18.3 MB/op). - uniqueKeys: 1.01x-1.11x throughput, -14.9% to -22.5% allocations.
There was a problem hiding this comment.
📋 Review Summary
This PR significantly optimizes PrecombineGroupingTable by eliminating per-element wrapper and lambda allocations on cache hits, caching window hash codes, fast-pathing indexed List window comparisons, and removing redundant size estimations during entry creation and output compaction. The implementation is well-reasoned, respects the single-threaded bundle processing contract (@NotThreadSafe), properly cleans up temporary references in finally blocks, and includes thorough unit test coverage.
🔍 General Feedback
- The separation of globally windowed lookups (using
structuralKeydirectly with a reusableBiFunction) and windowed lookups (using a reusablelookupKeyprobe) cleanly avoids unnecessary heap allocations on the hot path. - Merging
groupingKeyWeightintoGroupingTableEntry.keyWeightand skippingsizer.estimateSize(accumulator)incompactForOutputpreserves accurate cache weight accounting while eliminating duplicate JAMM traversals.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @kennknowles for label java. This pull request likely touches a core component ("core" label). Please review with scrutiny. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Reduce per-element heap allocations, repeated window hashing, and redundant size estimation in
PrecombineGroupingTable.For some context,
PrecombineGroupingTableruns on every input element of lifted combines in the Java SDK harness. Previously, eachput()allocated a key wrapper (GloballyWindowedTableGroupingKeyorWindowedGroupingTableKey) and a capturing lambda forlruMap.compute(). Windowed lookups also re-hashed and iterated the window collection on every hit,GroupingTableEntry's constructor calledsizer.estimateSize(accumulator)twice on insertion, andoutput()re-weighed compacted accumulators right before discarding the entry.What changed:
lruMapdirectly bystructuralKeyand reuse a singleBiFunctioninstance soput()does one map lookup with no wrapper or lambda allocation on cache hits, clearing the temporarypending*fields in afinallyblock.lruMapwith a reusable mutableWindowedGroupingTableKey(cleared infinally, allocating a stored key only on cache misses), precompute key hash codes, cache the last-seen window collection's hash code, and fast-path indexedListwindow equality without allocating iterators (falling back toIterables.elementsEqualfor non-Listcollections).GroupingTableEntrystaticand merge key weights into a single field (48 B vs 56 B), drop the redundant weight field fromWindowedGroupingTableKey(24 B vs 32 B), and remove the now-unusedGloballyWindowedTableGroupingKeywrapper.sizer.estimateSize(accumulator)call inGroupingTableEntry's constructor and skip re-weighing accumulators duringoutput()compaction when entries are immediately emitted and removed.Why this is safe:
PrecombineGroupingTableis already@NotThreadSafeand strictly single-threaded per bundle forput()andflush(). Both the reusablepending*fields and the mutablelookupKeyprobe are cleared infinallyon everyput()so large inputs or evicted keys are never retained between calls or on exceptions, andlookupKeyis never inserted intolruMap.WindowedGroupingTableKeyviasizer.estimateSize(groupingKey)(preserving JAMM memory accounting for keys and windows), while globally windowed misses weighstructuralKeydirectly since no wrapper object is stored.output()are immediately removed fromlruMap.Benchmarks (JMH
PrecombineGroupingTableBenchmark.sumIntegerBinaryCombine,-f 2 -wi 4 -i 4 -w 1s -r 1s -t 16 -prof gc,-Xmx4gwith JAMM agent, OpenJDK 21.0.11, AMD Ryzen 9 9950X3D, matched back-to-back againstmaster1c20e381bc8):uniformtrueuniformfalsenormaltruenormalfalsehotKeytruehotKeyfalseuniqueKeystrueuniqueKeysfalseValidation:
Verified locally against
PrecombineGroupingTableTestandCombineRunnersTest(including new coverage for global-window key weight eviction, windowed grouping across distinctListandSetwindow collections, and verifying key/accumulator sizing counts on insert, hit, and output compaction), plus Spotless, Checkstyle, and SpotBugs on:sdks:java:harness. Full:javaPreCommitand runner post-commit suites were left for CI.