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

Overview of F# tooling in Visual Studio


F# has been supported in Visual Studio since Visual Studio 2010, and in Visual Studio 2015 the support has improved with better syntax colorizations than Visual Studio 2010, not just IDE. This F# IDE support is officially called Visual F#.

This tooling is available as open source from Microsoft and it is available to be downloaded from GitHub at https://github.com/Microsoft/visualfsharp/.

And the F# compiler itself is open source and it is also available from GitHub (including the design proposal discussions) at https://github.com/fsharp.

The tooling is under the governance of Microsoft, but it is welcoming community contributions as it is available on GitHub. All of the community participations of tooling, compilers and the language specifications are under the governance of the F# Software Foundation (FSSF).

We can also support FSSF directly. For more information about FSSF, please visit http://fsharp.org/.

Note

The F# community projects are also managed by FSSF, and it is welcoming contributions as well. FSSF is an independent entity and it is not tied to Microsoft.

Visual F# in Visual Studio 2015 has mainly the following capabilities:

  • Project template support, including the NuGet package addition and references to other projects in the same solution file. The other projects can be VB or C# projects, not just F#.

  • AssemblyInfo support in a separate file. This feature has been available since Visual Studio 2015. Previously it was only available in C# and VB projects.

  • The F# compiler, FSC, is used to compile F# into .NET executable and libraries in the form of DLL.

  • Integration of the F# compiler, MSBuild infrastructure, and also Intellisense.

  • F# libraries, a foundation to the functional programming constructs of F# and F# unique concurrency features such as asynchronous workflow and MailboxProcessor. It also contains many useful functions to further interoperate with C#/VB, including interoperating with .NET delegates.

  • Interactive support for the F# interactive (FSI) prompt in the IDE.

For more information about F# tooling, this is the official MSDN Library link: 

https://msdn.microsoft.com/visualfsharpdocs/conceptual/visual-fsharp

It is recommended to always consult the Visual F# documentation on GitHub first, then combine it with the online MSDN Library section of F#.

Microsoft has planned to rebuild all of the online MSDN Library to use the GitHub participation model, so developers can submit a pull request to modify or to amend any documentation page. Visual F# is now being reworked on as well, but there are some pages still left behind inside Visual F# GitHub repo.

To always check the latest development of F# documentation on GitHub, visit https://github.com/Microsoft/visualfsharpdocs.

Interactive support for F# interactive

F# interactive is a tool to interpret your F# code and run it immediately. It will also process and show the results, the types, and the syntax errors. The code can be run in the interactive prompt or taken from the source code and then run into F# interactive.

Note

The concept of having interpreted code, executing it, and seeing the results is called REPL. REPL is abbreviated from Read-Eval-Print-Loop, and it was first available as the system's command-line prompt. There is nothing new about this REPL concept, as other programming languages such as Python already have had REPL before F#.

Compared to C# and VB, F# interactive is the first to have interactive REPL support since Visual Studio 2010. Unfortunately, there is no Visual Studio's Intellisense support for F# interactive yet.

There are two ways to use F# interactive:

  • In Visual Studio IDE

  • In Command Prompt

The most common usage of F# interactive is within Visual Studio IDE.

We have to set up the F# Interactive window to be displayed in order to use F# interactive.

These are the steps to display the F# Interactive window:

  1. Open the View menu from the Visual Studio main menu.

  2. Choose Other Windows.. and then choose F# Interactive.

A window that hosts the F# Interactive within Visual Studio will appear and it will be ready to interpret our F# code:

As a starter, type #help followed by ;; to mark as closing statements to be evaluated. We now see some further options:

F# interactive can be used to not only interpret and run F# code but also as a way to see immediate results of a calculation.

Type 5 * 25;; and press Enter.

We now see the result of that calculation:

We can also execute codes in the Visual Studio editor when we are opening F# source code file.

For example, create a new project using the F# tutorial project template:

You may find that your display of Visual Studio is different from the previous screenshot. Actually, the aforementioned display depends on what Visual Studio edition we have. For Visual Studio Enterprise, more templates are available for us to use, such as the Modeling Projects to create UML.

For the purpose of F#, the project templates of the F# projects are the same for the Community Edition and above.

After creating the project, an F# project contains Tutorial.fsx.

Before we use F# interactive, we should turn on the option for displaying line numbers. It is also recommended to have this option always turned on, as it will provide easier navigation to the code we write:

  1. Go to the Tools menu and choose Options. It is available in F# options in the Options dialog:

  2. Now double-click Tutorial.fsx, and highlight lines 44 to 61:

  3. Then press Alt + Enter. F# interprets the code. We can see the result of the interpretation in F# Interactive:

We have tried F# interactive from within the Visual Studio IDE. Let's use F# interactive from Command Prompt.

Note

We can also use Ctrl + Alt + F to activate or open F# Interactive.

To use F# interactive from Command Prompt, we call the executable FSI directly from Command Prompt.

The best way to run FSI is from Visual Studio's developer Command Prompt. This developer Command Prompt is available under the Visual Studio 2015 folder on the start menu of the Windows desktop menu bar.

Select it, and now we have the Developer Command Prompt for VS2015:

Type FSI and press Enter.

We can try to write some code to evaluate, such as:

let anynumber = 5 * 25;; 

Press Enter. The immediate result will be displayed:

To quit the FSI, type #quit;; and press Enter.

Using F# interactive from Command Prompt is faster but it is also not quite so user-friendly because we cannot evaluate multiple lines of code easily. It is easier to evaluate this in Visual Studio IDE.

Note

For simplicity and ease of use, the rest of this book will always use FSI within the Visual Studio IDE.

For more information about F# FSI, consult the FSI reference from the MSDN Library at  https://msdn.microsoft.com/visualfsharpdocs/conceptual/fsharp-interactive-%5bfsi.exe%5d-reference.

FSI is also configurable. We can configure FSI further by leveraging the FSI class library in the Microsoft.FSharp.Compiler.Interactive namespace. More information on this library is also available at the F# FSI URL mentioned previously.