I have honestly no idea where to post this. It's n...
# getting-started
l
I have honestly no idea where to post this. It's not really a getting-started question, but it's very generic, applying to all backends. I have some code that looks like this:
Copy code
private fun String.isAstralPlane(): Boolean {
            return this.any(Char::isHighSurrogate)
        }
Now, because
any
is implemented as
CharSequence.any
, the emitted code will call
invokevirtual
on the
CharSequence.length()
function for each iteration, instead of getting the string length once and reusing it, which would be possible if there was an overload for
String
. One could also make the argument that the compiler should see that the type of
this
in the inlined code is a
String
and not a
CharSequence
, and thus generate more efficient code. Am I overreacting, or should this be fixed?
y
Benchmark, benchmark, benchmark! The JVM is pretty smart, so are most JS engines. LLVM is incredible at optimizations, too. Loop hoisting is such a simple optimization, so I'm fairly confident that all these backends won't mind it at all
Now, if you make a micro benchmark (using kotlinx-benchmark) that shows a significant slowdown here, then it's likely worth reporting and asking for a
String.any
overload or similar solution