From 8940451d6527a38e82a21468087e50c94f5be9a3 Mon Sep 17 00:00:00 2001 From: Pavel Ptashyts <49400901+pavel-ptashyts@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:36:38 +0100 Subject: [PATCH] Skip cookie lookup work when nothing can match 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 --- .../bench/CookieStoreGetBenchmark.java | 39 +++++++++++++ .../cookie/ThreadSafeCookieStore.java | 56 +++++++++++-------- .../cookie/ThreadSafeCookieStoreGetTest.java | 54 ++++++++++++++++++ 3 files changed, 127 insertions(+), 22 deletions(-) diff --git a/client/src/jmh/java/org/asynchttpclient/bench/CookieStoreGetBenchmark.java b/client/src/jmh/java/org/asynchttpclient/bench/CookieStoreGetBenchmark.java index be77d4564..5b25b9614 100644 --- a/client/src/jmh/java/org/asynchttpclient/bench/CookieStoreGetBenchmark.java +++ b/client/src/jmh/java/org/asynchttpclient/bench/CookieStoreGetBenchmark.java @@ -76,4 +76,43 @@ public void setup() { public List get() { return store.get(requestUri); } + + /** + * A request whose host has no cookie: either nothing was ever stored, the common case for an API + * client on the default store, or the jar holds only another host's cookies. Plain {@code http}, + * so a lookup that resolves the secure context parses the host as an address. + */ + @State(Scope.Thread) + public static class NoMatch { + @Param({"api.eu.service.example.org", "192.0.2.10"}) + public String host; + + private final ThreadSafeCookieStore emptyStore = new ThreadSafeCookieStore(); + private final ThreadSafeCookieStore populatedStore = new ThreadSafeCookieStore(); + private Uri requestUri; + + @Setup(Level.Trial) + public void setup() { + Uri other = Uri.create("http://www.example.com/"); + for (int i = 0; i < 5; i++) { + populatedStore.add(other, new DefaultCookie("cookie" + i, "value" + i)); + } + DefaultCookie parent = new DefaultCookie("parent", "v"); + parent.setDomain("example.com"); + populatedStore.add(other, parent); + populatedStore.add(Uri.create("http://198.51.100.7/"), new DefaultCookie("ip", "v")); + + requestUri = Uri.create("http://" + host + "/v1/items"); + } + } + + @Benchmark + public List emptyJar(NoMatch state) { + return state.emptyStore.get(state.requestUri); + } + + @Benchmark + public List unrelatedHost(NoMatch state) { + return state.populatedStore.get(state.requestUri); + } } diff --git a/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java b/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java index 9cb5a82e2..76d0b5ea3 100644 --- a/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java +++ b/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java @@ -78,8 +78,11 @@ public void add(Uri uri, Cookie cookie) { @Override public List get(Uri uri) { - String domain = requestDomain(uri); - return get(domain, requestPath(uri), uri.isSecured(), isSecureContext(uri, domain)); + // Most clients never receive a cookie; an empty jar cannot match, so skip the host work. + if (cookieJar.isEmpty()) { + return Collections.emptyList(); + } + return get(uri, requestDomain(uri), requestPath(uri)); } @Override @@ -404,7 +407,12 @@ && pathsMatch(storedKey.path, newKey.path) return false; } - private List get(String domain, String path, boolean tls, boolean secure) { + private List get(Uri uri, String domain, String path) { + boolean tls = uri.isSecured(); + // Resolved at the first bucket found: on plaintext it parses the host as an address, and a host + // with no bucket in its domain chain never needs it. + boolean secure = false; + boolean secureResolved = false; boolean exactDomainMatch = true; String subDomain = domain; List> results = null; @@ -413,17 +421,21 @@ private List get(String domain, String path, boolean tls, boolean secure boolean walkParents = !isIpAddressLiteral(domain); while (MiscUtils.isNonEmpty(subDomain)) { - // Lazily allocate a single result list and append matches straight into it; an imperative - // scan avoids the per-sub-domain-level Stream pipeline (filter/map stages, two capturing - // lambdas, a spliterator and the Collectors.toList intermediate list) that this ran on every - // cookie-enabled request. - if (results == null) { - results = new ArrayList<>(4); - } - int from = results.size(); - collectStoredCookies(subDomain, path, tls, secure, exactDomainMatch, results); - if (results.size() - from > 1) { - results.subList(from, results.size()).sort(LONGER_PATH_FIRST); + Map bucket = cookieJar.get(subDomain); + if (bucket != null) { + if (!secureResolved) { + secure = isSecureContext(uri, domain); + secureResolved = true; + } + // Lazily allocate a single result list and append matches straight into it; an imperative + // scan avoids the per-sub-domain-level Stream pipeline (filter/map stages, two capturing + // lambdas, a spliterator and the Collectors.toList intermediate list) that this ran on every + // cookie-enabled request. + int from = results == null ? 0 : results.size(); + results = collectStoredCookies(bucket, path, tls, secure, exactDomainMatch, results); + if (results != null && results.size() - from > 1) { + results.subList(from, results.size()).sort(LONGER_PATH_FIRST); + } } if (!walkParents) { break; @@ -432,7 +444,7 @@ private List get(String domain, String path, boolean tls, boolean secure exactDomainMatch = false; } - if (results == null || results.isEmpty()) { + if (results == null) { return Collections.emptyList(); } if (results.size() == 1) { @@ -464,13 +476,9 @@ private List get(String domain, String path, boolean tls, boolean secure .reversed() .thenComparingLong(entry -> entry.getValue().seq); - private void collectStoredCookies(String domain, String path, boolean tls, boolean secure, boolean isExactMatch, - List> out) { - final Map innerMap = cookieJar.get(domain); - if (innerMap == null) { - return; - } - + private @Nullable List> collectStoredCookies( + Map innerMap, String path, boolean tls, boolean secure, boolean isExactMatch, + @Nullable List> out) { for (Map.Entry entry : innerMap.entrySet()) { CookieKey key = entry.getKey(); StoredCookie storedCookie = entry.getValue(); @@ -478,9 +486,13 @@ private void collectStoredCookies(String domain, String path, boolean tls, boole && (isExactMatch || !storedCookie.hostOnly) && pathsMatch(key.path, path) && (tls || !storedCookie.cookie.isSecure() || (secure && !storedCookie.overTls))) { + if (out == null) { + out = new ArrayList<>(4); + } out.add(entry); } } + return out; } private void removeExpired() { diff --git a/client/src/test/java/org/asynchttpclient/cookie/ThreadSafeCookieStoreGetTest.java b/client/src/test/java/org/asynchttpclient/cookie/ThreadSafeCookieStoreGetTest.java index 251aabeb7..419745b99 100644 --- a/client/src/test/java/org/asynchttpclient/cookie/ThreadSafeCookieStoreGetTest.java +++ b/client/src/test/java/org/asynchttpclient/cookie/ThreadSafeCookieStoreGetTest.java @@ -197,4 +197,58 @@ public void returnsEmptyForUnknownDomain() { List result = store.get(Uri.create("http://www.bar.com/")); assertTrue(result.isEmpty(), "no cookies should match an unrelated domain"); } + + private static final String[] UNRELATED_HOSTS = { + "http://www.bar.com/", "https://www.bar.com/", + "http://api.eu.service.bar.org/x", "https://api.eu.service.bar.org/x", + "http://192.0.2.10/", "https://192.0.2.10/", + "http://localhost/", "http://127.0.0.1/", + }; + + @Test + public void anEmptyJarReturnsNothingForAnyHost() { + ThreadSafeCookieStore store = new ThreadSafeCookieStore(); + for (String url : UNRELATED_HOSTS) { + assertTrue(store.get(Uri.create(url)).isEmpty(), url); + } + } + + @Test + public void aClearedJarReturnsNothing() { + ThreadSafeCookieStore store = new ThreadSafeCookieStore(); + Uri uri = Uri.create("http://www.foo.com/"); + store.add(uri, ClientCookieDecoder.LAX.decode("ALPHA=VALUE1; Path=/")); + store.clear(); + + assertTrue(store.get(uri).isEmpty()); + } + + @Test + public void aPopulatedJarReturnsNothingForAnUnrelatedHost() { + ThreadSafeCookieStore store = new ThreadSafeCookieStore(); + store.add(Uri.create("https://www.foo.com/"), ClientCookieDecoder.LAX.decode("TLS=V; Secure; Path=/")); + store.add(Uri.create("http://www.foo.com/"), ClientCookieDecoder.LAX.decode("HOST=V; Path=/")); + store.add(Uri.create("http://www.foo.com/"), ClientCookieDecoder.LAX.decode("PARENT=V; Domain=foo.com; Path=/")); + store.add(Uri.create("http://198.51.100.7/"), ClientCookieDecoder.LAX.decode("IP=V; Path=/")); + + for (String url : UNRELATED_HOSTS) { + assertTrue(store.get(Uri.create(url)).isEmpty(), url); + } + assertEquals(setOf("TLS=V", "HOST=V", "PARENT=V"), namesValues(store.get(Uri.create("https://www.foo.com/")))); + assertEquals(setOf("HOST=V", "PARENT=V"), namesValues(store.get(Uri.create("http://www.foo.com/")))); + assertEquals(setOf("PARENT=V"), namesValues(store.get(Uri.create("http://api.eu.foo.com/")))); + assertEquals(setOf("IP=V"), namesValues(store.get(Uri.create("http://198.51.100.7/")))); + } + + @Test + public void aBucketWithNoMatchReturnsNothing() { + ThreadSafeCookieStore store = new ThreadSafeCookieStore(); + store.add(Uri.create("https://www.foo.com/"), ClientCookieDecoder.LAX.decode("TLS=V; Secure; Path=/")); + store.add(Uri.create("http://www.foo.com/"), ClientCookieDecoder.LAX.decode("DEEP=V; Path=/deep")); + store.add(Uri.create("http://www.foo.com/"), ClientCookieDecoder.LAX.decode("HOST=V; Path=/")); + + assertEquals(setOf("DEEP=V", "HOST=V"), namesValues(store.get(Uri.create("http://www.foo.com/deep")))); + assertEquals(setOf("HOST=V"), namesValues(store.get(Uri.create("http://www.foo.com/x")))); + assertTrue(store.get(Uri.create("http://sub.www.foo.com/deep")).isEmpty(), "host-only cookies only"); + } }