Me and another dude (<ArcheCraft>) are revamping a...
# language-evolution
e
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)
Copy code
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 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
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:
Copy code
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:
Copy code
val a = Vec3()
val b = a.mapCoordinates { (it + 1) / 2 }
But of course this is not a general solution
e
Also, where can I read more about the ability of the jdk to SIMDify specific patterns?
🍀 1
j
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
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
K 8
e
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
There are various strategies to be researched.
e
May I be an useful use case/beta tester for that?
e
We are still a long way from having anything working there.
e
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
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.