Marcin Wisniowski
11/22/2021, 2:41 PMdata class Point(val x: Int, val y: Int)
? In an API with lots of operations on such `Point`s, there is lots of Point
objects being created. Changing the API to operate on x and y parameters separately without a Point
class avoids the overhead, but makes the API less clean. Is there some language feature that can help here? I am looking for something like inline classes, but for two member properties.Joffrey
11/22/2021, 2:44 PMInt
in the low and high bits of a Long
, which doesn't require knowing the max widthconst val GRID_WIDTH = 200 // or whatever is your grid size
value class Point(private val index: Int) {
val x: Int get() = index % GRID_WIDTH
val y: Int get() = index / GRID_WIDTH
}
Marcin Wisniowski
11/22/2021, 2:46 PMInt
in my example for simplicity, my Point
actually takes Double
coordinates. So I cannot do such an operation.Double
values into a single variable somehow.Joffrey
11/22/2021, 2:48 PMAlthough I usedAnd here goes the workaround.. out the window 😄in my example for simplicity, myInt
actually takesPoint
coordinates. So I cannot do such an operation.Double
Point
objects though?Marcin Wisniowski
11/22/2021, 2:53 PMPoint
objects created every frame. (But even if there was no impact this would be interesting to solve)Joffrey
11/22/2021, 2:56 PMIt's used during animations of multiple objects, there could be hundreds ofStill doesn't mean it's a problem. It really depends on how you use these objects (what their lifecycle is). For instance, if you use them as temporary local variables, they will be allocated on the TLAB and will be very cheapobjects created every framePoint
But even if there was no impact this would be interesting to solveI think multi-property value classes will come at some point (so the Kotlin team is already working on it). This will be especially true when project Valhalla comes in. In the mean time, we're stuck with regular classes or workarounds that manage to cram everything in a single field 😄 of course you can also pass the 2 values as argument to all functions, but that doesn't solve the problem for return values
Michael Böiers
11/22/2021, 3:29 PMMarcin Wisniowski
11/22/2021, 4:39 PMPoint(x: Double, y: Double)
value class. See it at your own risk: https://pl.kotl.in/camTTpjz5Joffrey
11/22/2021, 4:55 PM%
and /
)I managed to make aNow the question is: aren't all those operations actually slower than just allocating and garbage-collectingvalue classPoint(x: Double, y: Double)
Point
instances?Michael Böiers
11/22/2021, 5:28 PMMarcin Wisniowski
11/22/2021, 5:31 PMI think multi-property value classes will come at some point@Joffrey Is there a KEEP for that?
Michael Böiers
11/22/2021, 5:32 PMJoffrey
11/22/2021, 5:35 PMephemient
11/22/2021, 10:57 PMvalue class FloatPair
generated by https://github.com/ephemient/kotlin-numeric
but unfortunately Double is 64-bits and there is no 128-bit primitive so you can't pull the same trickMarcin Wisniowski
11/23/2021, 12:32 AMephemient
11/23/2021, 12:49 AM