Implement cache eviction for classpath snapshot cache

Implement an in-memory cache that uses a combination of strong
references and `SoftReference`s so that it adapts to memory
availability.

Cache eviction is currently performed after loading a classpath snapshot
(this can be changed later if necessary).

Evicted cache entries' values will be converted from strong references
into `SoftReference`s so that they can still be used for as long as the
JVM allows them.

There are 2 types of cache eviction:
  - Least recently used: Oldest entries will be evicted
  - Memory usage limit: If memory is limited, all entries will be
    evicted

Test: Added InMemoryCacheWithEvictionTest unit test

^KT-51978 In Progress
This commit is contained in:
Hung Nguyen
2022-04-02 17:37:44 +01:00
committed by teamcity
parent 889db4cc5d
commit 6d3e679a59
5 changed files with 325 additions and 33 deletions
@@ -25,7 +25,7 @@ enum class BuildPerformanceMetric(val parent: BuildPerformanceMetric? = null, va
CLASSPATH_ENTRY_COUNT(parent = SHRINK_AND_SAVE_CLASSPATH_SNAPSHOT_EXECUTION_COUNT, "Number of classpath entries", type = SizeMetricType.NUMBER),
CLASSPATH_SNAPSHOT_SIZE(parent = SHRINK_AND_SAVE_CLASSPATH_SNAPSHOT_EXECUTION_COUNT, "Size of classpath snapshot", type = SizeMetricType.BYTES),
SHRUNK_CLASSPATH_SNAPSHOT_SIZE(parent = SHRINK_AND_SAVE_CLASSPATH_SNAPSHOT_EXECUTION_COUNT, "Size of shrunk classpath snapshot", type = SizeMetricType.BYTES),
LOAD_CLASSPATH_SNAPSHOT_CACHE_MISSES(parent = null, "Number of cache misses when loading classpath snapshot", type = SizeMetricType.NUMBER),
LOAD_CLASSPATH_ENTRY_SNAPSHOT_CACHE_MISSES(parent = null, "Number of cache misses when loading classpath entry snapshots", type = SizeMetricType.NUMBER),
;
companion object {
@@ -68,7 +68,7 @@ enum class BuildTime(val parent: BuildTime? = null, val readableString: String)
FIND_INACCESSIBLE_CLASSES(parent = SNAPSHOT_CLASSES, "Find inaccessible classes"),
SNAPSHOT_KOTLIN_CLASSES(parent = SNAPSHOT_CLASSES, "Snapshot Kotlin classes"),
SNAPSHOT_JAVA_CLASSES(parent = SNAPSHOT_CLASSES, "Snapshot Java classes"),
SAVE_CLASSPATH_ENTRY_SNAPSHOT(parent = CLASSPATH_ENTRY_SNAPSHOT_TRANSFORM, "Save classpath entry snapshot")
SAVE_CLASSPATH_ENTRY_SNAPSHOT(parent = CLASSPATH_ENTRY_SNAPSHOT_TRANSFORM, "Save classpath entry snapshot"),
;
companion object {