r/java • • Jun 10 '24

[deleted by user]

[removed]

614 Upvotes

594 comments sorted by

View all comments

Show parent comment

-3

u/JDeagle5 Jun 10 '24

Null safety is provided by annotations + intellij. Kotlin null safety actually eats up a considerable portion of cpu in runtime.

6

u/vytah Jun 10 '24

That's a trade-off both ways.

Annotations are really, really annoying to use.

0

u/JDeagle5 Jun 11 '24

I wouldn't say it is more annoying than question marks.

5

u/marvk Jun 11 '24

Kotlin null safety actually eats up a considerable portion of cpu in runtime.

No. Just no.

[...] At runtime, objects of nullable types and objects of non-nullable types are treated the same: A nullable type isn't a wrapper for a non-nullable type. All checks are performed at compile time. That means there's almost no runtime overhead for working with nullable types in Kotlin.

Note: We say "almost" because, even though intrinsic checks are generated, their overhead is minimal.

- https://kotlinlang.org/docs/java-to-kotlin-nullability-guide.html#support-for-nullable-types

0

u/GeneratedUsername5 Jun 11 '24

Unfortunately yes.

Don't just belive what they say in docs, try to perftest yourself. I tried to perftest Javalin, a Kotlin-wrapper around Jetty (pure-java lib). Here you can see "almost no runtime overhead" or "minimal overhead"

https://imgur.com/zwIsWFn

Top CPU hotspot - kotlin intrinsic for checking parameters for null. It took 7(!) times longer to check for nulls, than to route a request, considering routing is already poorly optimized.

So, yes, if kotlin devs meant half-dead server with 1 RPM, then overhead is minimal. For performance goals it very much isn't.

P.S. I like how they try to convince that the checks are compiler-only and then immediately acknowledge that there are runtime checks.