38,39 €
Build powerful and fast applications with F#
This book is for F# developers who want to build high-performance applications. Knowledge of functional programming would be helpful.
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.
This easy-to-follow guide is full of hands-on examples of real-world multithreading tasks. Each topic is explained and placed in context, and for the more inquisitive, there are also more in-depth details of the concepts used.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 424
Veröffentlichungsjahr: 2017
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2017
Production reference: 1130117
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78646-807-9
www.packtpub.com
Author
Eriawan Kusumawardhono
Copy Editor
Safis Editing
Reviewer
Arthur Pham
Project Coordinator
Ulhas Kambli
Commissioning Editor
Kunal Parikh
Proofreader
Safis Editing
Acquisition Editor
Sonali Vernekar
Indexer
Tejal Daruwale Soni
Content Development Editor
Nikhil Borkar
Graphics
Abhinash Sahu
Technical Editor
Hussain Kanchwala
Production Coordinator
Shraddha Falebhai
Eriawan Kusumawardhono is a veteran senior software development engineer (SDE) who has software development experience of more than 13 years, with 11 years of having developed in .NET, since the introduction of Visual Studio 2003. On usual working day he works at Allegro Development, a software development company, and in his spare time, he does speaking and training for software development and programming language communities in Indonesia. He is also a member of F# Foundation speaker program. He enjoys being a full-time technical polyglot software developer, mingling both OOP and functional programming at the same time.
He currently holds a Microsoft MVP award in Visual Studio development platform competency, from 2011 to 2017, for more than 5 consecutive years.
He lives in Jakarta with his wife, enjoying reading and cooking heterogeneous exotic food in Indonesia.
Many thanks to Microsoft, especially Microsoft Research, and Don Syme for developing F#, a functional programming that meets OOP nicely and truly pragmatic! I would like to give special thanks to Packt for giving me the chance to write this wonderful book and Nikhil for his continuous support and always giving me constructive feedback. I also would like to give a big huge thanks for my lovely wife, Dini Kusumawardhono, for always giving me unconditional love and support during the writing of this book and also her continuous understanding every day. I would like to dedicate this book to software development communities in Indonesia, and to all of the F# developer communities around the world. F# rocks!
Arthur Pham has been working for Thomson Reuters as a Lead Quantitative Engineer since 2006.
He has spent many years designing and implementing derivatives pricing models and still loves learning new programming languages, such as F#, C++, Python, Flex/ActionScript, C#, Ruby, and JavaScript.
He currently lives in New York, NY, USA and can be contacted on Twitter at @arthurpham.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Thank you for purchasing this Packt book. We take our commitment to improving our content and products to meet your needs seriously—that's why your feedback is so valuable. Whatever your feelings about your purchase, please consider leaving a review on this book's Amazon page. Not only will this help us, more importantly it will also help others in the community to make an informed decision about the resources that they invest in to learn. You can also review for us on a regular basis by joining our reviewers' club. If you're interested in joining, or would like to learn more about the benefits we offer, please contact us: [email protected].
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#.
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.
This book is for F# developers who want to build high-performance applications. Knowledge of functional programming would be helpful.
We need to ensure that we have met all of the software and hardware requirements and that we have their correct versions as well.
The following are the requirement specifications for an F# environment:
The following are the Visual Studio 2015 Update 3 hardware requirements:
For more information, please check the requirement specification on MSDN:
https://www.visualstudio.com/en-us/downloads/visual-studio-2015-system-requirements-vs.aspx
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.
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.
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.
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:
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
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!
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 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.
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.
F# has common bottlenecks although they might be subtle as well.
In order to be able to quickly understand the bottleneck factors in F#, we will categorize the shared general bottlenecks of .NET as managed bottlenecks (also in C#/VB), and F#-only bottlenecks (this includes when using F# with other languages).
The following are managed .NET bottlenecks (from obvious to less obvious):
Side effects mean all of the elements outside the formal computation (it is often called the outside world) that we interact with, and this includes the changing global state. The outside world can be all of the things that we cannot fully determine as the end result. Examples of the outside world include:
DateTime.Now will always return different results and this is as expected because the result must change every time it is called or instantiated. It is not free of side effects, but it is still expected to always return a different result.
Side effects are not just for functional programming developers, as many of us are now becoming quite aware. There are no absolute side effect-free computations because we should learn and be able to correctly handle them. For example, even printing a screen to a console is also a side effect because it involves I/O, and it changes the state of the outside world.
The following are F#'s unique bottlenecks:
Many of us, when dealing with concurrent problems, sometimes try to use a hammer for every nail. There is no silver bullet for all of the problems of implementing concurrency.
It is also recommended to understand concurrency, as concurrency is now becoming more relevant because of the many core models in the releases of modern microprocessors (or simply processors) in the last 7 years. This fact is also becoming a trend as the clock speed of the latest processors has been usually limited to 3.2 GHz for the last 3 years.
Microsoft's Visual C++ architect, Herb Sutter, has written a very thorough article in the form of a whitepaper famously known as The Free Lunch Is Over:
http://www.gotw.ca/publications/concurrency-ddj.htm
Let's understand first what concurrency is and the F# supports.
Before we dive deeper into concurrency in F#, we should understand the definition of concurrency.
Concurrency is one of the main disciplines of computer science and it is still one of the main problems of computations.
Simply defined, concurrency is the composition of the order of independent process units or partially-ordered process units that can be executed in parallel or not in parallel, but not in sequential order. The term order in this context means ordered as sequentially.
The following diagram illustrates the concept of sequential (not concurrent) in action:
Process 1 to Process 4 as shown in the preceding diagram is executed sequentially step by step. Process 2 must wait for Process 1 to be completed first, as do Process 3 and Process 4.
This sequence is also called a synchronous process or is simply referred to as being synchronous.
The following figure is a sample illustration of a parallel concurrency combination of parallel and synchronous processes:
Processes 1A, 2A, and 3A run in parallel, although each parallel lane has its own sequence of processes that are run sequentially.
The term parallel means that it is not just executing simultaneously in parallel, but parallel also means that it may run on many processors or on many cores, as is common in modern processors that have multiple cores.
A simple definition of asynchronous means not synchronous. This means that if we have an asynchronous flow, the process is not run synchronously.
These are the implications of an asynchronous flow:
This is a sample case of asynchronous: a customer is going to have dinner in a restaurant. The flows are:
The asynchronous model that uses a notification to inform that a process is completed is called asynchronous callback.
The result returned at the end of the execution later (or in the future) is called a Future. It is also the future, in a sense, when many processes are executed in parallel, having results later.
This is the official documentation of Future in MSDN Library:
https://msdn.microsoft.com/en-us/library/ff963556.aspx
For I/O operations, such as printing a document, we cannot determine whether the printing is successful or not, so the notification of the end process is not available. We can implement an asynchronous operation on I/O, and the fact that there is no observable notification of this is why this asynchronous model is called the asynchronous fire and forget model.
Many developers, even seasoned or experienced developers, still think that concurrency and parallel programming are different. Actually, parallel programming is just one member within the concurrency discipline, together with the differentiation of asynchronous and synchronous processing models.
This is also one of the most misunderstood concurrency concepts or problems, and there are many more regarding how we approach concurrency.
These are some common organized sample cases of misunderstood concurrency problems:
Fact: Not all concurrent problems are easily solved with parallelism.
Assuming all implementation of asynchronous is asynchronous.Fact: This depends on how we implement async; sometimes the execution of an async construct is executed synchronously.
Ignoring blocking threads such as I/O.Fact: Blocking I/O threads should be handled asynchronously; otherwise, the current thread is always waiting indefinitely until the I/O thread is finished.
The synchronized lock is blocking.Fact: The lock is not a blocking thread.
Relying on the CPU speed.Fact: The CPU speed increase is becoming less of an issue. The research and development of modern CPUs is focusing on multiple core CPUs.
A few sample cases of concurrent problems are mentioned as follows:
The case samples of the first case are:
Sample cases of the second case are listed as follows:
RAID array in the storage I/O is one of the best samples of parallelism in I/O. It stores data in parallel across multiple disks. It is faster than common I/O because data is stored in parts (not whole data to a disk) to several disks in parallel.
The third case is related to all of the I/O operations including sending data to a printer and saving large data into a disk. These operations are always blocking threads.
For the case of lock, Microsoft has issued official statements that lock in .NET used by C# and VB is executed without any interruption, and it only locks an object until it has finished executing the block in the synchronized lock. It's still allowing other threads to run without waiting for the thread that has the lock to finish.
This is the official thread synchronization of C# and VB in MSDN:
https://msdn.microsoft.com/en-us/library/ms173179.aspx
It is recommended to always check online the MSDN Library of the .NET class library, as this is always updated.
Concurrency support in F# is based on the existing work of concurrency support features in .NET BCL (the Base Class Library). It's also by design, since F# runs on top of .NET CLR and can use .NET BCL. F# also has its unique ways that bring more features other than just language features (for example, asynchronous computations).
The .NET BCL part of concurrency has basic support for the following:
Beginning with .NET 4.0, we have the Task Parallel Library (TPL). This library makes concurrent support easier. TPL consists of the following:
For a more complete reference of concurrency support in .NET, please visit https://msdn.microsoft.com/en-us/library/hh156548(v=vs.110).aspx.
.NET has no support yet for fiber API in Win32 API. Microsoft currently has no definite plan for fiber support.
F# has its own unique features of concurrency supports. They are:
More on concurrency support in F# is available in Chapter 4, Introduction to Concurrency in F# and Chapter 5, Advanced Concurrency Support in F#.
Now it's time to dive more into some codes. To start writing F# code, we can use F# and Visual Studio combined. This includes IDE supports for F#.
There is one aspect of understanding running F# code that is crucial: debugging F# code. We have to be able to debug F# code, especially when we have very large projects that have hundreds of F# code files, not to mention when each of the code files may have too many lines of code. For example, having to check a running F# code that has more than 2,000 lines.
The following are the advantages of the debug features:
Visual F# in Visual Studio 2015 also has debugging capabilities. It was not as powerful when it was introduced in Visual Studio 2008 as additional add-on, but now the debugging experience is much better. It has been integrated with the Visual Studio extensibility model nicely, providing, for example, faster execution while running in the debug mode and having conditional breakpoints.
It is different from the C#/VB debugger because F#, although being a strongly and strictly typed language, currently has no support for evaluating expressions in the debugger's immediate windows in Visual Studio 2015.
