Skip to content

[Java] Reduce PrecombineGroupingTable allocations and redundant sizing - #41741

Open
bvolpato wants to merge 1 commit into
apache:masterfrom
bvolpato:perf/precombine-grouping-table
Open

bvolpato wants to merge 1 commit into
apache:masterfrom
bvolpato:perf/precombine-grouping-table

Conversation

@bvolpato

Copy link
Copy Markdown
Contributor

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.

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.
@github-actions github-actions Bot added java core size/l L: 250-999 lines changed labels Nov 29, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 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 structuralKey directly with a reusable BiFunction) and windowed lookups (using a reusable lookupKey probe) cleanly avoids unnecessary heap allocations on the hot path.
  • Merging groupingKeyWeight into GroupingTableEntry.keyWeight and skipping sizer.estimateSize(accumulator) in compactForOutput preserves accurate cache weight accounting while eliminating duplicate JAMM traversals.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @kennknowles for label java.
Backup reviewer: chamikaramj (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

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 assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant