Mastering High Performance with Kotlin
eBook - ePub

Mastering High Performance with Kotlin

Overcome performance difficulties in Kotlin with a range of exciting techniques and solutions

Igor Kucherenko

Share book
  1. 316 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering High Performance with Kotlin

Overcome performance difficulties in Kotlin with a range of exciting techniques and solutions

Igor Kucherenko

Book details
Book preview
Table of contents
Citations

About This Book

Find out how to write Kotlin code without overhead and how to use different profiling tools and bytecode viewer to inspect expressions of Kotlin language.

Key Features

  • Apply modern Kotlin features to speed up processing and implement highly efficient and reliable codes.
  • Learn memory optimization, concurrency, multi-threading, scaling, and caching techniques to achieve high performance.
  • Learn how to prevent unnecessary overhead and use profiling tools to detect performance issues.

Book Description

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

What you will learn

  • Understand the importance of high performance
  • Learn performance metrics
  • Learn popular design patterns currently being used in Kotlin
  • Understand how to apply modern Kotlin features to data processing
  • Learn how to use profling tools
  • Discover how to read bytecode
  • Learn to perform memory optimizations
  • Uncover approaches to the multithreading environment

Who this book is for

This book is for Kotlin developers who would like to build reliable and high-performance applications. Prior Kotlin programming knowledge is assumed.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mastering High Performance with Kotlin an online PDF/ePUB?
Yes, you can access Mastering High Performance with Kotlin by Igor Kucherenko in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Java. We have over one million books available in our catalogue for you to explore.

Information

Year
2018
ISBN
9781788998352
Edition
1

Best Practices

This chapter offers a collection of best practices that we've discussed in this book. This collection is a set of rules or templates that you should follow in order to be able to write simple and reliable code without performance overhead.
We will cover the following topics in this chapter:
  • The disposable pattern
  • Immutability
  • The String pool
  • Functional programming
  • Collections
  • Properties
  • Delegation
  • Ranges
  • Concurrency and parallelism

The disposable pattern

Use the disposable pattern along with the try-finally construction to avoid resource leaks.
The disposable pattern is a pattern for resource management. This pattern assumes that an object represents a resource and that the resource can be released by invoking a method such as close or dispose.
The following example demonstrates how to use them:
fun readFirstLine() : String? {
......
var bufferedReader: BufferedReader? = null
return try {
......
bufferedReader = BufferedReader(inputStreamReader)
bufferedReader.readLine()
} catch (e: Exception) {
null
} finally {
......
bufferedReader?.close()
}
}
The Kotlin standard library already has extension functions that use this approach under the hood:
fun readFirstLine(): String? = File("input.txt")
.inputStream()
.bufferedReader()
.use { it.readLine() }
You should also remember the dispose method when you work with the RxJava library:
fun main(vars: Array<String>) {
var memoryLeak: NoMemoryLeak? = NoMemoryLeak()
memoryLeak?.start()
memoryLeak?.disposable?.dispose()
memoryLeak = NoMemoryLeak()
memoryLeak.start()
Thread.currentThread().join()
}

class NoMemoryLeak {

init {
objectNumber ++
}

private val currentObjectNumber = objectNumber

var disposable: Disposable? = null

fun start() {
disposable = Observable.interval(1, TimeUnit.SECONDS)
.subscribe { println(currentObjectNumber) }
}

companion object {
@JvmField
var objectNumber = 0
}
}

Immutability

If the state of an object can't be changed after it is initialized, then we consider this object immutable. Immutability allows you to write simple and reliable code. If the state of an object can't be changed, we can safely use it as a key for a map function, as in the following example:
fun main(vars: Array<String>) {
data class ImmutableKey(val name: String? = null)
val map = HashMap<ImmutableKey, Int>()
map[ImmutableKey("someName")] = 2
print(map[Immutabl...

Table of contents