Vsevolod Profile picture
Feb 6 10 tweets 3 min read Read on X
Nice post from the series.

While it's totally okay to [micro-]optimize specific functions for specific targets (e.g. Dalvik), the standard library implementation is "slow" not because it's not optimized, but, surprisingly, because it is!
When designing API for millions of developers, the first thing in mind should be being concise and idiomatic: developers adopt practices and code from the standard library, so eventually, "idiomatic" Kotlin becomes widespread. And idiomatic code is easier to optimize
The original implementation tries to outsmart reality:

It has a branch for the very uncommon case in an attempt to be defensive and save some work. Which is then checked for later again, effectively being a dead code that slows down reasoning and execution
We are past this, it's 2015 already: !
It also tries to outsmart the compiler: "hey, I don't want an iterator of the string, thus I'll check each index individually to have charAt() called!" which, as we can see from the article, does not work well
And finally, it's not idiomatic. What we have is "if the string is not empty or for each index char at this index is blank" when what we *really* want is "every symbol of the string is blank".

Let's fix that and see how it goes?
'fun CharSequence.isBlank() = all { it.isWhitespace() }'. To no surprise, this version is as fast as the @romainguy one! Image
@romainguy After our change, the code is how we intended to write it. Predictable and straightforward code is where the compiler shines -- no allocations, no need for escape analysis, no excessive branches. A few bytecode-level transformations and here we are
@romainguy What can we learn from this?

It's worth caring about the semantics, the intent of the code, its readability and conceptual clarity.

Don't try to outsmart the compiler unless you know precisely what you are doing. At the end of the day, the street always wins :)
@romainguy Obligatory performance curve from @shipilev.

We came into "Debting on performance" in this one a few years ago, and now we pay the interest.
Don't do that, strive for beauty peak and the rest will follow Image

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Vsevolod

Vsevolod Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(