Avoid per-poll ConcurrentHashMap KeyIterator allocation in operation managers - #3271
Merged
nicolaslopezbravo merged 1 commit intoJun 22, 2026
Conversation
Each operation manager (Get/Put/Delete/TtlUpdate/Undelete/ReplicateBlob) stores its in-flight operations in a ConcurrentHashMap.newKeySet() and iterates it once per poll() with an enhanced-for loop. The for-each calls .iterator(), which allocates a ConcurrentHashMap$KeyIterator unconditionally -- even when the set is empty -- before hasNext() returns false. With the RequestResponseHandler poll loop spinning every few ms across six managers, that churns a throwaway iterator per manager per poll even when nothing is in flight (the common case on a frontend). Guarding the loop with !set.isEmpty() skips the .iterator() allocation in the empty case. isEmpty() checks the map's count and allocates nothing. Behavior is unchanged: an empty set's for-each is already a no-op, so the guard only removes the wasted allocation; when operations are present the loop runs exactly as before. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
nicolaslopezbravo
force-pushed
the
nlb/router-poll-keyiterator-garbage
branch
from
June 13, 2026 04:58
6379772 to
c7b5b95
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #3271 +/- ##
=============================================
- Coverage 64.24% 50.85% -13.39%
+ Complexity 10398 8680 -1718
=============================================
Files 840 937 +97
Lines 71755 80235 +8480
Branches 8611 9645 +1034
=============================================
- Hits 46099 40804 -5295
- Misses 23004 36038 +13034
- Partials 2652 3393 +741 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
nicolaslopezbravo
requested review from
Qian1900,
abha-mutalik,
beijxu and
crliao
June 13, 2026 05:18
Qian1900
approved these changes
Jun 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Guard the per-
poll()iteration of each operation manager's in-flight operation set withisEmpty(), so the enhanced-for loop does not allocate aConcurrentHashMap$KeyIteratorwhen the set is empty.Why
Each operation manager (
GetManager,PutManager,DeleteManager,TtlUpdateManager,UndeleteManager,ReplicateBlobManager) stores its in-flight operations in aConcurrentHashMap.newKeySet()and iterates it once perpoll()with an enhanced-for loop. The for-each calls.iterator(), andConcurrentHashMap.KeySetView.iterator()allocates aKeyIteratorunconditionally — even for an empty set — beforehasNext()returns false:The
RequestResponseHandlerpoll loop runs continuously across all six managers, so an idle or lightly-loaded router allocates a throwaway iterator per manager per poll. Heap analysis of a busy frontend showed tens of millions of these short-livedConcurrentHashMap$KeyIteratorobjects as the dominant young-gen garbage, inflating GC frequency.Guarding with
!operations.isEmpty()skips the.iterator()call in the empty case;isEmpty()checks the map count and allocates nothing.Behavior
No behavior change — an empty set's for-each is already a no-op, so the guard only removes the wasted allocation. When operations are present the loop runs exactly as before.
Testing Done
./gradlew :ambry-router:compileJava— BUILD SUCCESSFUL../gradlew :ambry-router:test --tests GetManagerTest --tests PutManagerTest --tests DeleteManagerTest --tests TtlUpdateManagerTest --tests UndeleteManagerTest— 201 tests, 201 passed, 0 failed.