From 7c57e6fe692085f9ca3df9eaa88780b76caeb63d Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 16 Jan 2017 20:07:39 +0300 Subject: [PATCH] more deprecations and visibility tightening --- .../psi/impl/source/resolve/ResolveCache.java | 12 +- .../util/containers/ConcurrentMapsTest.java | 121 +++++++----------- .../ConcurrentSoftValueHashMap.java | 6 +- .../ConcurrentWeakKeySoftValueHashMap.java | 2 +- .../containers/WeakKeySoftValueHashMap.java | 2 +- 5 files changed, 60 insertions(+), 83 deletions(-) diff --git a/platform/core-impl/src/com/intellij/psi/impl/source/resolve/ResolveCache.java b/platform/core-impl/src/com/intellij/psi/impl/source/resolve/ResolveCache.java index 172c338fe729..d2f00931d1fb 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/source/resolve/ResolveCache.java +++ b/platform/core-impl/src/com/intellij/psi/impl/source/resolve/ResolveCache.java @@ -37,8 +37,8 @@ import java.util.concurrent.ConcurrentMap; public class ResolveCache { private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.resolve.ResolveCache"); - @SuppressWarnings("unchecked") - private final ConcurrentMap[] myMaps = new ConcurrentWeakKeySoftValueHashMap[2*2*2]; //boolean physical, boolean incompleteCode, boolean isPoly + + private final ConcurrentMap[] myMaps = new ConcurrentMap[2*2*2]; //boolean physical, boolean incompleteCode, boolean isPoly private final RecursionGuard myGuard = RecursionManager.createGuard("resolveCache"); public static ResolveCache getInstance(Project project) { @@ -84,7 +84,7 @@ public class ResolveCache { } }); } - + @NotNull private static ConcurrentMap createWeakMap() { return new ConcurrentWeakKeySoftValueHashMap(100, 0.75f, Runtime.getRuntime().availableProcessors(), ContainerUtil.canonicalStrategy()){ @@ -224,9 +224,9 @@ public class ResolveCache { } private static final Object NULL_RESULT = new Object(); - private void cache(@NotNull TRef ref, - @NotNull ConcurrentMap map, - TResult result) { + private static void cache(@NotNull TRef ref, + @NotNull ConcurrentMap map, + TResult result) { // optimization: less contention TResult cached = map.get(ref); if (cached != null && cached == result) { diff --git a/platform/platform-tests/testSrc/com/intellij/util/containers/ConcurrentMapsTest.java b/platform/platform-tests/testSrc/com/intellij/util/containers/ConcurrentMapsTest.java index 5c760836228d..9db538f4fa15 100644 --- a/platform/platform-tests/testSrc/com/intellij/util/containers/ConcurrentMapsTest.java +++ b/platform/platform-tests/testSrc/com/intellij/util/containers/ConcurrentMapsTest.java @@ -24,6 +24,7 @@ import org.junit.Test; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentMap; import static org.junit.Assert.*; @@ -45,17 +46,8 @@ public class ConcurrentMapsTest { @Test(timeout = TIMEOUT) public void testConcurrentWeakKeysRemovedWhenIdentityStrategyIsUsed() { - ConcurrentWeakHashMap map = new ConcurrentWeakHashMap<>(ContainerUtil.identityStrategy()); - map.put(new Object(), new Object()); - - do { - tryGcSoftlyReachableObjects(); // sometimes weak references are not collected under linux, try to stress gc to force them - System.gc(); - } - while (!map.processQueue()); - assertEquals(0, map.underlyingMapSize()); - map.put(this, this); - assertEquals(1, map.underlyingMapSize()); + ConcurrentMap map = ContainerUtil.createConcurrentWeakMap(ContainerUtil.identityStrategy()); + checkKeyIsTossedAfterGCPressure(map); } @Test(timeout = TIMEOUT) @@ -80,7 +72,7 @@ public class ConcurrentMapsTest { @Test(timeout = TIMEOUT) public void testRemoveFromSoftEntrySet() { - ConcurrentSoftHashMap map = new ConcurrentSoftHashMap<>(); + ConcurrentMap map = ContainerUtil.createConcurrentSoftMap(); map.put(this, this); Set> entries = map.entrySet(); assertEquals(1, entries.size()); @@ -92,7 +84,7 @@ public class ConcurrentMapsTest { @Test(timeout = TIMEOUT) public void testRemoveFromWeakEntrySet() { - ConcurrentWeakHashMap map = new ConcurrentWeakHashMap<>(); + ConcurrentMap map = ContainerUtil.createConcurrentWeakMap(); map.put(this, this); Set> entries = map.entrySet(); assertEquals(1, entries.size()); @@ -104,17 +96,8 @@ public class ConcurrentMapsTest { @Test(timeout = TIMEOUT) public void testConcurrentWeakTossedWeakKeysAreRemoved() { - ConcurrentWeakHashMap map = new ConcurrentWeakHashMap<>(); - map.put(new Object(), new Object()); - - do { - tryGcSoftlyReachableObjects(); // sometimes weak references are not collected under linux, try to stress gc to force them - System.gc(); - } - while (!map.processQueue()); - assertEquals(0, map.underlyingMapSize()); - map.put(this, this); - assertEquals(1, map.underlyingMapSize()); + ConcurrentMap map = ContainerUtil.createConcurrentWeakMap(); + checkKeyIsTossedAfterGCPressure(map); } public static void tryGcSoftlyReachableObjects() { @@ -123,48 +106,37 @@ public class ConcurrentMapsTest { @Test(timeout = TIMEOUT) public void testConcurrentSoftTossedSoftKeysAreRemoved() { - ConcurrentSoftHashMap map = new ConcurrentSoftHashMap<>(); - map.put(new Object(), new Object()); - - do { - tryGcSoftlyReachableObjects(); - System.gc(); - } - while (!map.processQueue()); - assertEquals(0, map.underlyingMapSize()); - map.put(this, this); - assertEquals(1, map.underlyingMapSize()); + ConcurrentMap map = ContainerUtil.createConcurrentSoftMap(); + checkKeyIsTossedAfterGCPressure(map); } @Test(timeout = TIMEOUT) public void testConcurrentWeakTossedWeakValueIsRemoved() { - ConcurrentWeakValueHashMap map = - (ConcurrentWeakValueHashMap)ContainerUtil.createConcurrentWeakValueMap(); + ConcurrentMap map = ContainerUtil.createConcurrentWeakValueMap(); + checkKeyIsTossedAfterGCPressure(map); + } + + private void checkKeyIsTossedAfterGCPressure(ConcurrentMap map) { map.put(new Object(), new Object()); + //noinspection SizeReplaceableByIsEmpty do { + map.put(this, this); // to run processQueues(); + map.remove(this); + tryGcSoftlyReachableObjects(); // sometimes weak references are not collected under linux, try to stress gc to force them System.gc(); } - while (!map.processQueue()); - assertEquals(0, map.underlyingMapSize()); + while (map.size() != 0); + assertEquals(0, map.size()); map.put(this, this); - assertEquals(1, map.underlyingMapSize()); + assertEquals(1, map.size()); } @Test(timeout = TIMEOUT) public void testConcurrentSoftTossedSoftValueIsRemoved() { - ConcurrentSoftValueHashMap map = new ConcurrentSoftValueHashMap<>(); - map.put(new Object(), new Object()); - - do { - tryGcSoftlyReachableObjects(); - System.gc(); - } - while (!map.processQueue()); - assertEquals(0, map.underlyingMapSize()); - map.put(this, this); - assertEquals(1, map.underlyingMapSize()); + ConcurrentMap map = ContainerUtil.createConcurrentSoftValueMap(); + checkKeyIsTossedAfterGCPressure(map); } @Test(timeout = TIMEOUT) @@ -224,7 +196,7 @@ public class ConcurrentMapsTest { @Test(timeout = TIMEOUT) public void testConcurrentSoftCustomStrategy() { - ConcurrentSoftHashMap map = new ConcurrentSoftHashMap<>(CUSTOM_STRATEGY); + ConcurrentMap map = ContainerUtil.createConcurrentSoftMap(10,0.7f,16,CUSTOM_STRATEGY); map.put("ab", "ab"); assertEquals(1, map.size()); @@ -236,32 +208,32 @@ public class ConcurrentMapsTest { @Test public void testConcurrentSoftNullKey() { - Map map = new ConcurrentSoftHashMap<>(); + Map map = ContainerUtil.createConcurrentSoftMap(); - checkNullKeys(map); + tryToInsertNullKeys(map); } @Test public void testConcurrentWeakNullKey() { - Map map = new ConcurrentWeakHashMap<>(); + Map map = ContainerUtil.createConcurrentWeakMap(); - checkNullKeys(map); + tryToInsertNullKeys(map); } @Test(expected = IllegalArgumentException.class) public void testConcurrentWeakSoftNullKey() { - Map map = new ConcurrentWeakKeySoftValueHashMap<>(1, 1, 1, CUSTOM_STRATEGY); + Map map = ContainerUtil.createConcurrentWeakKeySoftValueMap(1, 1, 1, CUSTOM_STRATEGY); - checkNullKeys(map); + tryToInsertNullKeys(map); } @Test(expected = IllegalArgumentException.class) public void testConcurrentWeakWeakNullKey() { - Map map = new ConcurrentWeakKeyWeakValueHashMap<>(1, 1, 1, CUSTOM_STRATEGY); + Map map = ContainerUtil.createConcurrentWeakKeyWeakValueMap(CUSTOM_STRATEGY); - checkNullKeys(map); + tryToInsertNullKeys(map); } - private static void checkNullKeys(Map map) { + private static void tryToInsertNullKeys(Map map) { map.put(null, "ab"); assertEquals(1, map.size()); assertEquals("ab", map.get(null)); @@ -272,7 +244,7 @@ public class ConcurrentMapsTest { @Test(timeout = TIMEOUT) public void testConcurrentWeakSoftCustomStrategy() { - ConcurrentWeakKeySoftValueHashMap map = new ConcurrentWeakKeySoftValueHashMap<>(1, 1, 1, CUSTOM_STRATEGY); + ConcurrentMap map = ContainerUtil.createConcurrentWeakKeySoftValueMap(1, 1, 1, CUSTOM_STRATEGY); map.put("ab", "ab"); assertEquals(1, map.size()); @@ -340,7 +312,7 @@ public class ConcurrentMapsTest { @Test(timeout = TIMEOUT) public void testConcurrentLongObjectHashMap() { - ConcurrentLongObjectMap map = new ConcurrentLongObjectHashMap<>(); + ConcurrentLongObjectMap map = ContainerUtil.createConcurrentLongObjectMap(); for (int i = 0; i < 1000; i++) { Object prev = map.put(i, i); assertNull(prev); @@ -383,7 +355,7 @@ public class ConcurrentMapsTest { @Test(timeout = TIMEOUT) public void testConcurrentIntObjectHashMap() { - ConcurrentIntObjectMap map = new ConcurrentIntObjectHashMap<>(); + ConcurrentIntObjectMap map = ContainerUtil.createConcurrentIntObjectMap(); for (int i = 0; i < 1000; i++) { Object prev = map.put(i, i); assertNull(prev); @@ -404,32 +376,37 @@ public class ConcurrentMapsTest { @Test(timeout = TIMEOUT) public void testConcurrentWeakKeyAndValueTossed() { - ConcurrentWeakKeyWeakValueHashMap map = - (ConcurrentWeakKeyWeakValueHashMap)ContainerUtil.createConcurrentWeakKeyWeakValueMap(); + ConcurrentMap map = ContainerUtil.createConcurrentWeakKeyWeakValueMap(); map.put(new Object(), new Object()); do { + map.put(this, this); // to run processQueues(); + map.remove(this); + tryGcSoftlyReachableObjects(); System.gc(); } - while (!map.processQueues()); - assertTrue(map.isEmpty()); + while (!map.isEmpty()); map.put(this, new Object()); do { + map.put(this, this); // to run processQueues(); + map.remove(this); + tryGcSoftlyReachableObjects(); System.gc(); } - while (!map.processQueues()); - assertTrue(map.isEmpty()); + while (!map.isEmpty()); map.put(new Object(), this); do { + map.put(this, this); // to run processQueues(); + map.remove(this); + tryGcSoftlyReachableObjects(); System.gc(); } - while (!map.processQueues()); - assertTrue(map.isEmpty()); + while (!map.isEmpty()); } @Test diff --git a/platform/util/src/com/intellij/util/containers/ConcurrentSoftValueHashMap.java b/platform/util/src/com/intellij/util/containers/ConcurrentSoftValueHashMap.java index 5bc36b2752ae..951dab1d78ce 100644 --- a/platform/util/src/com/intellij/util/containers/ConcurrentSoftValueHashMap.java +++ b/platform/util/src/com/intellij/util/containers/ConcurrentSoftValueHashMap.java @@ -29,12 +29,12 @@ import java.util.Map; * Null values are NOT allowed * @deprecated Use {@link ContainerUtil#createConcurrentSoftValueMap()} instead */ -public final class ConcurrentSoftValueHashMap extends ConcurrentRefValueHashMap { - public ConcurrentSoftValueHashMap(@NotNull Map map) { +final class ConcurrentSoftValueHashMap extends ConcurrentRefValueHashMap { + ConcurrentSoftValueHashMap(@NotNull Map map) { super(map); } - public ConcurrentSoftValueHashMap() { + ConcurrentSoftValueHashMap() { } public ConcurrentSoftValueHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) { diff --git a/platform/util/src/com/intellij/util/containers/ConcurrentWeakKeySoftValueHashMap.java b/platform/util/src/com/intellij/util/containers/ConcurrentWeakKeySoftValueHashMap.java index 9bce656b72da..97efba497628 100644 --- a/platform/util/src/com/intellij/util/containers/ConcurrentWeakKeySoftValueHashMap.java +++ b/platform/util/src/com/intellij/util/containers/ConcurrentWeakKeySoftValueHashMap.java @@ -275,7 +275,7 @@ public class ConcurrentWeakKeySoftValueHashMap implements ConcurrentMap keyReference; while ((keyReference = (KeyReference)myKeyQueue.poll()) != null) { diff --git a/platform/util/src/com/intellij/util/containers/WeakKeySoftValueHashMap.java b/platform/util/src/com/intellij/util/containers/WeakKeySoftValueHashMap.java index a0a1372812bd..d5ec99ddfab0 100644 --- a/platform/util/src/com/intellij/util/containers/WeakKeySoftValueHashMap.java +++ b/platform/util/src/com/intellij/util/containers/WeakKeySoftValueHashMap.java @@ -22,7 +22,7 @@ import java.lang.ref.SoftReference; import java.util.Map; final class WeakKeySoftValueHashMap extends RefKeyRefValueHashMap implements Map{ - public WeakKeySoftValueHashMap() { + WeakKeySoftValueHashMap() { super(new WeakHashMap>()); }