Skip to content

Commit

Permalink
Fix #1228 : Make GeomMeta thread-safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
alshan committed Nov 4, 2024
1 parent eed5a63 commit 6d95b8b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 33 deletions.
5 changes: 3 additions & 2 deletions future_changes.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
## [4.5.2] - 2024-mm-dd
## [4.5.2] - 2024-11-dd

### Added

### Changed

### Fixed

- Kandy toPNG reports NullPointerException [[#1228](https://github.com/JetBrains/lets-plot/issues/1228)]
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,8 @@
package org.jetbrains.letsPlot.core.plot.base

import org.jetbrains.letsPlot.commons.values.Color
import kotlin.native.concurrent.ThreadLocal

// In Kotlin Native objects a frozen by default. Annotate with `ThreadLocal` to unfreeze.
// See: https://github.com/JetBrains/kotlin-native/blob/master/IMMUTABILITY.md
// Required mutations:
// - `renderedAesByGeom` map
@ThreadLocal
object GeomMeta {
private val renderedAesByGeom = HashMap<GeomKind, List<Aes<*>>>()

private fun renders(geomKind: GeomKind): List<Aes<*>> {
if (!renderedAesByGeom.containsKey(geomKind)) {
renderedAesByGeom[geomKind] =
renderedAesList(geomKind)
}
return renderedAesByGeom[geomKind]!!
}

fun renders(
geomKind: GeomKind,
actualColorAes: Aes<Color>,
actualFillAes: Aes<Color>,
exclude: List<Aes<*>> = emptyList()
): List<Aes<*>> {
return (renders(geomKind) - exclude).map {
when (it) {
Aes.COLOR -> actualColorAes
Aes.FILL -> actualFillAes
else -> it
}
}
}

private val POINT = listOf(
Aes.X, Aes.Y,
Expand Down Expand Up @@ -81,11 +51,30 @@ object GeomMeta {
Aes.ALPHA
)

private val RENDERED_AES_BY_GEOM: Map<GeomKind, List<Aes<*>>> = GeomKind.entries.associateWith {
renderedAesList(it)
}

fun renders(
geomKind: GeomKind,
actualColorAes: Aes<Color>,
actualFillAes: Aes<Color>,
exclude: List<Aes<*>> = emptyList()
): List<Aes<*>> {
return (RENDERED_AES_BY_GEOM.getValue(geomKind) - exclude).map {
when (it) {
Aes.COLOR -> actualColorAes
Aes.FILL -> actualFillAes
else -> it
}
}
}

private fun renderedAesList(geomKind: GeomKind): List<Aes<*>> {
return when (geomKind) {
GeomKind.POINT,
GeomKind.BLANK-> POINT
GeomKind.BLANK -> POINT

GeomKind.PATH -> PATH
GeomKind.LINE -> PATH

Expand Down

0 comments on commit 6d95b8b

Please sign in to comment.