Book Image

Scala High Performance Programming

By : Vincent Theron, Michael Diamant
Book Image

Scala High Performance Programming

By: Vincent Theron, Michael Diamant

Overview of this book

<p>Scala is a statically and strongly typed language that blends functional and object-oriented paradigms. It has experienced growing popularity as an appealing and pragmatic choice to write production-ready software in the functional paradigm. Scala and the functional programming paradigm enable you to solve problems with less code and lower maintenance costs than the alternatives. However, these gains can come at the cost of performance if you are not careful.</p> <p>Scala High Performance Programming arms you with the knowledge you need to create performant Scala applications. Starting with the basics of understanding how to define performance, we explore Scala's language features and functional programming techniques while keeping a close eye on performance throughout all the topics.</p> <p>We introduce you as the newest software engineer at a fictitious financial trading company, named MV Trading. As you learn new techniques and approaches to reduce latency and improve throughput, you'll apply them to MV Trading’s business problems. By the end of the book, you will be well prepared to write production-ready, performant Scala software using the functional paradigm to solve real-world problems.</p>
Table of Contents (13 chapters)

Preface

Scala is an audacious programming language that blends object-oriented and functional programming concepts on the JVM. Scala has grown from relative obscurity to a top choice for developing robust and maintainable JVM applications. However, writing highly performant applications remains a challenge without a deep understanding of the language and the advanced features that it provides.

Since 2011, we have used Scala to solve complex business challenges with demanding performance requirements. In Scala High Performance Programming, we share the lessons that we have learned over the years and the techniques we apply when writing software. We explore the language and its ecosystem of tools and widely-used libraries in this book.

Our goal with this book is to help you understand the options that are made available to you by the language. We empower you to gather the information that is needed to make informed design and implementation decisions in your software systems. Rather than feed you a few Scala-fish and send you on your way, we help you learn how to fish and give you the tools to write more functional and more performant software. Along the way, we motivate technical discussions by concocting business problems that are reminiscent of real-world problems. We hope that by reading this book, you can come to appreciate the power of Scala, and find the tools to write more functional and more performant applications.

What this book covers

Chapter 1, The Road to Performance, introduces the concept of performance and the important terms for this topic.

Chapter 2, Measuring Performance on the JVM, details the tools that are available on the JVM to measure and evaluate performance, including JMH and Flight Recorder.

Chapter 3, Unleashing Scala Performance, provides a guided tour of various techniques and patterns to take advantage of the language features and improve program performance.

Chapter 4, Exploring the Collection API, discusses various collection abstractions that are provided by the standard Scala library. We focus on eagerly evaluated collections in this chapter.

Chapter 5, Lazy Collections and Event Sourcing, is an advanced chapter that discusses two types of lazy sequences: views and streams. We also give a brief overview of the event sourcing paradigm.

Chapter 6, Concurrency in Scala, discusses the importance of writing robust concurrent code. We dive into the Future API that is provided by the standard library, and introduce the Task abstraction from the Scalaz library.

Chapter 7Architecting for Performance, this closing chapter combines deeper knowledge on previously covered topics and explores CRDTs as building blocks for distributed systems. This chapter also explores load control policies with the free monad to build systems with bounded latency characteristics when facing high throughput.

What you need for this book

You should install Java Development Kit version 8 or higher for your operating system to work through all code examples. This book discusses the Oracle HotSpot JVM, and it demonstrates tools that are included in Oracle JDK. You should also get the latest version of sbt (version 0.13.11, at the time of writing) from http://www.scala-sbt.org/download.html.

Who this book is for

You should possess a basic knowledge of the Scala programming language, have some familiarity with elementary functional programming concepts, and have experience of writing production-level JVM software. We recommend that readers who are new to Scala and functional programming spend some time studying other resources before reading this book in order to get the best out of it. Two excellent Scala-centric resources are Programming in Scala, Artima Press and Functional Programming in Scala, Manning Publications. The former is best suited for strong object-oriented Java programmers that are looking to understand the language first and the functional programming paradigm second. The latter focuses heavily on the functional programming paradigm and less so on language-specific constructs.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, and user inputs are shown as follows: "The -XX:+FlightRecorderOptions accepts a parameter named settings, which, by default, points to $JAVA_HOME/jre/lib/jfr/default.jfc."

A block of code is set as follows:

def sum(l: List[Int]): Int = l match { 
  case Nil => 0 
  case x :: xs => x + sum(xs) 
}

Any command-line input or output is written as follows:

sbt 'project chapter2' 'set javaOptions := Seq("-Xmx1G")' 'runMain highperfscala.benchmarks.ThroughputBenchmark
src/main/resources/historical_data 250000'

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Let's start on the Overview tab of the Code tab group."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Scala-High-Performance-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/ScalaHighPerformanceProgramming_ColorImages.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.