Book Image

F# High Performance

By : Eriawan Kusumawardhono
Book Image

F# High Performance

By: Eriawan Kusumawardhono

Overview of this book

F# is a functional programming language and is used in enterprise applications that demand high performance. It has its own unique trait: it is a functional programming language and has OOP support at the same time. This book will help you make F# applications run faster with examples you can easily break down and take into your own work. You will be able to assess the performance of the program and identify bottlenecks. Beginning with a gentle overview of concurrency features in F#, you will get to know the advanced topics of concurrency optimizations in F#, such as F# message passing agent of MailboxProcessor and further interoperation with .NET TPL. Based on this knowledge, you will be able to enhance the performance optimizations when implementing and using other F# language features. The book also covers optimization techniques by using F# best practices and F# libraries. You will learn how the concepts of concurrency and parallel programming will help in improving the performance. With this, you would be able to take advantage of multi-core processors and track memory leaks, root causes, and CPU issues. Finally, you will be able to test their applications to achieve scalability.
Table of Contents (15 chapters)
F# High Performance
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Preface

It’s well-known today that F# has been quite a first class citizen, a built-in part of programming language support in Visual Studio from Visual Studio 2010. F# is a programming language that has its own unique trait: it is a functional programming language, and at the same time, it has OOP support. F# has run on .NET until now, although we can also run F# as a cross-platform language, such as on Android (using Mono).

Although F# mostly runs faster than C# or VB when doing computations, F# has its own unique performance characteristics. Some of the performance characteristics might not be so obviously identified and code implementations might have implicit bad practices and subtleties that may lead to performance bottlenecks. The bottlenecks may or may not be faster than C#/VB counterparts, although some of the bottlenecks may share the same performance characteristics, such as the use of .NET APIs.

The main goal of this book is to identify the performance problems in F#, measuring and optimizing F# code to run more efficiently, while also maintaining the functional programming style as appropriately as possible.

This book focuses on optimizing F#. A basic skill of F# knowledge (including functional programming concept and basic OOP) is a prerequisite to start understanding its performance problems and optimizing F#.

What this book covers 

Chapter 1, Performing Common Optimizations in F#, introduces F# and gives an overview of the common problems in F# code.

Chapter 2, Performance Measurement, helps you get to know what and how to measure and the tooling ecosystem to measure performance.

Chapter 3, Optimizing Data Structures, helps you decide the best and the most optimal use of F#-specific data structures and optimize them.

Chapter 4, Introduction to Concurrency in F#, provides a general introduction to the concept of concurrency and concurrency implementation support in F#.

Chapter 5, Advanced Concurrency Support in F#, provides best practices in implementing advanced concurrency support in F#, including interop with .NET TPL and avoiding performance pitfalls.

Chapter 6, Optimizing Type Provider, gives a quick walk-through of type provider implementation, while at the same time providing optimization and avoiding performance pitfalls in implementing your own custom type providers.

Chapter 7, Language Features and Constructs Optimization, explains best practices when implementing and using various F# language features and constructs without sacrificing the performance and correctness in functional programming.

Chapter 8, Optimizing Computation Expressions, explains best practices in using F# computation workflows and optimizing your own implementation of a computation workflow.

Who this book is for 

This book is for F# developers who want to build high-performance applications. Knowledge of functional programming would be helpful.

What you need for this book 

We need to ensure that we have met all of the software and hardware requirements and that we have their correct versions as well.

Basic requirements and setup environments

The following are the requirement specifications for an F# environment:

  • Visual Studio 2015 Update 3. The minimum edition is Community Edition because it is the free minimum version of Visual Studio, which has extensibility support and Visual F# (also F# project templates) as part of the installation. Express Edition is not recommended because it has no F# project templates built in.

  • .NET 4.6 as the minimum target runtime.

  • Windows 7 SP1 (Windows 8.1 Update 1 64-bit or Windows 10 64-bit is recommended)

The following are the Visual Studio 2015 Update 3 hardware requirements:

  • 4 GB RAM on Windows 7 SP1 or 6 GB RAM on Windows 8.1 and Windows 10.

  • At least a Core 2 Duo or Athlon processor. It is recommended to have Core i5 as it is also a recommendation for a Windows 8.1 or Windows 10 machine. Also, using Core i5 will provide easier illustrations to see multi core in action when we discuss concurrency in Chapter 4 and 5.

  • Approximately 12 GB to 15 GB of disk space with Windows 10 UWP installed (without Android development feature).

  • DirectX 9.0c capable display adapter.

For more information, please check the requirement specification on MSDN:

https://www.visualstudio.com/en-us/downloads/visual-studio-2015-system-requirements-vs.aspx

Note

We recommend having at least 8 GB of RAM. This minimal memory size is recommended in order to have an optimal experience for not just running F# code. For large F# projects, Visual Studio IDE will load type information, metadata and debug symbols in the background, and Visual Studio's memory consumption will quickly climb to more than 3 GB. This is why 64-bit Windows is recommended as it allows us to use RAM higher than 4 GB.  

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, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

[<EntryPoint>]
let main argv = 
    printfn "Hello F# world"
    0 // return an integer exit code

If there is a line (or lines) of code that needs to be highlighted, it is set as follows:

[<EntryPoint>]
let main argv =
    printfn "Hello F# world"
    0 // return an integer exit code

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: "Clicking the Next button moves you to the next screen."

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.

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/F-Sharp-High-Performance/ We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

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.