[LL] CleanableSoftValueCache: Avoid cleanup on already removed values

- The cache guarantees deterministic cleanup on removal, so if the
  reference has already been removed from the cache, there is no need to
  clean it up again.
- Still, I don't want to guarantee that `SoftValueCleaner` is only
  invoked once (see its documentation), as soft reference semantics
  cannot be tested properly in our current test infrastructure.

^KT-61222
This commit is contained in:
Marco Pennekamp
2024-02-07 21:43:21 +01:00
committed by Space Team
parent f7a0e1f7c1
commit 2b40ef6b05
@@ -61,8 +61,12 @@ class CleanableSoftValueCache<K : Any, V : Any>(
@Suppress("UNCHECKED_CAST")
ref as SoftReferenceWithCleanup<K, V>
backingMap.remove(ref.key, ref)
ref.performCleanup()
val wasRemoved = backingMap.remove(ref.key, ref)
// If `ref` already wasn't part of the map, it will have been cleaned up by a deterministic removal operation.
if (wasRemoved) {
ref.performCleanup()
}
}
}