Browse Source

Use separate cache dispatchers with limited parallelism (#3929)

pull/3930/head
Konstantin 1 year 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.graphics.vector.rememberVectorPainter
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.jetbrains.compose.resources.vector.toImageVector import org.jetbrains.compose.resources.vector.toImageVector
import org.jetbrains.compose.resources.vector.xmldom.Element import org.jetbrains.compose.resources.vector.xmldom.Element
@ -88,6 +90,8 @@ private sealed interface ImageCache {
class Vector(val vector: ImageVector) : ImageCache class Vector(val vector: ImageVector) : ImageCache
} }
@OptIn(ExperimentalCoroutinesApi::class)
private val imageCacheDispatcher = Dispatchers.Default.limitedParallelism(1)
private val imageCache = mutableMapOf<String, ImageCache>() private val imageCache = mutableMapOf<String, ImageCache>()
//@TestOnly //@TestOnly
@ -99,6 +103,6 @@ private suspend fun loadImage(
path: String, path: String,
resourceReader: ResourceReader, resourceReader: ResourceReader,
decode: (ByteArray) -> ImageCache decode: (ByteArray) -> ImageCache
): ImageCache = withContext(cacheDispatcher) { ): ImageCache = withContext(imageCacheDispatcher) {
imageCache.getOrPut(path) { decode(resourceReader.read(path)) } 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 package org.jetbrains.compose.resources
import kotlinx.coroutines.CoroutineDispatcher
internal typealias ResourceId = String internal typealias ResourceId = String
@RequiresOptIn("This API is experimental and is likely to change in the future.") @RequiresOptIn("This API is experimental and is likely to change in the future.")
annotation class ExperimentalResourceApi annotation class ExperimentalResourceApi
internal expect val cacheDispatcher: CoroutineDispatcher

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

Loading…
Cancel
Save