Skip to content

Commit e847483

Browse files
[PERF] Optimize Compose filtering in PdfViewerScreen
- Category: B — Performance Optimization - Files: app/src/main/java/com/yourname/pdftoolkit/ui/screens/PdfViewerScreen.kt, AI_RUN_LOG.md - Build: PASS - Tests: SKIPPED (pre-existing failures) - Emulator: SKIPPED - Risk: LOW Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent e892e7e commit e847483

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

AI_RUN_LOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,18 @@ Each entry represents one week's focused improvement to the PDF viewer and edito
4747
**Branch:** auto/weekly-20250515-performance-remember-keys
4848
**Notes:**
4949
- `LazyColumn` items in `PdfPagesContent` were running `filter` operations on potentially large lists (`annotations` and `searchState.matches`) on every recomposition. Wrapping these in `remember` with appropriate keys improves scroll performance significantly.
50+
51+
## 2026-05-02
52+
**Status:** SUCCESS ✅
53+
**Category:** B — Performance
54+
**Task:** Reduce unnecessary page re-renders by wrapping inline list filtering in remember blocks.
55+
**Files Changed:**
56+
- app/src/main/java/com/yourname/pdftoolkit/ui/screens/PdfViewerScreen.kt: Wrapped searchState.matches.filter and annotations.filter in remember blocks to prevent O(N) filtering on every scroll recomposition.
57+
**Verification:**
58+
- Build: PASS
59+
- Tests: N/A (pre-existing failures unrelated to changes)
60+
- Emulator: SKIPPED
61+
**Performance Impact:**
62+
- Allocations/Recompositions: Significantly reduced allocations during fast scrolling when searching or viewing annotations in large documents.
63+
**Branch:** auto/weekly-20260502-optimize-compose-filtering
64+
**Notes:** O(N) operations inside LazyColumn items block can severely impact scroll performance. Adding `remember` with proper keys solves this.

app/src/main/java/com/yourname/pdftoolkit/ui/screens/PdfViewerScreen.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,21 +1250,27 @@ private fun PdfPagesContent(
12501250
count = totalPages,
12511251
key = { it }
12521252
) { index ->
1253-
val pageMatches = searchState.matches.filter { it.pageIndex == index }
1253+
val pageMatches = remember(searchState.matches, index) {
1254+
searchState.matches.filter { it.pageIndex == index }
1255+
}
12541256
val currentGlobalResult = searchState.matches.getOrNull(searchState.currentMatchIndex)
12551257
val currentMatchIndexOnPage = if (currentGlobalResult != null && currentGlobalResult.pageIndex == index) {
12561258
pageMatches.indexOf(currentGlobalResult)
12571259
} else {
12581260
-1
12591261
}
12601262

1263+
val pageAnnotations = remember(annotations, index) {
1264+
annotations.filter { it.pageIndex == index }
1265+
}
1266+
12611267
PdfPageWithAnnotations(
12621268
pageIndex = index,
12631269
loadPage = loadPage,
12641270
isEditMode = isEditMode,
12651271
selectedTool = selectedTool,
12661272
selectedColor = selectedColor,
1267-
annotations = annotations.filter { it.pageIndex == index },
1273+
annotations = pageAnnotations,
12681274
currentStroke = if (currentDrawingPageIndex == index) currentStroke else emptyList(),
12691275
onCurrentStrokeChange = { stroke ->
12701276
onDrawingPageIndexChange(index)

0 commit comments

Comments
 (0)