https://kotlinlang.org logo
#getting-started
Title
# getting-started
k

Koneko Toujou

02/06/2023, 3:26 PM
is kotlin/native as stable as kotlin/jvm ?
👀 1
v

Vampire

02/06/2023, 3:29 PM
https://kotlinlang.org/docs/components-stability.html#current-stability-of-kotlin-components
Kotlin/JVM
Stable
1.0
Kotlin/Native Runtime
Beta
1.3
Kotlin/Native new memory manager
Beta
1.7.20
Kotlin/Native interop with C and Objective C
Beta
1.3
k

Koneko Toujou

02/06/2023, 3:31 PM
alright 🙂
as im wondering if i should use kotlin native or GRAALVM for LIBGDX
🤷 1
like, does kotlin native ONLY work with pure kotlin code or does it also allow java libs and java code?
h

hfhbd

02/06/2023, 3:37 PM
By default, Kotlin native only supports Kotlin and with cinterop C code. Sure you can use some compilers to convert java/jvm bytecode to native code or use JNI.
c

CLOVIS

02/06/2023, 3:38 PM
You mentioned LibGDX so I'm assuming you're doing game development. Kotlin/JVM will be much faster than Kotlin/Native for that. GraalVM (to my knowledge) isn't a good fit for games.
k

Koneko Toujou

02/06/2023, 3:41 PM
alright
r

Rob Elliot

02/06/2023, 4:39 PM
Kotlin/JVM will be much faster than Kotlin/Native [for game development]
That surprises me - I thought the JVM was a fairly major drawback to game level speeds?
c

CLOVIS

02/06/2023, 4:41 PM
Kotlin/Native is relatively new technology, the JVM has 30+ years of optimizations behind it. Kotlin still has GC etc in all platforms
Kotlin/Native is expected (but I haven't tested) to use a lot less RAM though.
r

Rob Elliot

02/06/2023, 4:43 PM
OK, I'd imagined that if you were using it for glue code and a C / C++ framework of some kind underneath via a facade it would be a reasonable gaming solution, interesting to hear not.
c

CLOVIS

02/06/2023, 4:43 PM
Well of course if all you do in Kotlin is a scripting façade, that's a different story
My point is, any non-trivial algorithm is most likely faster in Kotlin/JVM than in Kotlin/Native, now if what you do is just a small script and the real work appears somewhere else, it's irrelevant to this discussion
the JVM has a reputation of being really slow but it's quite far from the truth. Of course though, everything depends on your use case.
a

Arjan van Wieringen

02/06/2023, 5:14 PM
The JVM reputation of being really slow is extremely outdated
r

Rob Elliot

02/06/2023, 5:16 PM
I know, there's no VM that has ever had the engineering put into it that the JVM has. My understanding is that it excels at use cases where it can take its multiple runtime cycles through some code to decide how best to optimise it, and the odd 100ms garbage collection isn't a big issue, like HTTP servers; and is not so good for use cases that want fast startup or cannot cope with occasional brief pauses, like games and command line interfaces.
c

CLOVIS

02/06/2023, 5:18 PM
Except the renderer, games are not real-time, and a 100ms garbage collection pause is completely fine.
r

Rob Elliot

02/06/2023, 5:19 PM
For instance a year ago for fun I tried "Hello world" in multiple formats; a rust compiled version took 3.588 ms to run, a kotlin/native version 6.868 ms, and a JVM one 74.629 ms.
v

Vampire

02/06/2023, 5:19 PM
There are not really pauses nowadays. There are quite some games written in Java and also command line tools. There are even use-cases where a Java program runs faster than an according native program. You can in each language write bad and slow code or good and fast code. But yeah, if you need every nanosecond and every bit of RAM, you probably have to use Assembler anyway. 😄
c

CLOVIS

02/06/2023, 5:20 PM
100ms is also a very extremely worst case. A game like Minecraft (which arguably is one of the most successful 3D games of all time, and is also known for not being well optimized) runs pretty well.
There are also multiple different GCs that have various performance requirements. Some of them are not even STW
You should also take into account the cost of parallelism. Coroutines are not a thing in C/C++, and you get a lot from having stuff like AI algorithms be heavily asynchronous which is hell to do in C++
r

Rob Elliot

02/06/2023, 5:34 PM
I'm well aware of Minecraft, I've helped my son write extensions to it in Kotlin. But Minecraft's surely a paradigmatic example of how gameplay is more important than graphics in the success of a game, precisely because the graphics are objectively dramatically worse[1] than typical modern 3D games? Has anyone got an example of a top flight graphics & FPS dependent game that is deployed as JVM bytecode running on a JVM? I remember Il-2 Sturmovik used the JVM for the interface, but dropped down to C++ for the actual flying. I'd have thought if it were viable it would be an excellent idea, given Kotlin is streets ahead of C++ in terms of how much safety it gives you and how bug ridden games can be. [1] where worse means lower detailed, less lifelike - fine for its purposes, obv.
k

Klitos Kyriacou

02/06/2023, 5:42 PM
a

Arjan van Wieringen

02/06/2023, 5:47 PM
@Rob Elliot The JVM isn't the target for top game engines. Neither is Rust (yet) or Go, or Swift, or Dart, or a host of other languages. And most of them never will be. There is more than games in the world.
Everything has their place. But the JVM-is-so-slow shtick is getting old, and it was already getting old 15 years ago.
r

Rob Elliot

02/06/2023, 5:51 PM
The conversation was explicitly about game development.
c

CLOVIS

02/06/2023, 6:13 PM
@Rob Elliot that's a bit unfair, Minecraft has editable terrain which basically no other game has.
r

Rob Elliot

02/06/2023, 6:15 PM
What does that have to do with its graphics?
Minecraft's brilliant.
It's brilliant because of its game design - the complete freedom, the way that creation and playing happen via the same mechanisms, the way you can create games using it - it's ana stonishing bit of game design.
c

CLOVIS

02/06/2023, 6:16 PM
But yeah the main issue against using the JVM for 3d games now is that JNI has quite a high cost, so you're better off implementing the renderer in C/C++ and doing only high-level game logic on the JVM. Maybe Panama will help with that, maybe not. Anyway my point was between Kotlin/JVM and Kotlin/Native, not between Kotlin/JVM and C++.
@Rob Elliot you can't have highly detailed graphics and editable terrain. No matter the technology, it's just not a thing anyone managed to do
Also, not every game as AAA graphics
IMO the missing piece for the JVM for gaming is a first-party Vulkan (or similar) integration at the JVM-level, that doesn't have the downsides of JNI
It's definitely not a priority for Oracle though
u

ursus

02/07/2023, 12:56 AM
I thought system's people tend to not like java, is not because it is slow, because it is not, but because of the performance unpredictability (gc)
v

Vampire

02/07/2023, 12:58 AM
The performance is not unpredictable. It is even extremely configurable
u

ursus

02/07/2023, 12:59 AM
well, is it? you dont know when memory is going to be freed
atleast, that's what I heard from systems people about liking rust, that they need the predictability of c++ but in a nicer safer language
v

Vampire

02/07/2023, 1:09 AM
You might not know when, but you shouldn't care. I'm not familiar with all Garbage Collectors and choices maybe there is also some "more predictable" one. But there are serveral Garbage Collectors to choose from and with several options each if you really know what you do and need tweakage.
u

ursus

02/07/2023, 1:17 AM
but this I believe is why spacex won't run java, they just cannot "risk" a gc
v

Vampire

02/07/2023, 1:18 AM
There is no risk in a proper GC o_O
u

ursus

02/07/2023, 1:20 AM
okay I don' know, it's what I've heard, first there is jitting and stuff, sure you can solve that with AOT, but gc you cannot determine when it runs, and they need to know this memory is freed when this function returns/this other thing dealocates etc
v

Vampire

02/07/2023, 1:24 AM
As I said, they shouldn't care, that's what a GC is for. If they need to care, a language with GC is maybe not the right choice. But why should they care? And jitter I don't know, but can probably be eliminated by right choice of GC and its options.
u

ursus

02/07/2023, 1:25 AM
but in system programming you do care, say self driving cars etc
v

Vampire

02/07/2023, 1:26 AM
Enumerating use-cases does not answer the question why they should care
u

ursus

02/07/2023, 1:27 AM
because high performance applications care about predictability, they need to run the same way all the time, not sometimes randomly slower because of reasons, memory usage also differing etc, all hinges on how GC runs
v

Vampire

02/07/2023, 1:28 AM
Again, just because you have a GC does not mean anything is unpredictable or randomly slower
It's all a question of right choice. If you of course use the serial or default parallel GC, application will freeze during collection. But if you e. g. choose the Z GC, your application does not stop for more than 10 ms, which should be predictable enough.
u

ursus

02/07/2023, 1:34 AM
Why is android itself mostly written in c++ then?
v

Vampire

02/07/2023, 1:38 AM
Ask the ones that implemented it. Besides, what you need on Android is a JVM implementation and that is Dalvik and and JVM implementations themselves are often written in non-Java language afair. Maybe also back then there were not as many GC choices, GC evolved pretty much in the last years. Z GC e. g. is stable as of Java 15 or something like that.
k

Koneko Toujou

02/07/2023, 4:30 AM
It's mainly JNI interop which is unavailable Eg, attempting to completely rewrite skia in java with GL bindings would be a collosal undertaking and would serverly hurt any expectations of native skia vs JVM skia
Also some things MUST be done in C/C++ that are otherwise impossible or impractical to do via JVM
Sust as for example, rewriting each C++ library used one by one to mostly pure java with little JNI bindings would just be a pain
Eg, C++ templates are practically impossible to do in Java genetics due to recursive instantiation and templated function resolution
Lifetime scopes are also non existent in java
Well Phantom References and Cleaner API are probably as close as we can get?
e

ephemient

02/07/2023, 5:20 AM
try (=)
(java)/`.use {}` (kotlin) are the the lifetime scope of resources
k

Koneko Toujou

02/07/2023, 6:43 AM
Yea but more of resources that exceed try/use scopes
Where manual disposal might become complicated or unwanted
136 Views