Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce variance in benchmarks #418

Merged
merged 1 commit into from
Nov 24, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import coil3.compose.AsyncImage
@Composable
internal fun ImageItem(
text: String,
index: Int = -1,
modifier: Modifier = Modifier,
) {
Surface(modifier) {
Box {
AsyncImage(
model = rememberRandomSampleImageUrl(width = 400),
model = rememberRandomSampleImageUrl(index),
contentScale = ContentScale.Crop,
contentDescription = null,
modifier = Modifier.fillMaxSize(),
Expand All @@ -54,20 +55,19 @@ internal fun ImageItem(
}
}

private val rangeForRandom = (0..100000)
val precannedImageUrls: List<String> by lazy {
(0 until 50).map { randomSampleImageUrl() }
}

private val rangeForRandom = (0..100_000)

fun randomSampleImageUrl(
seed: Int = rangeForRandom.random(),
width: Int = 300,
width: Int = 800,
height: Int = width,
): String = "https://picsum.photos/seed/$seed/$width/$height"

/**
* Remember a URL generate by [randomSampleImageUrl].
*/
@Composable
fun rememberRandomSampleImageUrl(
seed: Int = rangeForRandom.random(),
width: Int = 300,
height: Int = width,
): String = rememberSaveable { randomSampleImageUrl(seed, width, height) }
fun rememberRandomSampleImageUrl(index: Int = -1): String = rememberSaveable {
precannedImageUrls.getOrNull(index) ?: randomSampleImageUrl()
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fun ImagesList(navigator: Navigator) {
.height(160.dp),
) {
AsyncImage(
model = rememberRandomSampleImageUrl(width = 800),
model = rememberRandomSampleImageUrl(index),
contentScale = ContentScale.Crop,
contentDescription = null,
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fun ListOverImage(navigator: Navigator) {

Box {
AsyncImage(
model = rememberRandomSampleImageUrl(width = 800),
model = rememberRandomSampleImageUrl(0),
contentScale = ContentScale.Crop,
contentDescription = null,
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fun MaterialsSample(@Suppress("UNUSED_PARAMETER") navigator: Navigator) {

Box {
AsyncImage(
model = rememberRandomSampleImageUrl(width = 720, height = 1280),
model = rememberRandomSampleImageUrl(),
contentScale = ContentScale.Crop,
contentDescription = null,
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import coil3.SingletonImageLoader
import coil3.compose.LocalPlatformContext
import coil3.request.ImageRequest

val Samples = listOf(
Sample("Scaffold") { ScaffoldSample(it) },
Expand Down Expand Up @@ -74,6 +78,16 @@ fun Samples(
Navigator { currentSample = null }
}

val coilPlatformContext = LocalPlatformContext.current
LaunchedEffect(coilPlatformContext) {
// Preload the first 20 precanned image urls
val imageLoader = SingletonImageLoader.get(coilPlatformContext)
precannedImageUrls
.asSequence()
.map { ImageRequest.Builder(coilPlatformContext).data(it).build() }
.forEach { imageLoader.enqueue(it) }
}

Crossfade(
targetState = currentSample,
modifier = Modifier.testTagsAsResourceId(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ fun ScaffoldSample(
scrolledContainerColor = Color.Transparent,
),
modifier = Modifier
.hazeChild(hazeState) {
this.style = style

.hazeChild(state = hazeState, style = style) {
when (mode) {
ScaffoldSampleMode.Default -> Unit
ScaffoldSampleMode.Progressive -> {
Expand Down Expand Up @@ -134,6 +132,7 @@ fun ScaffoldSample(
items(50) { index ->
ImageItem(
text = "${index + 1}",
index = index,
modifier = Modifier
.fillMaxWidth()
.aspectRatio(3 / 4f),
Expand Down