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

Commit

Permalink
Kotlin 1.4.30 + Circumvent JS-IR performance issue with min/max + Use…
Browse files Browse the repository at this point in the history
… FastArrayList on Poly2Tri
  • Loading branch information
soywiz committed Feb 4, 2021
1 parent 966fdaa commit 43b37d6
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 55 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# sytleguide
kotlin.code.style=official

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

# version
group=com.soywiz.korlibs.korma
version=2.0.0-SNAPSHOT

# korlibs
kdsVersion=2.0.5
kdsVersion=2.0.6

# bintray location
project.bintray.org=korlibs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ entities.

package com.soywiz.korma.triangle.internal

import com.soywiz.kds.*
import com.soywiz.korma.geom.*
import com.soywiz.korma.geom.triangle.*
import kotlin.math.*
Expand Down Expand Up @@ -855,7 +856,7 @@ internal data class PolyTriangle internal constructor(
override var p1: IPoint,
override var p2: IPoint
) : Triangle {
val neighbors: ArrayList<PolyTriangle?> = ArrayList<PolyTriangle?>(3).apply { add(null); add(null); add(null) } as ArrayList<PolyTriangle?> // Neighbor list
val neighbors: FastArrayList<PolyTriangle?> = FastArrayList<PolyTriangle?>(3).apply { add(null); add(null); add(null) } // Neighbor list
var interior: Boolean = false // Has this triangle been marked as an interior triangle?
val constrained_edge = BooleanArray(3) // Flags to determine if an edge is a Constrained edge
val delaunay_edge = BooleanArray(3) // Flags to determine if an edge is a Delauney edge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ internal class ClipperOffset(private val miterLimit: Double = 2.0, private val a

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

var x = normals[k].x
var y = normals[k].y
Expand Down Expand Up @@ -2826,19 +2826,19 @@ internal class DefaultClipper(initOptions: Int = 0) : ClipperBase(Clipper.PRESER
): Boolean {
if (a1 < a2) {
if (b1 < b2) {
Left[0] = kotlin.math.max(a1, b1)
Right[0] = kotlin.math.min(a2, b2)
Left[0] = max2(a1, b1)
Right[0] = min2(a2, b2)
} else {
Left[0] = kotlin.math.max(a1, b2)
Right[0] = kotlin.math.min(a2, b1)
Left[0] = max2(a1, b2)
Right[0] = min2(a2, b1)
}
} else {
if (b1 < b2) {
Left[0] = kotlin.math.max(a2, b1)
Right[0] = kotlin.math.min(a1, b2)
Left[0] = max2(a2, b1)
Right[0] = min2(a1, b2)
} else {
Left[0] = kotlin.math.max(a2, b2)
Right[0] = kotlin.math.min(a1, b1)
Left[0] = max2(a2, b2)
Right[0] = min2(a1, b1)
}
}
return Left[0] < Right[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ internal fun Path.toShape2d(): Shape2d {
val bl = this[(n + 3) % 4]

if ((tl.x == bl.x) && (tr.x == br.x) && (tl.y == tr.y) && (bl.y == br.y)) {
val xmin = kotlin.math.min(tl.x, tr.x)
val xmax = kotlin.math.max(tl.x, tr.x)
val ymin = kotlin.math.min(tl.y, bl.y)
val ymax = kotlin.math.max(tl.y, bl.y)
val xmin = min2(tl.x, tr.x)
val xmax = max2(tl.x, tr.x)
val ymin = min2(tl.y, bl.y)
val ymax = max2(tl.y, bl.y)
//println("($xmin,$ymin)-($xmax-$ymax) : $tl,$tr,$br,$bl")
return Shape2d.Rectangle(xmin, ymin, xmax - xmin, ymax - ymin)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.soywiz.korma.geom.shape.ops.internal

@PublishedApi internal fun min2(a: Int, b: Int) = if (a < b) a else b
@PublishedApi internal fun max2(a: Int, b: Int) = if (a > b) a else b

@PublishedApi internal fun min2(a: Float, b: Float) = if (a < b) a else b
@PublishedApi internal fun max2(a: Float, b: Float) = if (a > b) a else b

@PublishedApi internal fun min2(a: Double, b: Double) = if (a < b) a else b
@PublishedApi internal fun max2(a: Double, b: Double) = if (a > b) a else b
12 changes: 6 additions & 6 deletions korma/src/commonMain/kotlin/com/soywiz/korma/geom/Rectangle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ data class Rectangle(
infix fun intersection(that: Rectangle) = intersection(that, Rectangle())

fun intersection(that: Rectangle, target: Rectangle = Rectangle()) = if (this intersects that) target.setBounds(
kotlin.math.max(this.left, that.left), kotlin.math.max(this.top, that.top),
kotlin.math.min(this.right, that.right), kotlin.math.min(this.bottom, that.bottom)
max2(this.left, that.left), max2(this.top, that.top),
min2(this.right, that.right), min2(this.bottom, that.bottom)
) else null

fun displaced(dx: Double, dy: Double) = Rectangle(this.x + dx, this.y + dy, width, height)
Expand Down Expand Up @@ -327,10 +327,10 @@ fun Iterable<IRectangle>.bounds(target: Rectangle = Rectangle()): Rectangle {
bottom = r.bottom
first = false
} else {
left = kotlin.math.min(left, r.left)
right = kotlin.math.max(right, r.right)
top = kotlin.math.min(top, r.top)
bottom = kotlin.math.max(bottom, r.bottom)
left = min2(left, r.left)
right = max2(right, r.right)
top = min2(top, r.top)
bottom = max2(bottom, r.bottom)
}
}
return target.setBounds(left, top, right, bottom)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.soywiz.korma.geom

import com.soywiz.korma.internal.*
import com.soywiz.korma.internal.max2
import kotlin.native.concurrent.ThreadLocal

class ScaleMode(
Expand Down Expand Up @@ -27,14 +29,14 @@ class ScaleMode(
val COVER = ScaleMode { c, iw, ih, cw, ch ->
val s0 = cw / iw
val s1 = ch / ih
val s = kotlin.math.max(s0, s1)
val s = max2(s0, s1)
if (c == 0) iw * s else ih * s
}

val SHOW_ALL = ScaleMode { c, iw, ih, cw, ch ->
val s0 = cw / iw
val s1 = ch / ih
val s = kotlin.math.min(s0, s1)
val s = min2(s0, s1)
if (c == 0) iw * s else ih * s
}

Expand Down
4 changes: 2 additions & 2 deletions korma/src/commonMain/kotlin/com/soywiz/korma/geom/Size.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ interface ISize {

val area: Double get() = width * height
val perimeter: Double get() = width * 2 + height * 2
val min: Double get() = kotlin.math.min(width, height)
val max: Double get() = kotlin.math.max(width, height)
val min: Double get() = min2(width, height)
val max: Double get() = max2(width, height)

companion object {
operator fun invoke(width: Double, height: Double): ISize = Size(Point(width, height))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class BinPacker(val width: Double, val height: Double, val algo: Algo = MaxRects

class Result<T>(val maxWidth: Double, val maxHeight: Double, val items: List<Pair<T, Rectangle?>>) {
private val rectanglesNotNull = items.map { it.second }.filterNotNull()
val width: Double = rectanglesNotNull.map { it.right }.max() ?: 0.0
val height: Double = rectanglesNotNull.map { it.bottom }.max() ?: 0.0
val width: Double = rectanglesNotNull.map { it.right }.maxOrNull() ?: 0.0
val height: Double = rectanglesNotNull.map { it.bottom }.maxOrNull() ?: 0.0
val rects: List<Rectangle?> get() = items.map { it.second }
val rectsStr: String get() = rects.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ inline fun approximateCurve(
crossinline compute: (ratio: Double, get: (x: Double, y: Double) -> Unit) -> Unit,
crossinline emit: (x: Double, y: Double) -> Unit
) {
val rcurveSteps = max(curveSteps, 20)
val rcurveSteps = max2(curveSteps, 20)
val dt = 1.0 / rcurveSteps
var lastX = 0.0
var lastY = 0.0
Expand Down Expand Up @@ -218,10 +218,10 @@ fun IPointArrayList.toRectangleOrNull(): Shape2d.Rectangle? {
val ys = setOf(getY(0), getY(1), getY(2), getY(3))
if (xs.size != 2 || ys.size != 2) return null
//get coordinates
val left = xs.min() ?: return null
val right = xs.max() ?: return null
val top = ys.max() ?: return null
val bottom = ys.min() ?: return null
val left = xs.minOrNull() ?: return null
val right = xs.maxOrNull() ?: return null
val top = ys.maxOrNull() ?: return null
val bottom = ys.minOrNull() ?: return null
return Shape2d.Rectangle(Rectangle.fromBounds(top, left, right, bottom))
}

Expand Down
20 changes: 10 additions & 10 deletions korma/src/commonMain/kotlin/com/soywiz/korma/geom/vector/Edge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ class Edge {
}

fun setToHalf(a: Edge, b: Edge): Edge = this.apply {
val minY = min(a.minY, b.minY)
val maxY = min(a.maxY, b.maxY)
val minY = min2(a.minY, b.minY)
val maxY = min2(a.maxY, b.maxY)
val minX = (a.intersectX(minY) + b.intersectX(minY)) / 2
val maxX = (a.intersectX(maxY) + b.intersectX(maxY)) / 2
setTo(minX, minY, maxX, maxY, +1)
}

val minX get() = min(ax, bx)
val maxX get() = max(ax, bx)
val minY get() = min(ay, by)
val maxY get() = max(ay, by)
val minX get() = min2(ax, bx)
val maxX get() = max2(ax, bx)
val minY get() = min2(ay, by)
val maxY get() = max2(ay, by)

@Suppress("ConvertTwoComparisonsToRangeCheck")
//fun containsY(y: Int): Boolean = if (ay == by) y == ay else if (wind >= 0) y >= ay && y < by else y > ay && y <= by
Expand Down Expand Up @@ -206,10 +206,10 @@ internal class LineSegment(ax: Double, ay: Double, bx: Double, by: Double) {
}
internal data class Line(val ax: Double, val ay: Double, val bx: Double, val by: Double) {
val minX get() = min(ax, bx)
val maxX get() = max(ax, bx)
val minY get() = min(ay, by)
val maxY get() = max(ay, by)
val minX get() = min2(ax, bx)
val maxX get() = max2(ax, bx)
val minY get() = min2(ay, by)
val maxY get() = max2(ay, by)
val isCoplanarX get() = ay == by
val isCoplanarY get() = ax == bx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.soywiz.korma.geom.vector
import com.soywiz.korma.annotations.KorDslMarker
import com.soywiz.korma.geom.*
import com.soywiz.korma.geom.bezier.*
import com.soywiz.korma.internal.*
import com.soywiz.korma.internal.min2
import kotlin.math.*

@KorDslMarker
Expand Down Expand Up @@ -96,7 +98,7 @@ fun VectorBuilder.arc(x: Double, y: Double, r: Double, start: Angle, end: Angle)

val startAngle = start.radians % PI_TWO
val endAngle = end.radians % PI_TWO
var remainingAngle = kotlin.math.min(PI_TWO, abs(endAngle - startAngle))
var remainingAngle = min2(PI_TWO, abs(endAngle - startAngle))
if (remainingAngle == 0.0 && start != end) remainingAngle = PI_TWO
val sgn = if (startAngle < endAngle) +1 else -1
var a1 = startAngle
Expand All @@ -106,7 +108,7 @@ fun VectorBuilder.arc(x: Double, y: Double, r: Double, start: Angle, end: Angle)
val p4 = Point()
var index = 0
while (remainingAngle > EPSILON) {
val a2 = a1 + sgn * kotlin.math.min(remainingAngle, PI_OVER_TWO)
val a2 = a1 + sgn * min2(remainingAngle, PI_OVER_TWO)

val k = 0.5522847498
val a = (a2 - a1) / 2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ internal infix fun Double.umod(other: Double): Double {

@PublishedApi
internal fun floorCeil(v: Double): Double = if (v < 0.0) ceil(v) else floor(v)

@PublishedApi internal fun min2(a: Int, b: Int) = if (a < b) a else b
@PublishedApi internal fun max2(a: Int, b: Int) = if (a > b) a else b

@PublishedApi internal fun min2(a: Float, b: Float) = if (a < b) a else b
@PublishedApi internal fun max2(a: Float, b: Float) = if (a > b) a else b

@PublishedApi internal fun min2(a: Double, b: Double) = if (a < b) a else b
@PublishedApi internal fun max2(a: Double, b: Double) = if (a > b) a else b
7 changes: 5 additions & 2 deletions korma/src/commonMain/kotlin/com/soywiz/korma/math/ArrayExt.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.soywiz.korma.math

import com.soywiz.korma.internal.*
import com.soywiz.korma.internal.max2

fun DoubleArray.minOrElse(nil: Double): Double {
if (isEmpty()) return nil
var out = Double.POSITIVE_INFINITY
for (i in 0..lastIndex) out = kotlin.math.min(out, this[i])
for (i in 0..lastIndex) out = min2(out, this[i])
return out
}

fun DoubleArray.maxOrElse(nil: Double): Double {
if (isEmpty()) return nil
var out = Double.NEGATIVE_INFINITY
for (i in 0..lastIndex) out = kotlin.math.max(out, this[i])
for (i in 0..lastIndex) out = max2(out, this[i])
return out
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ data class RandomWeights<T>(val weightsMap: Map<T, Double>) {

companion object {
private fun normalizeWeights(weights: List<Double>): List<Double> {
val min = weights.min() ?: 0.0
val min = weights.minOrNull() ?: 0.0
return weights.map { (it + min) + 1 }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.soywiz.korma.segment

import com.soywiz.kds.*
import com.soywiz.korma.annotations.*
import com.soywiz.korma.internal.*
import com.soywiz.korma.internal.max2

/**
* Non-overlapping SegmentSet
Expand Down Expand Up @@ -90,8 +92,8 @@ class IntSegmentSet {
fastForEachInterestingRange(min, max) { n, x1, x2 ->
if (intersects(x1, x2, min, max)) {
if (removeStart == -1) removeStart = n
this.min[removeStart] = kotlin.math.min(this.min.getAt(removeStart), kotlin.math.min(x1, min))
this.max[removeStart] = kotlin.math.max(this.max.getAt(removeStart), kotlin.math.max(x2, max))
this.min[removeStart] = min2(this.min.getAt(removeStart), min2(x1, min))
this.max[removeStart] = max2(this.max.getAt(removeStart), max2(x2, max))
removeCount++
}
}
Expand Down Expand Up @@ -140,7 +142,7 @@ class IntSegmentSet {
var count = 0
fastForEachInterestingRange(min, max) { n, x1, x2 ->
if (intersects(x1, x2, min, max)) {
out(kotlin.math.max(x1, min), kotlin.math.min(x2, max))
out(max2(x1, min), min2(x2, max))
count++
}
}
Expand All @@ -162,7 +164,7 @@ class IntSegmentSet {
var count = 0
fastForEach { n, x1, x2 ->
if (intersects(x1, x2, min, max)) {
out(kotlin.math.max(x1, min), kotlin.math.min(x2, max))
out(max2(x1, min), min2(x2, max))
count++
}
}
Expand Down Expand Up @@ -207,7 +209,7 @@ class IntSegmentSet {
// @TODO: In KDS latest versions
@PublishedApi
internal inline fun genericBinarySearchLeft(fromIndex: Int, toIndex: Int, check: (value: Int) -> Int): Int =
genericBinarySearch(fromIndex, toIndex, invalid = { from, to, low, high -> kotlin.math.min(low, high).coerceIn(from, to - 1) }, check = check)
genericBinarySearch(fromIndex, toIndex, invalid = { from, to, low, high -> min2(low, high).coerceIn(from, to - 1) }, check = check)
@PublishedApi
internal inline fun genericBinarySearchRight(fromIndex: Int, toIndex: Int, check: (value: Int) -> Int): Int =
genericBinarySearch(fromIndex, toIndex, invalid = { from, to, low, high -> kotlin.math.max(low, high).coerceIn(from, to - 1) }, check = check)
genericBinarySearch(fromIndex, toIndex, invalid = { from, to, low, high -> max2(low, high).coerceIn(from, to - 1) }, check = check)

0 comments on commit 43b37d6

Please sign in to comment.