From cdf5ff13a9427a3f1357744dc8e43e916f69d019 Mon Sep 17 00:00:00 2001 From: Artur Babichev Date: Sun, 10 Sep 2023 13:35:11 +0400 Subject: [PATCH] optimize running --- .../com/softartdev/shared/BenchmarkState.kt | 27 +++++------ .../shared/material3/BenchmarkBody.kt | 45 ++++++++++--------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/sample/shared/src/commonMain/kotlin/com/softartdev/shared/BenchmarkState.kt b/sample/shared/src/commonMain/kotlin/com/softartdev/shared/BenchmarkState.kt index cc9c1f3..43ee1b7 100644 --- a/sample/shared/src/commonMain/kotlin/com/softartdev/shared/BenchmarkState.kt +++ b/sample/shared/src/commonMain/kotlin/com/softartdev/shared/BenchmarkState.kt @@ -13,7 +13,8 @@ object BenchmarkState { val n: MutableState = mutableStateOf(value = 100) val tasks: SnapshotStateList = mutableStateListOf() var dispatcherType: MutableState = mutableStateOf(value = DispatcherType.IO) - val showLoading: State = derivedStateOf { tasks.any { it.percent.value < 100 } } + val showLoading: State = derivedStateOf { tasks.any(::isWorking) } + private fun isWorking(task: BenchmarkTask): Boolean = task.job?.isActive ?: false val parallelDispatcher: CoroutineDispatcher get() = when (dispatcherType.value) { @@ -26,31 +27,27 @@ object BenchmarkState { var supervisorJob: CompletableJob = SupervisorJob() var coroutineScope: CoroutineScope = CoroutineScope(parallelDispatcher + supervisorJob) - fun release() { - coroutineScope.cancel() - tasks.clear() - } - // coroutines parallel execution with dispatcher fun runTasks() { if (tasks.isNotEmpty()) { release() } - for (i in 0 until n.value) { - val task = BenchmarkTask(id = i) - tasks.add(task) - } supervisorJob = SupervisorJob() coroutineScope = CoroutineScope(parallelDispatcher + supervisorJob) - tasks.forEach { - val job = coroutineScope.launch { - it.percent.value = 0 - it.benchmarkRunnable.run() + for (i in 0 until n.value) { + val task = BenchmarkTask(id = i) + task.job = coroutineScope.launch { + task.benchmarkRunnable.run() } - it.job = job + tasks.add(task) } } + fun release() { + coroutineScope.cancel() + tasks.clear() + } + data class BenchmarkTask( val id: Int = tasks.size, val percent: MutableState = mutableStateOf(value = 0), diff --git a/sample/shared/src/commonMain/kotlin/com/softartdev/shared/material3/BenchmarkBody.kt b/sample/shared/src/commonMain/kotlin/com/softartdev/shared/material3/BenchmarkBody.kt index 68c71e1..0c0ec11 100644 --- a/sample/shared/src/commonMain/kotlin/com/softartdev/shared/material3/BenchmarkBody.kt +++ b/sample/shared/src/commonMain/kotlin/com/softartdev/shared/material3/BenchmarkBody.kt @@ -3,6 +3,8 @@ package com.softartdev.shared.material3 import androidx.compose.foundation.layout.* +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.ArrowBack import androidx.compose.material.icons.filled.Delete @@ -38,7 +40,10 @@ fun BenchmarkBody( ) }) { paddingValues -> Box(modifier = Modifier.padding(paddingValues)) { - Column(horizontalAlignment = Alignment.CenterHorizontally) { + Column( + modifier = Modifier.verticalScroll(rememberScrollState()), + horizontalAlignment = Alignment.CenterHorizontally + ) { if (showLoading.value) LinearProgressIndicator(modifier = Modifier.fillMaxWidth()) FlowRow( modifier = Modifier.fillMaxWidth(), @@ -62,9 +67,9 @@ fun BenchmarkBody( color = MaterialTheme.colorScheme.tertiary ) } - val workingThreadsCount: Int by remember { derivedStateOf { - BenchmarkState.tasks.size - BenchmarkState.tasks.count { it.percent.value == 0 } - } } + val workingThreadsCount: Int by remember { + derivedStateOf { BenchmarkState.tasks.size - BenchmarkState.tasks.count { it.percent.value == 0 } } + } FlowRow( modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceAround, @@ -85,21 +90,21 @@ fun BenchmarkBody( @Composable fun DispatcherTypeMenu() { var expanded by remember { mutableStateOf(false) } - SuggestionChip(onClick = { expanded = true }, label = { - Text( - text = "${BenchmarkState.dispatcherType.value.name} ${if (expanded) "▲" else "▼"}", - textAlign = TextAlign.Center - ) - }) - DropdownMenu( - expanded = expanded, - onDismissRequest = { expanded = false } - ) { - DispatcherType.entries.forEach { - DropdownMenuItem(onClick = { - BenchmarkState.dispatcherType.value = it - expanded = false - }, text = { Text(it.name) }) - } + SuggestionChip(onClick = { expanded = true }, label = { + Text( + text = "${BenchmarkState.dispatcherType.value.name} ${if (expanded) "▲" else "▼"}", + textAlign = TextAlign.Center + ) + }) + DropdownMenu( + expanded = expanded, + onDismissRequest = { expanded = false } + ) { + DispatcherType.entries.forEach { + DropdownMenuItem(onClick = { + BenchmarkState.dispatcherType.value = it + expanded = false + }, text = { Text(it.name) }) } + } }