Repository navigation
Skip cookie lookup work when nothing can match - #2421
Open
pavel-ptashyts wants to merge 1 commit into
Open
pavel-ptashyts wants to merge 1 commit into
pavel-ptashyts wants to merge 1 commit into
Conversation
The default config installs a ThreadSafeCookieStore and every request asks it for cookies, but most API clients never receive one. On an empty jar each lookup still canonicalised the host, on plain http parsed it as an address for the loopback check (a byte array and an InetAddress for an IP literal), allocated a result list and walked every parent label. Return the empty list before any host work when the jar is empty, allocate the result list only when a bucket yields a match, and resolve the secure context only once a bucket for the host or a parent exists. Returned cookies and their order are unchanged. Claude Code on behalf of Pavel Ptashyts Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.
Motivation
The default config installs a
ThreadSafeCookieStore(DefaultAsyncHttpClientConfig), and every request goesDefaultAsyncHttpClient.executeRequest->StoreCookies.rebind->ThreadSafeCookieStore.get(Uri). Most API clients never receive a cookie, so the jar is empty, yet each lookup still:requestDomain),httpURI, ranisSecureContext->isLoopbackHost, which for an IP literal allocates a byte array and anInetAddress,ArrayList(4)before the first bucket lookup,DomainUtils.getSubDomain).An empty jar can never match, and neither can a populated jar with no bucket for the host or its parents.
CookieStoreGetBenchmarkonly measured a populated jar with matching cookies, so this path was not measured.This supersedes #2261, which only added the empty-jar return and was closed without review.
Modification
Only
ThreadSafeCookieStore.getand its private helpers change:get(Uri)returnsCollections.emptyList()before any host work whencookieJar.isEmpty().collectStoredCookiestakes the bucket and allocates the result list on the first match, so a bucket that yields nothing allocates nothing.isSecureContext(uri, domain)runs once, when the first bucket for the host or a parent is found. It is a pure function of the URI and host. Before this change it was computed up front, and its result is used only while scanning a bucket and in the final sort, which needs at least one bucket. So every use still sees the same value.The
addpath andStoreCookies.rebindare untouched. No public API change.What is preserved
The cookies returned and their order: secure provenance first on a secure request, the host's own bucket before its parents, longer paths first, host-only versus parent-domain cookies, IP-literal hosts not walking parent labels, expiry, and the TLS and loopback rules for
Securecookies. Agetracing anaddmay or may not see the new cookie, as before (ConcurrentHashMap.isEmptyandgetare both weakly consistent).Tests
New in
ThreadSafeCookieStoreGetTest:anEmptyJarReturnsNothingForAnyHost: a host name, a multi-label host name, an IPv4 literal,localhostand127.0.0.1, each overhttpandhttpswhere relevant.aClearedJarReturnsNothing.aPopulatedJarReturnsNothingForAnUnrelatedHost: the same hosts against a jar holding a host cookie, a parent-domain cookie, a TLS-onlySecurecookie and an IP cookie. It also checks that the jar's own hosts still get the same cookies.aBucketWithNoMatchReturnsNothing: a bucket whose cookies fail the path or host-only test returns nothing.The existing cookie tests pass unchanged:
PublicSuffixCookieTest,SecureCookieEvictionTest,SecureCookieSchemeTest,ThreadSafeCookieStoreExpiryTest,ThreadSafeCookieStoreGetTest,CookieStoreTest,CrossHostReplayCookieTest.mvnw.cmd clean verify -Dgpg.skip=trueon Corretto JDK 11.0.33 (Windows) passed on8940451d6: 2488 tests, 0 failures, 47 skipped, Revapi clean.-Dgpg.skiponly skips signing, since there is no signing key here.Benchmark
CookieStoreGetBenchmarkgets two new cases. Both use a plainhttpURI for a multi-label host name (api.eu.service.example.org) and an IPv4 literal (192.0.2.10):emptyJar: nothing stored.unrelatedHost: the jar holds five cookies forwww.example.com, one forexample.comand one for198.51.100.7.The
cookiesPerDomainparameter does not apply to the new cases. They ran under both of its values, and the table showscookiesPerDomain = 1; the5rows agree.Setup: JMH 1.37, Corretto JDK 11.0.33, Windows, 1 fork, 5 x 1 s warmup, 5 x 1 s measurement,
-prof gc,-Xmx256m.mainand this branch run the same benchmark class; formain, itsThreadSafeCookieStoreclasses come first on the classpath. The host was running other builds, so times are noisy. B/op is exact.ConcurrentHashMap.getneeds asStringkeys. Removing them would mean changing the jar's key type, which this change does not do.getcase is unchanged in time. Its 112 B drop at one cookie per domain is not claimed as a gain: this change does not remove an allocation on that path, so the drop is probably escape analysis differing between the two builds.main, B/op was 88 undercookiesPerDomain = 1and 64 under5, which is escape analysis varying between forks.Attribution
Claude Code on behalf of Pavel Ptashyts
🤖 Generated with Claude Code