⚡ Bolt: [O(N^3) to O(N^2) nested loop optimization in SubjectTrendLineChart] - #206
⚡ Bolt: [O(N^3) to O(N^2) nested loop optimization in SubjectTrendLineChart]#206alvin000009238 wants to merge 1 commit into
Conversation
…Chart Co-authored-by: alvin000009238 <107313913+alvin000009238@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Code Review
This pull request optimizes the rendering logic in SubjectTrendLineChart by precomputing subject sets to reduce lookup complexity within nested loops. The reviewer suggested further performance improvements, such as hoisting independent computations out of the nested loops and using mapTo(HashSet()) to avoid intermediate list allocations.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| val currentSubjects = currentPoints.map { it.third }.toSet() | ||
| val nextSubjects = nextPoints.map { it.third }.toSet() | ||
|
|
||
| currentPoints.forEach { p1 -> | ||
| nextPoints.forEach { p2 -> | ||
| val p1HasSuccessor = nextPoints.any { it.third == p1.third } | ||
| val p2HasPredecessor = currentPoints.any { it.third == p2.third } | ||
| val p1HasSuccessor = nextSubjects.contains(p1.third) | ||
| val p2HasPredecessor = currentSubjects.contains(p2.third) |
There was a problem hiding this comment.
While the introduction of Set lookups successfully reduces the complexity from
-
p1HasSuccessoronly depends onp1andnextSubjects. It can be hoisted to the outer loop, reducing its lookups from$N \times M$ to$N$ . -
p2HasPredecessoronly depends onp2andcurrentSubjects. We can precompute this for allnextPointsbefore entering the loops, reducing its lookups from$N \times M$ to$M$ . -
mapTo(HashSet())can be used instead ofmap { ... }.toSet()to avoid intermediate list allocations.
This reduces the total number of set lookups from
| val currentSubjects = currentPoints.map { it.third }.toSet() | |
| val nextSubjects = nextPoints.map { it.third }.toSet() | |
| currentPoints.forEach { p1 -> | |
| nextPoints.forEach { p2 -> | |
| val p1HasSuccessor = nextPoints.any { it.third == p1.third } | |
| val p2HasPredecessor = currentPoints.any { it.third == p2.third } | |
| val p1HasSuccessor = nextSubjects.contains(p1.third) | |
| val p2HasPredecessor = currentSubjects.contains(p2.third) | |
| val currentSubjects = currentPoints.mapTo(HashSet()) { it.third } | |
| val nextSubjects = nextPoints.mapTo(HashSet()) { it.third } | |
| val p2HasPredecessorList = nextPoints.map { currentSubjects.contains(it.third) } | |
| currentPoints.forEach { p1 -> | |
| val p1HasSuccessor = nextSubjects.contains(p1.third) | |
| nextPoints.forEachIndexed { p2Index, p2 -> | |
| val p2HasPredecessor = p2HasPredecessorList[p2Index] |
💡 What:
Replaced O(N)
.any {}list scans inside a double loop with O(1).contains()checks on pre-computedSets inSubjectTrendLineChart.kt.🎯 Why:
The original implementation had an O(N^3) time complexity due to nested loops (
currentPoints.forEach->nextPoints.forEach->list.any). Pre-computing sets for thethirdvalue attribute reduces this to O(N^2), removing repeated iteration in UI drawing logic where performance is critical.📊 Impact:
The optimization significantly reduces CPU pressure during the execution of drawing logic in Jetpack Compose, minimizing the risk of frame drops.
🔬 Measurement:
A custom micro-benchmark (SubjectTrendLineChartPerformanceTest) was run against the original and optimized implementations using simulated data:
(Benchmark test script used for measurement was intentionally discarded to avoid adding unmaintained test code to the codebase.)
PR created automatically by Jules for task 13423601824762943453 started by @alvin000009238