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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
## 2024-10-06 - O(N^2) Lookup Optimization in List
**Learning:** Using `List.none { it.id == post.id }` within a loop creates an O(N^2) complexity because `none` iterates over the list which grows linearly.
**Action:** Replace `List.none` or `List.contains` checks on growing collections with an O(1) `Set.add` tracking mechanism when maintaining uniqueness.
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ class DefaultDataRepository(private val context: Context) : DataRepository {
val token = RedditOAuthHelper.getOrFetchAccessToken(context)
if (token == null) { emit(SearchVideosResult(emptyList(), null)); return@flow }
val results = mutableListOf<RedditPost>()
val seenIds = mutableSetOf<String>()
var currentAfter = after
var finalAfter: String? = null
try {
Expand All @@ -276,7 +277,8 @@ class DefaultDataRepository(private val context: Context) : DataRepository {
for (i in 0 until children.length()) {
val childData = children.getJSONObject(i).optJSONObject("data") ?: continue
parseRedditPost(childData)?.let { post ->
if (results.none { it.id == post.id }) {
// Performance optimization: Avoid O(N^2) list search by using a Set for seen IDs
if (seenIds.add(post.id)) {
results.add(post)
}
}
Expand Down
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
Loading