Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,25 @@ class AdaptiveCacheEngine<K : Any, V : Any>(
val target = dynamicCapacity
val now = SystemClock.elapsedRealtime()
while (store.size > target) {
val candidate = store.filter { (k, n) ->
!isProtectedKey(k) || (now - n.lastAccessedMs > protectedTtlMs)
}.minByOrNull { computeScore(it.value, now) } ?: break
val removedNode = store.remove(candidate.key)
// Optimization: Replace eager map.filter and minByOrNull with an allocation-free loop.
// Expected Impact: Eliminates temporary Map allocations per eviction, significantly reducing
// GC overhead and O(N) memory allocation churn during cache eviction spikes.
var candidateKey: K? = null
var minScore = Double.MAX_VALUE

for ((k, n) in store) {
if (!isProtectedKey(k) || (now - n.lastAccessedMs > protectedTtlMs)) {
val score = computeScore(n, now)
if (score < minScore) {
minScore = score
candidateKey = k
}
}
}

if (candidateKey == null) break

val removedNode = store.remove(candidateKey)
if (removedNode != null) {
currentSizeBytes.addAndGet(-removedNode.sizeBytes)
}
Expand Down
Loading