⚡ Bolt: Precompute map grouping for chart interactions - #198
⚡ Bolt: Precompute map grouping for chart interactions#198alvin000009238 wants to merge 1 commit into
Conversation
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 performance of the SubjectTrendLineChart by precomputing and caching point indices and grouping calculations (allPointsByIndexAndBaseName) in the ChartData state, preventing expensive collection operations on the UI thread during gesture detection. The review feedback suggests further refining this optimization by using the standard Kotlin Map interface instead of platform-specific java.util.SortedMap, and avoiding fallback allocations in the gesture detection loop by using return@forEach if the cached data is missing.
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 allPointsMap: Map<String, List<Triple<Int, Double, String>>>, | ||
| val dashedLines: Map<String, List<Triple<Triple<Int, Double, String>, Triple<Int, Double, String>, Color>>> | ||
| val dashedLines: Map<String, List<Triple<Triple<Int, Double, String>, Triple<Int, Double, String>, Color>>>, | ||
| val allPointsByIndexAndBaseName: Map<String, Pair<List<Int>, java.util.SortedMap<Int, List<Triple<Int, Double, String>>>>> |
There was a problem hiding this comment.
Exposing platform-specific types like java.util.SortedMap in the properties of ChartData is less idiomatic in Kotlin. Since we only perform standard map lookups on pointsByIndex during gesture detection, we can use the standard Kotlin Map interface instead.
| val allPointsByIndexAndBaseName: Map<String, Pair<List<Int>, java.util.SortedMap<Int, List<Triple<Int, Double, String>>>>> | |
| val allPointsByIndexAndBaseName: Map<String, Pair<List<Int>, Map<Int, List<Triple<Int, Double, String>>>>> |
| val groupedSubjects = subjectPoints.keys.groupBy { getSubjectBaseName(it) } | ||
| val allPointsMap = mutableMapOf<String, List<Triple<Int, Double, String>>>() | ||
| val dashedLines = mutableMapOf<String, List<Triple<Triple<Int, Double, String>, Triple<Int, Double, String>, Color>>>() | ||
| val allPointsByIndexAndBaseName = mutableMapOf<String, Pair<List<Int>, java.util.SortedMap<Int, List<Triple<Int, Double, String>>>>>() |
There was a problem hiding this comment.
Update the local variable type to use the standard Kotlin Map interface instead of java.util.SortedMap to match the updated property type in ChartData.
| val allPointsByIndexAndBaseName = mutableMapOf<String, Pair<List<Int>, java.util.SortedMap<Int, List<Triple<Int, Double, String>>>>>() | |
| val allPointsByIndexAndBaseName = mutableMapOf<String, Pair<List<Int>, Map<Int, List<Triple<Int, Double, String>>>>>() |
| val pointsByIndex = allPoints.groupBy { it.first }.toSortedMap() | ||
| val indices = pointsByIndex.keys.toList() | ||
| // [Performance Optimization] Retrieve precomputed indices and map to avoid reallocation | ||
| val (indices, pointsByIndex) = chartData.allPointsByIndexAndBaseName[baseName] ?: Pair(emptyList(), sortedMapOf()) |
There was a problem hiding this comment.
Using Pair(emptyList(), sortedMapOf()) as a fallback allocates a new Pair and a new SortedMap (via sortedMapOf()) on the UI thread during gesture detection if the key is missing.\n\nSince allPointsByIndexAndBaseName is guaranteed to contain the data for any non-empty baseName (which we already checked and returned early for if empty), we can safely use return@forEach to skip the iteration entirely if the data is missing. This avoids any potential fallback allocations.
| val (indices, pointsByIndex) = chartData.allPointsByIndexAndBaseName[baseName] ?: Pair(emptyList(), sortedMapOf()) | |
| val (indices, pointsByIndex) = chartData.allPointsByIndexAndBaseName[baseName] ?: return@forEach |
💡 What:
Precomputed
allPointsByIndexAndBaseNameinside theChartDatarememberblock to cache grouped and sorted point maps.🎯 Why:
The
SubjectTrendLineChartpreviously executed a costly.groupBy { it.first }.toSortedMap()operation directly inside thedetectTapGesturesonTapclosure for every single tap event. This blocking allocation and sort operation runs on the main UI thread during gesture recognition. Caching the output avoids reallocation on interaction since the chart's points list is static while rendered.📊 Impact:
Greatly reduces allocation overhead and main thread blocking time during pointer interactions with the chart by pre-evaluating the indices ahead of time.
🔬 Measurement:
Ran an isolated loop benchmark mirroring the map collection operation over 100 points iterating 10,000 times:
PR created automatically by Jules for task 11612514362841281618 started by @alvin000009238