Skip to content

Commit 81c7d03

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Cap deserialized LocalCache+MapMakerInternalMap concurrency level at 1024.
1024 ought to be enough for anybody. And probably few people are serializing caches or weak-entry-based maps, anyway. And people should be using [Caffeine](https://github.com/ben-manes/caffeine). There was already a cap of 64k, so these collections didn't permit arbitrary-sized allocations. Still, the reduction brings them more in line with what is already possible when deserializing built-in JDK types. See, for example, [`LogRecord`](https://github.com/openjdk/jdk/blob/4e8bec6f862b58732537dea15cb3dd50a01fede0/src/java.logging/share/classes/java/util/logging/LogRecord.java#L704) and [`ZoneRules`](https://github.com/openjdk/jdk/blob/4e8bec6f862b58732537dea15cb3dd50a01fede0/src/java.base/share/classes/java/time/zone/ZoneRules.java#L464) (though the constant factors are still different). (If any user of the classes is directly passing a concurrency level of more than 1024, it's extremely likely that that user would be better off if we were to cap it to 1024 in that code path, too. But I'm not bothering in this CL.) RELNOTES=n/a PiperOrigin-RevId: 993749847
1 parent 51246d0 commit 81c7d03

8 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎android/guava-tests/test/com/google/common/cache/LocalCacheTest.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,6 +2722,15 @@ public void testSerializationProxyManual() {
27222722
assertThat(localCacheThree.ticker).isEqualTo(localCacheTwo.ticker);
27232723
}
27242724

2725+
public void testDeserializeWithHugeConcurrencyLevel() {
2726+
LocalManualCache<?, ?> cache =
2727+
(LocalManualCache<?, ?>)
2728+
CacheBuilder.newBuilder().concurrencyLevel(Integer.MAX_VALUE).build();
2729+
assertThat(cache.localCache.segments).hasLength(65536);
2730+
LocalManualCache<?, ?> deserialized = reserialize(cache);
2731+
assertThat(deserialized.localCache.segments).hasLength(1024);
2732+
}
2733+
27252734
public void testLoadDifferentKeyInLoader() throws ExecutionException {
27262735
LocalCache<String, String> cache = makeLocalCache(createCacheBuilder());
27272736
String key1 = "key1";

‎android/guava-tests/test/com/google/common/collect/MapMakerInternalMapTest.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.common.collect;
1818

1919
import static com.google.common.collect.MapMakerInternalMap.DRAIN_THRESHOLD;
20+
import static com.google.common.testing.SerializableTester.reserialize;
2021
import static com.google.common.truth.Truth.assertThat;
2122

2223
import com.google.common.base.Equivalence;
@@ -966,6 +967,14 @@ public int readInt() throws IOException {
966967
}
967968
}
968969

970+
public void testDeserializeWithHugeConcurrencyLevel() {
971+
MapMakerInternalMap<?, ?, ?, ?> map =
972+
makeMap(createMapMaker().concurrencyLevel(Integer.MAX_VALUE));
973+
assertThat(map.segments).hasLength(65536);
974+
MapMakerInternalMap<?, ?, ?, ?> deserialized = reserialize(map);
975+
assertThat(deserialized.segments).hasLength(1024);
976+
}
977+
969978
// Our tests are generally (always?) updating the count from only one thread.
970979
@SuppressWarnings("NonAtomicVolatileUpdate")
971980
private static void incrementCount(Segment<?, ?, ?, ?> segment) {

‎android/guava/src/com/google/common/cache/LocalCache.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4559,7 +4559,7 @@ final CacheBuilder<K, V> recreateCacheBuilder() {
45594559
.setValueStrength(valueStrength)
45604560
.keyEquivalence(keyEquivalence)
45614561
.valueEquivalence(valueEquivalence)
4562-
.concurrencyLevel(concurrencyLevel)
4562+
.concurrencyLevel(min(concurrencyLevel, 1024))
45634563
.removalListener(removalListener);
45644564
builder.strictParsing = false;
45654565
if (expireAfterWriteNanos > 0) {

‎android/guava/src/com/google/common/collect/MapMakerInternalMap.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2852,7 +2852,7 @@ final MapMaker readMapMaker(ObjectInputStream in) throws IOException {
28522852
.setKeyStrength(keyStrength)
28532853
.setValueStrength(valueStrength)
28542854
.keyEquivalence(keyEquivalence)
2855-
.concurrencyLevel(concurrencyLevel);
2855+
.concurrencyLevel(min(concurrencyLevel, 1024));
28562856
}
28572857

28582858
@SuppressWarnings("unchecked")

‎guava-tests/test/com/google/common/cache/LocalCacheTest.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,6 +2768,15 @@ public void testSerializationProxyManual() {
27682768
assertThat(localCacheThree.ticker).isEqualTo(localCacheTwo.ticker);
27692769
}
27702770

2771+
public void testDeserializeWithHugeConcurrencyLevel() {
2772+
LocalManualCache<?, ?> cache =
2773+
(LocalManualCache<?, ?>)
2774+
CacheBuilder.newBuilder().concurrencyLevel(Integer.MAX_VALUE).build();
2775+
assertThat(cache.localCache.segments).hasLength(65536);
2776+
LocalManualCache<?, ?> deserialized = reserialize(cache);
2777+
assertThat(deserialized.localCache.segments).hasLength(1024);
2778+
}
2779+
27712780
public void testLoadDifferentKeyInLoader() throws ExecutionException {
27722781
LocalCache<String, String> cache = makeLocalCache(createCacheBuilder());
27732782
String key1 = "key1";

‎guava-tests/test/com/google/common/collect/MapMakerInternalMapTest.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.common.collect;
1818

1919
import static com.google.common.collect.MapMakerInternalMap.DRAIN_THRESHOLD;
20+
import static com.google.common.testing.SerializableTester.reserialize;
2021
import static com.google.common.truth.Truth.assertThat;
2122

2223
import com.google.common.base.Equivalence;
@@ -966,6 +967,14 @@ public int readInt() throws IOException {
966967
}
967968
}
968969

970+
public void testDeserializeWithHugeConcurrencyLevel() {
971+
MapMakerInternalMap<?, ?, ?, ?> map =
972+
makeMap(createMapMaker().concurrencyLevel(Integer.MAX_VALUE));
973+
assertThat(map.segments).hasLength(65536);
974+
MapMakerInternalMap<?, ?, ?, ?> deserialized = reserialize(map);
975+
assertThat(deserialized.segments).hasLength(1024);
976+
}
977+
969978
// Our tests are generally (always?) updating the count from only one thread.
970979
@SuppressWarnings("NonAtomicVolatileUpdate")
971980
private static void incrementCount(Segment<?, ?, ?, ?> segment) {

‎guava/src/com/google/common/cache/LocalCache.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4732,7 +4732,7 @@ final CacheBuilder<K, V> recreateCacheBuilder() {
47324732
.setValueStrength(valueStrength)
47334733
.keyEquivalence(keyEquivalence)
47344734
.valueEquivalence(valueEquivalence)
4735-
.concurrencyLevel(concurrencyLevel)
4735+
.concurrencyLevel(min(concurrencyLevel, 1024))
47364736
.removalListener(removalListener);
47374737
builder.strictParsing = false;
47384738
if (expireAfterWriteNanos > 0) {

‎guava/src/com/google/common/collect/MapMakerInternalMap.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2852,7 +2852,7 @@ final MapMaker readMapMaker(ObjectInputStream in) throws IOException {
28522852
.setKeyStrength(keyStrength)
28532853
.setValueStrength(valueStrength)
28542854
.keyEquivalence(keyEquivalence)
2855-
.concurrencyLevel(concurrencyLevel);
2855+
.concurrencyLevel(min(concurrencyLevel, 1024));
28562856
}
28572857

28582858
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)
⌂ 换网址