diff --git a/.jules/bolt.md b/.jules/bolt.md index 8f342ab..c5e5f39 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -3,3 +3,6 @@ ## 2024-05-24 - O(N) Iteration in Cache Size Calculation **Learning:** Calculating dynamic capacity dynamically via `store.values.sumOf { it.sizeBytes }` effectively incurs an O(N) cost on every `put` because it runs when `trimToCapacity()` calls `dynamicCapacity`. When cache sizes get large, this causes O(N²) overall insertion performance and UI stutters. **Action:** Use an `AtomicLong` (O(1)) to track the running sum of cache size instead of dynamically iterating through the entire collection. +## 2023-10-05 - [Compose LazyList Performance] +**Learning:** Using `indexOf(item)` inside Compose `items()` loops in a `LazyVerticalGrid` or `LazyColumn` causes unnecessary O(N) operations during recomposition, which leads to dropped frames and laggy scrolling, especially for large lists. +**Action:** Always use `itemsIndexed()` when the index is needed inside the `item` block to provide an O(1) index lookup and ensure smooth scrolling performance. diff --git a/app/src/main/java/com/example/reddittube/ui/main/HomeScreen.kt b/app/src/main/java/com/example/reddittube/ui/main/HomeScreen.kt index 833ef62..f45941a 100644 --- a/app/src/main/java/com/example/reddittube/ui/main/HomeScreen.kt +++ b/app/src/main/java/com/example/reddittube/ui/main/HomeScreen.kt @@ -629,11 +629,13 @@ private fun BrowseGrid( horizontalArrangement = Arrangement.spacedBy(10.dp), verticalArrangement = Arrangement.spacedBy(10.dp) ) { - items( + // ⚡ Bolt: Performance optimization + // Impact: Prevents O(N) lookup (indexOf) for every visible item during composition by using itemsIndexed. + // Measurement: Scrolling through large feed lists will be noticeably smoother with fewer dropped frames. + itemsIndexed( items = distinctData, - key = { post -> post.id } - ) { post -> - val index = distinctData.indexOf(post).coerceAtLeast(0) + key = { _, post -> post.id } + ) { index, post -> VideoCard( post = post, isLiked = likedIds.contains(post.id), 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