Skip to content

Optimize Fn Harness Caches - #41723

Open
reuvenlax wants to merge 1 commit into
apache:masterfrom
reuvenlax:perf/fn-harness-caches
Open

reuvenlax wants to merge 1 commit into
apache:masterfrom
reuvenlax:perf/fn-harness-caches

Conversation

@reuvenlax

Copy link
Copy Markdown
Contributor

@Abacn I split off the Caches change, so you can test it in isolation.

@reuvenlax
reuvenlax requested a review from Abacn November 27, 2026 19:12
@github-actions github-actions Bot added java core size/xl XL: >=1000 lines changed labels Nov 27, 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 Caches and ClearableCache in the Java SDK Harness by introducing fast-path object weighing, lazy weight evaluation for CompositeKey and CompositeKeyPrefix on the lookup path, precomputed key hash codes, and reference-counted concurrent key tracking for ClearableCache. The changes are well-architected, thoroughly tested for concurrency/eviction edge cases, and accompanied by comprehensive JMH benchmarks.

🔍 General Feedback

  • Lookup & Weighing Fast Paths: Deferring CompositeKey and CompositeKeyPrefix weight calculations until cache insertion and adding exact-class fast paths in Caches.weigh avoid expensive MemoryMeter.measureDeep reflection on cache hits and common immutable types.
  • Hash Distribution: Including Objects.hashCode(key) in CompositeKey.hashCode() fixes a major hash collision bottleneck where all keys within the same sub-cache namespace previously hashed to the same bucket.
  • ClearableCache Correctness & Concurrency: Replacing the non-thread-safe WeakHashMap with the epoch-aware, reference-counted KeyTracker and ShrinkOnEviction Dekker checks properly handles nested sub-caches, evictions, and concurrent clear() operations without leaking tracked keys or resurrecting stale entries.

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

@reuvenlax

Copy link
Copy Markdown
Contributor Author

@Abacn Also the hacky COMPACT_STRINGS codepath wasn't there for performance, it was to better estimate string sizes (in some cases today we can overestimate a string size by 2x in the cache). Since the impact is unclear on real pipelines, I'm removing this code for now and replacing with a comment.

@reuvenlax
reuvenlax force-pushed the perf/fn-harness-caches branch from d9fff73 to 7cdfee1 Compare November 27, 2026 19:30
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