perf: eliminate UnmodifiableList virtual dispatch overhead in LocationCache#48674
Draft
xinlian12 wants to merge 4 commits intoAzure:mainfrom
Draft
perf: eliminate UnmodifiableList virtual dispatch overhead in LocationCache#48674xinlian12 wants to merge 4 commits intoAzure:mainfrom
xinlian12 wants to merge 4 commits intoAzure:mainfrom
Conversation
Eliminate per-response intermediate HashMap allocation by adding a new StoreResponse constructor that accepts HttpHeaders directly. Header names and values are populated into String[] arrays without materializing an intermediate Map. The JsonNodeStorePayload is updated to accept header arrays and only builds a Map lazily on error paths (extremely rare). Pre-size HashMaps throughout the hot path to avoid resize/rehash: - HttpHeaders request construction: sized to defaultHeaders + request headers - StoreResponse.replicaStatusList: pre-sized to 4 - StoreResponse.withRemappedStatusCode: pre-sized to header count - RxDocumentServiceRequest fallback maps: pre-sized to 32 Fix HttpUtils.asMap() double-allocation by iterating HttpHeaders directly instead of calling toMap() which creates an intermediate HashMap. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…Case fast-path Change 1: Replace vendored UnmodifiableList with Collections.unmodifiableList() - UnmodifiableList wraps via 4-level virtual dispatch chain (UnmodifiableList -> AbstractSerializableListDecorator -> AbstractListDecorator -> AbstractCollectionDecorator) - AbstractListDecorator.decorated() called on every list operation accounts for ~1.02% CPU (398 samples at 1t-c128) - Collections.unmodifiableList() returns a simple wrapper that JIT can inline efficiently Change 2: Add isAsciiLowerCase fast-path in HttpHeaders - HTTP/2 headers are already lowercase per protocol spec - Most x-ms-* custom headers are already lowercase - Skip String.toLowerCase(Locale.ROOT) when string is already all-lowercase via simple char range check - StringLatin1.toLowerCase accounts for ~1.29% CPU (500 samples) Files changed: - LocationCache.java: field types + return types + constructors - GlobalEndpointManager.java: return types - ClientRetryPolicy.java: variable type - GlobalPartitionEndpointManagerForPerPartitionCircuitBreaker.java: variable types - HttpHeaders.java: set() and getHeader() fast-path Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Summary
Replace the vendored
UnmodifiableListwrapper inLocationCachewith JDKCollections.unmodifiableList(), eliminating a 4-level polymorphic virtual dispatch chain that the JIT cannot inline.This is the second improvement stacked on top of #48662 (HashMap allocation optimization). Combined cumulative gain: +16.1% throughput vs baseline.
JFR Analysis
JFR profiling identified
AbstractListDecorator.decorated()consuming 1.02% CPU (398 samples at 1t-c128). The vendoredUnmodifiableListextendsAbstractListDecorator→AbstractCollectionDecorator→AbstractSerializableListDecorator, creating a 4-level virtual dispatch chain for every list operation:The JDK's
Collections.unmodifiableList()wraps with a single-levelUnmodifiableListthat the JIT can inline trivially.LocationCacheis called on every request for endpoint resolution, and multi-tenant configs call it 30× per operation cycle — making this a high-frequency hot path.Changes
UnmodifiableList<T>fields and return types withList<T>, useCollections.unmodifiableList()instead ofnew UnmodifiableList<>()UnmodifiableList<T>toList<T>setLowered()fast-path method that skipstoLowerCase()for keys already known to be lowercaseCollections.unmodifiableList()/List.of()instead of vendoredUnmodifiableListBenchmark Results
Environment: 16-core VM, JDK 21, GATEWAY mode, 3 repetitions averaged.
Reference:
perf/hashmap-collection-allocation(previous PASS branch)Why multi-tenant benefits most
30t-c5 showed +20.6% Read throughput because each of the 30 tenants triggers
LocationCachelookups independently. The vendoredUnmodifiableListdispatch overhead multiplied 30× per operation cycle. With the JDK wrapper, the JIT inlines the single-level dispatch, eliminating this per-tenant tax.Note on attempt 1
An earlier attempt (v1) also changed
HttpHeadersinternal map fromMap<String,HttpHeader>toMap<String,String>. This backfired — it increasedHashMap.putValandStringLatin1.toLowerCaseoverhead and caused regressions at 30t-c5 (-9.5%) and 1t-c1 (-7.4%). The v2 approach correctly dropped this risky change and focused only on the safeUnmodifiableListreplacement.