performance: make isEmpty() allocate-free, tests added

This commit is contained in:
Alexey Kudravtsev
2017-08-14 16:58:22 +03:00
parent 73cdefb017
commit f529aa6efa
3 changed files with 11 additions and 4 deletions
@@ -97,17 +97,20 @@ public class ConcurrentMapsTest {
}
private void checkKeyIsTossedAfterGCPressure(Map<Object, Object> map) {
assertTrue(map.isEmpty());
map.put(new Object(), new Object());
//noinspection SizeReplaceableByIsEmpty
do {
map.put(this, this); // to run processQueues();
assertFalse(map.isEmpty());
map.remove(this);
GCUtil.tryGcSoftlyReachableObjects();
System.gc();
}
while (map.size() != 0);
assertTrue(map.isEmpty());
assertEquals(0, map.size());
map.put(this, this);
assertEquals(1, map.size());
@@ -255,6 +258,7 @@ public class ConcurrentMapsTest {
}
private static void checkTossedKeyAndValue(@NotNull RefKeyRefValueHashMap<Object, Object> map) {
assertTrue(map.isEmpty());
map.put(new Object(), new Object());
do {
@@ -126,7 +126,8 @@ abstract class ConcurrentRefHashMap<K, V> extends AbstractMap<K, V> implements C
@Override
public boolean isEmpty() {
return myMap.isEmpty();
// make easier and alloc-free call to myMap first
return myMap.isEmpty() || entrySet().isEmpty();
}
@Override
@@ -202,7 +202,8 @@ abstract class RefHashMap<K, V> extends AbstractMap<K, V> implements Map<K, V> {
@Override
public boolean isEmpty() {
return myMap.isEmpty();
// make easier and alloc-free call to myMap first
return myMap.isEmpty() || entrySet().isEmpty();
}
@Override
@@ -383,7 +384,8 @@ abstract class RefHashMap<K, V> extends AbstractMap<K, V> implements Map<K, V> {
@NotNull
@Override
public Set<Entry<K, V>> entrySet() {
if (entrySet == null) entrySet = new EntrySet();
return entrySet;
Set<Entry<K, V>> es = entrySet;
if (es == null) entrySet = es = new EntrySet();
return es;
}
}