Inlining Functions with Lambdas
Lambdas are elegant, and it’s convenient to pass functions to functions, but there’s a catch—performance. Kotlin provides the inline keyword to eliminate the call overhead in order to improve performance, to provide non-local control flow such as a return from within forEach(), and to pass reified type parameters as we saw in Reified Type Parameters.
Before we delve into ways to improve performance of functions that use lambdas, let’s set some context. Every higher-order function we write doesn’t need the solutions we’ll see in this section. A good amount of code we write will enjoy a reasonable performance and need nothing special. But in some situations—such as when a higher-order function contains a loop ...
Get Programming Kotlin now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.