Skip to content

Skip cookie lookup work when nothing can match - #2421

Open
pavel-ptashyts wants to merge 1 commit into
AsyncHttpClient:mainfrom
maygemdev:pavel-ptashyts/perf-cookie-empty-jar
Open

pavel-ptashyts wants to merge 1 commit into
AsyncHttpClient:mainfrom
maygemdev:pavel-ptashyts/perf-cookie-empty-jar

Conversation

@pavel-ptashyts

Copy link
Copy Markdown
Contributor

Motivation

The default config installs a ThreadSafeCookieStore (DefaultAsyncHttpClientConfig), and every request goes DefaultAsyncHttpClient.executeRequest -> StoreCookies.rebind -> ThreadSafeCookieStore.get(Uri). Most API clients never receive a cookie, so the jar is empty, yet each lookup still:

  • canonicalised the host (requestDomain),
  • on a plain http URI, ran isSecureContext -> isLoopbackHost, which for an IP literal allocates a byte array and an InetAddress,
  • allocated an ArrayList(4) before the first bucket lookup,
  • took one substring and one map lookup per parent label (DomainUtils.getSubDomain).

An empty jar can never match, and neither can a populated jar with no bucket for the host or its parents. CookieStoreGetBenchmark only 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.get and its private helpers change:

  • get(Uri) returns Collections.emptyList() before any host work when cookieJar.isEmpty().
  • The loop looks up each bucket itself. collectStoredCookies takes 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 add path and StoreCookies.rebind are 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 Secure cookies. A get racing an add may or may not see the new cookie, as before (ConcurrentHashMap.isEmpty and get are both weakly consistent).

Tests

New in ThreadSafeCookieStoreGetTest:

  • anEmptyJarReturnsNothingForAnyHost: a host name, a multi-label host name, an IPv4 literal, localhost and 127.0.0.1, each over http and https where relevant.
  • aClearedJarReturnsNothing.
  • aPopulatedJarReturnsNothingForAnUnrelatedHost: the same hosts against a jar holding a host cookie, a parent-domain cookie, a TLS-only Secure cookie 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=true on Corretto JDK 11.0.33 (Windows) passed on 8940451d6: 2488 tests, 0 failures, 47 skipped, Revapi clean. -Dgpg.skip only skips signing, since there is no signing key here.

Benchmark

CookieStoreGetBenchmark gets two new cases. Both use a plain http URI 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 for www.example.com, one for example.com and one for 198.51.100.7.

The cookiesPerDomain parameter does not apply to the new cases. They ran under both of its values, and the table shows cookiesPerDomain = 1; the 5 rows 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. main and this branch run the same benchmark class; for main, its ThreadSafeCookieStore classes come first on the classpath. The host was running other builds, so times are noisy. B/op is exact.

Benchmark host main ns/op branch ns/op main B/op branch B/op
emptyJar api.eu.service.example.org 101.4 +- 4.2 2.5 +- 2.2 288 0
emptyJar 192.0.2.10 34.5 +- 0.9 2.1 +- 0.1 88 0
unrelatedHost api.eu.service.example.org 107.7 +- 2.9 98.6 +- 2.6 288 232
unrelatedHost 192.0.2.10 34.5 +- 0.7 18.1 +- 0.6 88 0
get (existing), cookiesPerDomain = 1 www.example.com 123.0 +- 2.8 125.4 +- 2.9 392 280
get (existing), cookiesPerDomain = 5 www.example.com 487.9 +- 10.6 490.3 +- 11.3 616 616
  • The empty jar, the common case for an API client on the default store, no longer costs anything measurable.
  • For a populated jar and an unrelated host name, the gain is small: 56 B (the list) and a few ns. The remaining 232 B are the parent-label substrings that ConcurrentHashMap.get needs as String keys. Removing them would mean changing the jar's key type, which this change does not do.
  • The existing matching get case 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.
  • In the IPv4 rows for main, B/op was 88 under cookiesPerDomain = 1 and 64 under 5, which is escape analysis varying between forks.

Attribution

Claude Code on behalf of Pavel Ptashyts

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant