Is there any way to remove this string? This funct...
# announcements
p
Is there any way to remove this string? This function is not optional, I don't know why this is happening.
g
Remove which string?
p
Thread.currentThread()
l
I don’t think the
intrinsics
check is added to the final bytecode, not sure though
g
Are we talking about LDC or about intrinsics?
intrinsics that checks for non null will be in final bytecode
d
Kotlin uses it to provide debug info when checking that it's not null.
Why do you need to remove it?
I think there's a compiler argument that prevents the check being emitted.
g
This compiler argument is semi-official and not recommended to use
such code will be optimized by HotSpot
p
This function runs every 10ms
g
and?
You can remove this check by writing :
Copy code
Thread.currentThread()?.isInterrupted
p
I think it's heavy to init a string every 10ms 😄, the process of this function is very light
g
I don’t think so
p
I am working dealing with UI too
d
it doesn't init a String at all, it's stored in a constant pool.
g
I don’t believe that you get any measurable difference
p
hmm
d
It depends on the JVM.
g
this string will be cached in constant pool
p
changing the topic, why
Thread.currentThread().isInterrupted
needs to be verified?
g
And I showed you how avoid this check
p
it's not optional
g
First: there is no “optional” in Kotlin, there is nullable
Second: result of currentThread() has special “platform” type
d
Because it's a platform call, Java functions can all return null and Kotlin needs to check that it doesn't when you don't expect it to.
g
it means that it can be nulalble and non-nullable up to you, kotlin don’t know
p
I am only targeting to Android so far
d
Love how were both explaining the same stuff simultaneously, I'll get out
Yeah, i think the check not null functions have a bigger impact on android. Don't quote me on that.
g
so I would just ignore this bytecode, it will be optimized and you never see any performance difference
but if it’s really bothers you, just use
Thread.currentThread()?.isInterrupted == true
instead
p
no, I will try
g
d
Dalvik JVM does have redundant null check elimination, if that matters.
p
@gildor worked! thank you!