optimization: do not instantiate weak/soft refs when not needed

GitOrigin-RevId: 93095b63af562f76d4ebc1108daea5ead860096a
This commit is contained in:
Alexey Kudravtsev
2024-09-10 14:24:15 +02:00
committed by intellij-monorepo-bot
parent e6dbd42817
commit 1f983b8625
3 changed files with 9 additions and 11 deletions

View File

@@ -351,22 +351,21 @@ abstract class ConcurrentRefHashMap<K, V> extends AbstractMap<K, V> implements C
@Override
public boolean remove(@NotNull Object key, @NotNull Object value) {
//noinspection unchecked
boolean removed = myMap.remove(createKeyReference((K)key), value);
boolean removed = myMap.remove(createHardKey(key), value);
processQueue();
return removed;
}
@Override
public boolean replace(@NotNull K key, @NotNull V oldValue, @NotNull V newValue) {
boolean replaced = myMap.replace(createKeyReference(key), oldValue, newValue);
boolean replaced = myMap.replace(createHardKey(key), oldValue, newValue);
processQueue();
return replaced;
}
@Override
public V replace(@NotNull K key, @NotNull V value) {
V replaced = myMap.replace(createKeyReference(key), value);
V replaced = myMap.replace(createHardKey(key), value);
processQueue();
return replaced;
}

View File

@@ -11,7 +11,7 @@ import java.lang.ref.SoftReference;
* Concurrent map with soft keys and soft values.
* Null keys are NOT allowed
* Null values are NOT allowed
* To instantiate use {@link ContainerUtil#createConcurrentWeakKeySoftValueMap(int, float, int, HashingStrategy)}
* To instantiate use {@link ContainerUtil#createConcurrentSoftKeySoftValueMap()}
*/
final class ConcurrentSoftKeySoftValueHashMap<K, V> extends ConcurrentWeakKeySoftValueHashMap<K,V> {
ConcurrentSoftKeySoftValueHashMap(int initialCapacity,

View File

@@ -341,10 +341,9 @@ public class ConcurrentWeakKeySoftValueHashMap<K, V> implements ConcurrentMap<K,
@Override
public boolean replace(@NotNull K key, @NotNull V oldValue, @NotNull V newValue) {
KeyReference<K, V> oldKeyReference = createKeyReference(key, oldValue);
ValueReference<K, V> oldValueReference = oldKeyReference.getValueReference();
KeyReference<K, V> newKeyReference = createKeyReference(key, newValue);
ValueReference<K, V> newValueReference = newKeyReference.getValueReference();
KeyReference<K, V> oldKeyReference = createHardKey(key);
ValueReference<K, V> oldValueReference = createValueReference(oldValue, myValueQueue);
ValueReference<K, V> newValueReference = createValueReference(newValue, myValueQueue);
boolean replaced = myMap.replace(oldKeyReference, oldValueReference, newValueReference);
processQueues();
@@ -353,8 +352,8 @@ public class ConcurrentWeakKeySoftValueHashMap<K, V> implements ConcurrentMap<K,
@Override
public V replace(@NotNull K key, @NotNull V value) {
KeyReference<K, V> keyReference = createKeyReference(key, value);
ValueReference<K, V> valueReference = keyReference.getValueReference();
KeyReference<K, V> keyReference = createHardKey(key);
ValueReference<K, V> valueReference = createValueReference(value, myValueQueue);
ValueReference<K, V> result = myMap.replace(keyReference, valueReference);
V prev = result == null ? null : result.get();
processQueues();