Optimised collectConsecutiveImageParagraphs: continue statements for early loop continuation

This commit is contained in:
davotoula
2025-09-13 23:32:17 +02:00
parent bfc920ca04
commit 8f0a829a4d

View File

@@ -20,7 +20,6 @@
*/ */
package com.vitorpamplona.amethyst.ui.components package com.vitorpamplona.amethyst.ui.components
import android.util.Log
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
@@ -434,34 +433,36 @@ private fun collectConsecutiveImageParagraphs(
): Pair<List<ParagraphState>, Int> { ): Pair<List<ParagraphState>, Int> {
val imageParagraphs = mutableListOf<ParagraphState>() val imageParagraphs = mutableListOf<ParagraphState>()
var j = startIndex var j = startIndex
while (j < paragraphs.size) { while (j < paragraphs.size) {
val currentParagraph = paragraphs[j] val currentParagraph = paragraphs[j]
val isEmpty = val words = currentParagraph.words
currentParagraph.words.isEmpty() ||
(
currentParagraph.words.size == 1 &&
currentParagraph.words.first() is RegularTextSegment &&
currentParagraph.words
.first()
.segmentText
.isBlank()
)
val isCurrentImageOnly = // Fast path for empty check
currentParagraph.words.isNotEmpty() && isImageOnlyParagraph(currentParagraph) if (words.isEmpty()) {
j++
continue
}
if (isCurrentImageOnly) { // Check for single whitespace word
if (words.size == 1) {
val firstWord = words.first()
if (firstWord is RegularTextSegment && firstWord.segmentText.isBlank()) {
j++
continue
}
}
// Check if it's an image-only paragraph
if (isImageOnlyParagraph(currentParagraph)) {
imageParagraphs.add(currentParagraph) imageParagraphs.add(currentParagraph)
j++ j++
} else if (isEmpty) {
// Skip empty paragraphs but continue looking for consecutive images
j++
} else { } else {
// Hit a non-empty, non-image paragraph - stop collecting
break break
} }
} }
return Pair(imageParagraphs, j) // Return collected paragraphs and next index to process
return imageParagraphs to j
} }
@OptIn(ExperimentalLayoutApi::class) @OptIn(ExperimentalLayoutApi::class)
@@ -561,8 +562,6 @@ private fun RenderWordsWithImageGallery(
words: ImmutableList<Segment>, words: ImmutableList<Segment>,
context: RenderContext, context: RenderContext,
) { ) {
val startTime = System.currentTimeMillis()
var i = 0 var i = 0
val n = words.size val n = words.size
@@ -609,8 +608,6 @@ private fun RenderWordsWithImageGallery(
i++ i++
} }
} }
Log.d("RichTextViewer", "RenderWordsWithImageGallery took ${System.currentTimeMillis() - startTime}ms for ${words.size} segments")
} }
@Composable @Composable