Book Image

Mastering High Performance with Kotlin

Book Image

Mastering High Performance with Kotlin

Overview of this book

The ease with which we write applications has been increasing, but with it comes the need to address their performance. A balancing act between easily implementing complex applications and keeping their performance optimal is a present-day requirement In this book, we explore how to achieve this crucial balance, while developing and deploying applications with Kotlin. The book starts by analyzing various Kotlin specifcations to identify those that have a potentially adverse effect on performance. Then, we move on to monitor techniques that enable us to identify performance bottlenecks and optimize performance metrics. Next, we look at techniques that help to us achieve high performance: memory optimization, concurrency, multi threading, scaling, and caching. We also look at fault tolerance solutions and the importance of logging. We'll also cover best practices of Kotlin programming that will help you to improve the quality of your code base. By the end of the book, you will have gained some insight into various techniques and solutions that will help to create high-performance applications in the Kotlin environment
Table of Contents (12 chapters)

Concurrency and parallelism

Multithreading is based on the concepts of concurrency and parallelism. Concurrency refers to the ability of a task to be split into independent subtasks that can be executed out of order without affecting the final result:

Let's look at the following example:

class Baker {
fun bake(): Cake {
for (i in 0..1_000_000_000) {
BigInteger.ONE.modPow(BigInteger.TEN, BigInteger.TEN)
}
return Cake()
}
}

The Baker class contains the bake() method, which invokes the modPow function that take a significant amount of time to imitate the process of baking and returns an instance of the Cake class:

class Cake

The Bakery class contains the order method, which takes the numberOfCakes argument and returns an instance of the List<Cake> type:

class Bakery {
fun order(numberOfCakes: Int): List<Cake> {
val...