https://kotlinlang.org logo
Title
e

elect

12/01/2021, 2:06 PM
Me and another dude (ArcheCraft) are revamping a kotlin glm port (multiplatform plus code generation). While we are at it, there is always this problem about allocations which bothers me (at least till we get valhalla)
val a = Vec3()
val b = (a + 1) / 2
a + 1
will generate another
Vec3
and so will
/ 2
. This normally isn't the end of the world, but in specific situations, such as real time 3d graphics, embedded devices, hotspots, etc etc this may be an issue. Googling around for ideas, I saw that also @Aleksei Dievskii in its viktor had this burden to deal with. So I was wondering if there might be some solution involving some new language paradigms or features..
a

Aleksei Dievskii

12/01/2021, 2:14 PM
@Aleksei Dievskii has subscribed to this thread. :))
we circumvent this problem by providing in-place variants of all the methods and encouraging the user to employ them. however, distinguishing intermediate copies that can be modified from inputs that should not becomes user's responsibility, which is not optimal.
👍 1
j

Joffrey

12/01/2021, 4:06 PM
The operations shown here as examples seem to be scalar modifications of all the vector's components. For this use case, an alternative could be to provide an extension such as:
inline fun Vec3.mapCoordinates(transform: (Int) -> Int): Vec3 = Vec3(
    x = transform(x),
    y = transform(y),
    z = transform(z),
)
This way users can perform this operation a bit more efficiently by creating a single output object:
val a = Vec3()
val b = a.mapCoordinates { (it + 1) / 2 }
But of course this is not a general solution
e

elect

12/02/2021, 6:39 AM
Also, where can I read more about the ability of the jdk to SIMDify specific patterns?
🍀 1
j

jimn

12/02/2021, 7:14 PM
concurnas is jvm specific, not unlike kotlin, with a gpu kernel feature as part of the core language. jvm has a new SIMD jep, the default situation right now with the hotspot/c2 and so on is the compiler source and the profiler lumped together with the
@intrinsic
features.
e

elizarov

12/03/2021, 12:46 PM
In tight loops that you know to be performance-sensitive your best option now is to provide in-place mutable version. E.g., you can take a look at a simple set of utilities I'm using here https://github.com/elizarov/PolyhedraExplorer/blob/main/src/commonMain/kotlin/util/Vec3.kt
Anyway, we are also tired of waiting for Valhalla, so we've just recently started a student research project to see if we can implement multi-field value classes in our Kotlin/JVM BE ourselves by "flattening" their representation into fields, locals, and parameters.
😍 4
:kotlin-intensifies: 8
e

elect

12/05/2021, 8:35 AM
that's super exciting Roman, may I ask who is he? That's may work nicely when everything is declared and consumed in the same scope. The moment you need to pass that around, you need to resort to classes/arrays.. or?
@elizarov
e

elizarov

12/07/2021, 4:35 PM
There are various strategies to be researched.
e

elect

12/07/2021, 4:37 PM
May I be an useful use case/beta tester for that?
e

elizarov

12/07/2021, 4:42 PM
We are still a long way from having anything working there.
e

elect

12/08/2021, 3:11 PM
I recently discovered this gem, which is based on this I wonder if a kotlin counterpart would be feasible (with a compiler plugin maybe?)
:mind-blown: 1
e

elizarov

12/08/2021, 3:14 PM
JVM will soon have stable vector APIs (see https://openjdk.java.net/jeps/414) that gives portable access to SIMD intrinsics to any JVM code. It will make super-easy and fast to work on vectors from Kotlin, too. It has nothing to do with our work on value classes, though.