diff --git a/app/src/main/java/com/example/reddittube/ui/main/components/MinimalButton.kt b/app/src/main/java/com/example/reddittube/ui/main/components/MinimalButton.kt index 056265a..8879150 100644 --- a/app/src/main/java/com/example/reddittube/ui/main/components/MinimalButton.kt +++ b/app/src/main/java/com/example/reddittube/ui/main/components/MinimalButton.kt @@ -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 diff --git a/app/src/main/java/com/example/reddittube/util/AdaptiveCacheEngine.kt b/app/src/main/java/com/example/reddittube/util/AdaptiveCacheEngine.kt index 177860c..1f9df17 100644 --- a/app/src/main/java/com/example/reddittube/util/AdaptiveCacheEngine.kt +++ b/app/src/main/java/com/example/reddittube/util/AdaptiveCacheEngine.kt @@ -97,10 +97,25 @@ class AdaptiveCacheEngine( 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) }