Skip to content

Commit

Permalink
ICU-22761 Optimize get value of LocaleObjectCache
Browse files Browse the repository at this point in the history
When invoking get(K key) method of LocaleObjectCache, it may be blocked for a long time, looping in lines 45 and 53, because after the referent of SoftReference is set to NULL, it may take a long time to add SoftReference to its ReferenceQueue if GC is slowly.

For example, in the Concurrent Copying GC in Android Art virtual machine, the referent in SoftReference is set to null during the copying phase, while the SoftReference is added to its ReferenceQueue in the ReferenceQueueDaemon thread after GC, which experiences a long time in between
  • Loading branch information
imurluck committed Apr 29, 2024
1 parent 3aa8b8c commit 9131585
Showing 1 changed file with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,34 @@ public V get(K key) {
}
if (value == null) {
key = normalizeKey(key);
V newVal = createObject(key);
if (key == null || newVal == null) {
// subclass must return non-null key/value object
// subclass must return non-null key object
if (key == null) {
return null;
}

CacheEntry<K, V> newEntry = new CacheEntry<K, V>(key, newVal, _queue);
entry = _map.get(key);
if (entry != null) {
value = entry.get();
}
// hit cache
if (value != null) {
return value;
}

while (value == null) {
cleanStaleEntries();
entry = _map.putIfAbsent(key, newEntry);
if (entry == null) {
value = newVal;
break;
} else {
value = entry.get();
}
// if map not contains key or the referent value of CacheEntry is set to be null
// both need create a new value
V newVal = createObject(key);
if (newVal == null) {
// subclass must return non-null value object
return null;
}

CacheEntry<K, V> newEntry = new CacheEntry<K, V>(key, newVal, _queue);
// just replace it
_map.put(key, newEntry);
// clean recycled SoftReferences again
cleanStaleEntries();
return newVal;
}
return value;
}
Expand Down

0 comments on commit 9131585

Please sign in to comment.