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

Compartir libro
  1. 316 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

Mastering High Performance with Kotlin

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

Igor Kucherenko

Detalles del libro
Vista previa del libro
Índice
Citas

Información del libro

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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Mastering High Performance with Kotlin un PDF/ePUB en línea?
Sí, puedes acceder a Mastering High Performance with Kotlin de Igor Kucherenko en formato PDF o ePUB, así como a otros libros populares de Computer Science y Programming in Java. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2018
ISBN
9781788998352
Edición
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...

Índice