Skip to content

Commit ac19392

Browse files
committed
Omit cookie spec registry when cookie management is disabled
The HTTP client builders always construct a client with a non-null cookie spec registry; if a custom instance is not specified, then the builders will use `CookieSpecSupport.createDefault()`, which loads the full public suffix list. The PSL rules are stored on the heap as a `ConcurrentHashMap`, i.e. a giant array (16,384 on my machine) containing map entries, each of which is also allocated as a separate object on the heap. This is a considerable number of live objects that the garbage collector may need to trace. I ran a simple microbenchmark to show the effects of loading the PSL on garbage collection (these numbers are from JDK 1.8): GC took 2 ms GC took 2 ms GC took 2 ms Loading public suffix list... done (took 193 ms) GC took 11 ms GC took 8 ms GC took 7 ms This is potentially a significant amount of tail latency for use cases that don't require cookie management, such as RPC calls. With this change, it is now simpler to configure and construct a client that does not load the PSL into memory.
1 parent 5cef6ed commit ac19392

4 files changed

Lines changed: 60 additions & 3 deletions

File tree

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/H2AsyncClientBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ public CloseableHttpAsyncClient build() {
901901
.build();
902902
}
903903
Lookup<CookieSpecFactory> cookieSpecRegistryCopy = this.cookieSpecRegistry;
904-
if (cookieSpecRegistryCopy == null) {
904+
if (cookieSpecRegistryCopy == null && !cookieManagementDisabled) {
905905
cookieSpecRegistryCopy = CookieSpecSupport.createDefault();
906906
}
907907

httpclient5/src/main/java/org/apache/hc/client5/http/impl/async/HttpAsyncClientBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ public CloseableHttpAsyncClient build() {
11181118
.build();
11191119
}
11201120
Lookup<CookieSpecFactory> cookieSpecRegistryCopy = this.cookieSpecRegistry;
1121-
if (cookieSpecRegistryCopy == null) {
1121+
if (cookieSpecRegistryCopy == null && !cookieManagementDisabled) {
11221122
cookieSpecRegistryCopy = CookieSpecSupport.createDefault();
11231123
}
11241124

httpclient5/src/main/java/org/apache/hc/client5/http/impl/classic/HttpClientBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ public CloseableHttpClient build() {
10661066
.build();
10671067
}
10681068
Lookup<CookieSpecFactory> cookieSpecRegistryCopy = this.cookieSpecRegistry;
1069-
if (cookieSpecRegistryCopy == null) {
1069+
if (cookieSpecRegistryCopy == null && !this.cookieManagementDisabled) {
10701070
cookieSpecRegistryCopy = CookieSpecSupport.createDefault();
10711071
}
10721072

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
28+
package org.apache.hc.client5.http.examples;
29+
30+
import org.apache.hc.client5.http.psl.PublicSuffixMatcherLoader;
31+
32+
import static java.util.concurrent.TimeUnit.NANOSECONDS;
33+
34+
public class PublicSuffixLoaderBenchmark {
35+
public static void main(final String[] args) {
36+
benchmarkGarbageCollection();
37+
loadPublicSuffixList();
38+
benchmarkGarbageCollection();
39+
}
40+
41+
private static void loadPublicSuffixList() {
42+
final long start = System.nanoTime();
43+
System.out.print("Loading public suffix list...");
44+
PublicSuffixMatcherLoader.getDefault();
45+
final long end = System.nanoTime();
46+
System.out.printf(" done (took %,d ms)%n", NANOSECONDS.toMillis(end - start));
47+
}
48+
49+
private static void benchmarkGarbageCollection() {
50+
for (int i = 0; i < 3; i++) {
51+
final long start = System.nanoTime();
52+
System.gc();
53+
final long end = System.nanoTime();
54+
System.out.printf("GC took %,d ms%n", NANOSECONDS.toMillis(end - start));
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)