Browse Source

Use separate cache dispatchers with limited parallelism (#3929)

pull/3930/head
Konstantin 12 months ago committed by GitHub
parent
commit
7505624aa0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      components/resources/library/src/blockingMain/kotlin/org/jetbrains/compose/resources/Resource.blocking.kt
  2. 6
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageResources.kt
  3. 6
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/Resource.kt
  4. 6
      components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt
  5. 5
      components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/Resource.js.kt

9
components/resources/library/src/blockingMain/kotlin/org/jetbrains/compose/resources/Resource.blocking.kt

@ -1,9 +0,0 @@
package org.jetbrains.compose.resources
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.newSingleThreadContext
@OptIn(ExperimentalCoroutinesApi::class, DelicateCoroutinesApi::class)
internal actual val cacheDispatcher: CoroutineDispatcher = newSingleThreadContext("resources_cache_ctx")

6
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/ImageResources.kt

@ -9,6 +9,8 @@ import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.withContext
import org.jetbrains.compose.resources.vector.toImageVector
import org.jetbrains.compose.resources.vector.xmldom.Element
@ -88,6 +90,8 @@ private sealed interface ImageCache {
class Vector(val vector: ImageVector) : ImageCache
}
@OptIn(ExperimentalCoroutinesApi::class)
private val imageCacheDispatcher = Dispatchers.Default.limitedParallelism(1)
private val imageCache = mutableMapOf<String, ImageCache>()
//@TestOnly
@ -99,6 +103,6 @@ private suspend fun loadImage(
path: String,
resourceReader: ResourceReader,
decode: (ByteArray) -> ImageCache
): ImageCache = withContext(cacheDispatcher) {
): ImageCache = withContext(imageCacheDispatcher) {
imageCache.getOrPut(path) { decode(resourceReader.read(path)) }
}

6
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/Resource.kt

@ -1,10 +1,6 @@
package org.jetbrains.compose.resources
import kotlinx.coroutines.CoroutineDispatcher
internal typealias ResourceId = String
@RequiresOptIn("This API is experimental and is likely to change in the future.")
annotation class ExperimentalResourceApi
internal expect val cacheDispatcher: CoroutineDispatcher
annotation class ExperimentalResourceApi

6
components/resources/library/src/commonMain/kotlin/org/jetbrains/compose/resources/StringResources.kt

@ -2,6 +2,8 @@ package org.jetbrains.compose.resources
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.withContext
import org.jetbrains.compose.resources.vector.xmldom.Element
import org.jetbrains.compose.resources.vector.xmldom.NodeList
@ -14,6 +16,8 @@ private sealed interface StringItem {
data class Array(val items: List<String>) : StringItem
}
@OptIn(ExperimentalCoroutinesApi::class)
private val stringsCacheDispatcher = Dispatchers.Default.limitedParallelism(1)
private val parsedStringsCache = mutableMapOf<String, Map<String, StringItem>>()
//@TestOnly
@ -22,7 +26,7 @@ internal fun dropStringsCache() {
}
private suspend fun getParsedStrings(path: String, resourceReader: ResourceReader): Map<String, StringItem> =
withContext(cacheDispatcher) {
withContext(stringsCacheDispatcher) {
parsedStringsCache.getOrPut(path) {
val nodes = resourceReader.read(path).toXmlElement().childNodes
val strings = nodes.getElementsWithName("string").associate { element ->

5
components/resources/library/src/jsMain/kotlin/org/jetbrains/compose/resources/Resource.js.kt

@ -1,10 +1,5 @@
package org.jetbrains.compose.resources
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
internal actual val cacheDispatcher: CoroutineDispatcher = Dispatchers.Default
/**
* Represents the configuration object for web resources.
*

Loading…
Cancel
Save