Skip to content
This repository was archived by the owner on Jul 8, 2022. It is now read-only.

Commit 43b37d6

Browse files
committed
Kotlin 1.4.30 + Circumvent JS-IR performance issue with min/max + Use FastArrayList on Poly2Tri
1 parent 966fdaa commit 43b37d6

File tree

16 files changed

+84
-55
lines changed

16 files changed

+84
-55
lines changed

gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# sytleguide
22
kotlin.code.style=official
33

4-
# Kotlin 1.4.30-RC: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
5-
easyPluginVersion=0.12.5
4+
# Kotlin 1.4.30: https://github.com/korlibs/easy-kotlin-mpp-gradle-plugin
5+
easyPluginVersion=0.13.0
66

77
# version
88
group=com.soywiz.korlibs.korma
99
version=2.0.0-SNAPSHOT
1010

1111
# korlibs
12-
kdsVersion=2.0.5
12+
kdsVersion=2.0.6
1313

1414
# bintray location
1515
project.bintray.org=korlibs

korma-shape/src/commonMain/kotlin/com.soywiz.korma.triangle/internal/Poly2Tri.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ entities.
4242

4343
package com.soywiz.korma.triangle.internal
4444

45+
import com.soywiz.kds.*
4546
import com.soywiz.korma.geom.*
4647
import com.soywiz.korma.geom.triangle.*
4748
import kotlin.math.*
@@ -855,7 +856,7 @@ internal data class PolyTriangle internal constructor(
855856
override var p1: IPoint,
856857
override var p2: IPoint
857858
) : Triangle {
858-
val neighbors: ArrayList<PolyTriangle?> = ArrayList<PolyTriangle?>(3).apply { add(null); add(null); add(null) } as ArrayList<PolyTriangle?> // Neighbor list
859+
val neighbors: FastArrayList<PolyTriangle?> = FastArrayList<PolyTriangle?>(3).apply { add(null); add(null); add(null) } // Neighbor list
859860
var interior: Boolean = false // Has this triangle been marked as an interior triangle?
860861
val constrained_edge = BooleanArray(3) // Flags to determine if an edge is a Constrained edge
861862
val delaunay_edge = BooleanArray(3) // Flags to determine if an edge is a Delauney edge

korma-shape/src/commonMain/kotlin/com/soywiz/korma/geom/shape/ops/internal/Clipper.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,7 @@ internal class ClipperOffset(private val miterLimit: Double = 2.0, private val a
778778

779779
private fun doRound(j: Int, k: Int) {
780780
val a = atan2(inA, normals[k].x * normals[j].x + normals[k].y * normals[j].y)
781-
val steps = kotlin.math.max(round(stepsPerRad * abs(a)).toInt(), 1)
781+
val steps = max2(round(stepsPerRad * abs(a)).toInt(), 1)
782782

783783
var x = normals[k].x
784784
var y = normals[k].y
@@ -2826,19 +2826,19 @@ internal class DefaultClipper(initOptions: Int = 0) : ClipperBase(Clipper.PRESER
28262826
): Boolean {
28272827
if (a1 < a2) {
28282828
if (b1 < b2) {
2829-
Left[0] = kotlin.math.max(a1, b1)
2830-
Right[0] = kotlin.math.min(a2, b2)
2829+
Left[0] = max2(a1, b1)
2830+
Right[0] = min2(a2, b2)
28312831
} else {
2832-
Left[0] = kotlin.math.max(a1, b2)
2833-
Right[0] = kotlin.math.min(a2, b1)
2832+
Left[0] = max2(a1, b2)
2833+
Right[0] = min2(a2, b1)
28342834
}
28352835
} else {
28362836
if (b1 < b2) {
2837-
Left[0] = kotlin.math.max(a2, b1)
2838-
Right[0] = kotlin.math.min(a1, b2)
2837+
Left[0] = max2(a2, b1)
2838+
Right[0] = min2(a1, b2)
28392839
} else {
2840-
Left[0] = kotlin.math.max(a2, b2)
2841-
Right[0] = kotlin.math.min(a1, b1)
2840+
Left[0] = max2(a2, b2)
2841+
Right[0] = min2(a1, b1)
28422842
}
28432843
}
28442844
return Left[0] < Right[0]

korma-shape/src/commonMain/kotlin/com/soywiz/korma/geom/shape/ops/internal/ClipperExt.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ internal fun Path.toShape2d(): Shape2d {
1515
val bl = this[(n + 3) % 4]
1616

1717
if ((tl.x == bl.x) && (tr.x == br.x) && (tl.y == tr.y) && (bl.y == br.y)) {
18-
val xmin = kotlin.math.min(tl.x, tr.x)
19-
val xmax = kotlin.math.max(tl.x, tr.x)
20-
val ymin = kotlin.math.min(tl.y, bl.y)
21-
val ymax = kotlin.math.max(tl.y, bl.y)
18+
val xmin = min2(tl.x, tr.x)
19+
val xmax = max2(tl.x, tr.x)
20+
val ymin = min2(tl.y, bl.y)
21+
val ymax = max2(tl.y, bl.y)
2222
//println("($xmin,$ymin)-($xmax-$ymax) : $tl,$tr,$br,$bl")
2323
return Shape2d.Rectangle(xmin, ymin, xmax - xmin, ymax - ymin)
2424
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.soywiz.korma.geom.shape.ops.internal
2+
3+
@PublishedApi internal fun min2(a: Int, b: Int) = if (a < b) a else b
4+
@PublishedApi internal fun max2(a: Int, b: Int) = if (a > b) a else b
5+
6+
@PublishedApi internal fun min2(a: Float, b: Float) = if (a < b) a else b
7+
@PublishedApi internal fun max2(a: Float, b: Float) = if (a > b) a else b
8+
9+
@PublishedApi internal fun min2(a: Double, b: Double) = if (a < b) a else b
10+
@PublishedApi internal fun max2(a: Double, b: Double) = if (a > b) a else b

korma/src/commonMain/kotlin/com/soywiz/korma/geom/Rectangle.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ data class Rectangle(
106106
infix fun intersection(that: Rectangle) = intersection(that, Rectangle())
107107

108108
fun intersection(that: Rectangle, target: Rectangle = Rectangle()) = if (this intersects that) target.setBounds(
109-
kotlin.math.max(this.left, that.left), kotlin.math.max(this.top, that.top),
110-
kotlin.math.min(this.right, that.right), kotlin.math.min(this.bottom, that.bottom)
109+
max2(this.left, that.left), max2(this.top, that.top),
110+
min2(this.right, that.right), min2(this.bottom, that.bottom)
111111
) else null
112112

113113
fun displaced(dx: Double, dy: Double) = Rectangle(this.x + dx, this.y + dy, width, height)
@@ -327,10 +327,10 @@ fun Iterable<IRectangle>.bounds(target: Rectangle = Rectangle()): Rectangle {
327327
bottom = r.bottom
328328
first = false
329329
} else {
330-
left = kotlin.math.min(left, r.left)
331-
right = kotlin.math.max(right, r.right)
332-
top = kotlin.math.min(top, r.top)
333-
bottom = kotlin.math.max(bottom, r.bottom)
330+
left = min2(left, r.left)
331+
right = max2(right, r.right)
332+
top = min2(top, r.top)
333+
bottom = max2(bottom, r.bottom)
334334
}
335335
}
336336
return target.setBounds(left, top, right, bottom)

korma/src/commonMain/kotlin/com/soywiz/korma/geom/ScaleMode.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.soywiz.korma.geom
22

3+
import com.soywiz.korma.internal.*
4+
import com.soywiz.korma.internal.max2
35
import kotlin.native.concurrent.ThreadLocal
46

57
class ScaleMode(
@@ -27,14 +29,14 @@ class ScaleMode(
2729
val COVER = ScaleMode { c, iw, ih, cw, ch ->
2830
val s0 = cw / iw
2931
val s1 = ch / ih
30-
val s = kotlin.math.max(s0, s1)
32+
val s = max2(s0, s1)
3133
if (c == 0) iw * s else ih * s
3234
}
3335

3436
val SHOW_ALL = ScaleMode { c, iw, ih, cw, ch ->
3537
val s0 = cw / iw
3638
val s1 = ch / ih
37-
val s = kotlin.math.min(s0, s1)
39+
val s = min2(s0, s1)
3840
if (c == 0) iw * s else ih * s
3941
}
4042

korma/src/commonMain/kotlin/com/soywiz/korma/geom/Size.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ interface ISize {
99

1010
val area: Double get() = width * height
1111
val perimeter: Double get() = width * 2 + height * 2
12-
val min: Double get() = kotlin.math.min(width, height)
13-
val max: Double get() = kotlin.math.max(width, height)
12+
val min: Double get() = min2(width, height)
13+
val max: Double get() = max2(width, height)
1414

1515
companion object {
1616
operator fun invoke(width: Double, height: Double): ISize = Size(Point(width, height))

korma/src/commonMain/kotlin/com/soywiz/korma/geom/binpack/BinPacker.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class BinPacker(val width: Double, val height: Double, val algo: Algo = MaxRects
99

1010
class Result<T>(val maxWidth: Double, val maxHeight: Double, val items: List<Pair<T, Rectangle?>>) {
1111
private val rectanglesNotNull = items.map { it.second }.filterNotNull()
12-
val width: Double = rectanglesNotNull.map { it.right }.max() ?: 0.0
13-
val height: Double = rectanglesNotNull.map { it.bottom }.max() ?: 0.0
12+
val width: Double = rectanglesNotNull.map { it.right }.maxOrNull() ?: 0.0
13+
val height: Double = rectanglesNotNull.map { it.bottom }.maxOrNull() ?: 0.0
1414
val rects: List<Rectangle?> get() = items.map { it.second }
1515
val rectsStr: String get() = rects.toString()
1616
}

korma/src/commonMain/kotlin/com/soywiz/korma/geom/shape/Shape2d.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ inline fun approximateCurve(
178178
crossinline compute: (ratio: Double, get: (x: Double, y: Double) -> Unit) -> Unit,
179179
crossinline emit: (x: Double, y: Double) -> Unit
180180
) {
181-
val rcurveSteps = max(curveSteps, 20)
181+
val rcurveSteps = max2(curveSteps, 20)
182182
val dt = 1.0 / rcurveSteps
183183
var lastX = 0.0
184184
var lastY = 0.0
@@ -218,10 +218,10 @@ fun IPointArrayList.toRectangleOrNull(): Shape2d.Rectangle? {
218218
val ys = setOf(getY(0), getY(1), getY(2), getY(3))
219219
if (xs.size != 2 || ys.size != 2) return null
220220
//get coordinates
221-
val left = xs.min() ?: return null
222-
val right = xs.max() ?: return null
223-
val top = ys.max() ?: return null
224-
val bottom = ys.min() ?: return null
221+
val left = xs.minOrNull() ?: return null
222+
val right = xs.maxOrNull() ?: return null
223+
val top = ys.maxOrNull() ?: return null
224+
val bottom = ys.minOrNull() ?: return null
225225
return Shape2d.Rectangle(Rectangle.fromBounds(top, left, right, bottom))
226226
}
227227

0 commit comments

Comments
 (0)