O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

F# High Performance

Book Description

Build powerful and fast applications with F#

About This Book

  • Explore the advanced concurrency support in F# and .NET TPL
  • Covers major optimization techniques in F# to improve the performance of applications
  • Use Struct, Class and Record model, Interop with C# and VB without sacrificing performance.

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 Will Learn

  • Understand how the execution of functions in F# works
  • Identify common performance bottlenecks
  • Implement best practices to optimize performance
  • Use the available tooling to help measure performance
  • Combine the best practice of asynchronous and synchronous
  • Optimize further using various F# language constructs

In Detail

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.

Style and approach

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.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased 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 code file.

Table of Contents

  1. F# High Performance
    1. F# High Performance
    2. Credits
    3. About the Author
    4. About the Reviewer
    5. www.PacktPub.com
      1. Why subscribe?
    6. Customer Feedback
    7. Preface
      1. What this book covers 
      2. Who this book is for 
      3. What you need for this book 
        1. Basic requirements and setup environments
      4. Conventions 
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Performing Common Optimizations in F#
      1. Understanding the nature of F# code
        1. F# runtime characteristics
        2. Relation between F# code and its generated assembly
        3. Immutability versus mutability
      2. Overview of common bottlenecks
      3. Common samples of misunderstood concurrent problems
        1. Introduction to concurrency in F#
        2. Defining asynchronous
        3. Misunderstood concurrency problems
        4. Introduction to concurrency support in .NET and F#
      4. Overview of F# tooling in Visual Studio
        1. Interactive support for F# interactive
      5. Introduction to debugging in F#
      6. Summary
    9. 2. Performance Measurement
      1. Introduction to the nature of F# code compilation
        1. General overview of F# compiler processes and results
        2. A quick overview of IL in F#
        3. IL tooling in .NET
          1. Using ILDASM and ILASM to understand low-level IL assembly
      2. Using CLR Profiler 4.5
        1. A quick overview of CLR Profiler
        2. A quick walkthrough of CLR Profiler in action
      3. Ways to measure performance quantitatively
        1. Using .NET timers
        2. Running functions inside unit tests
          1. A quick introduction to unit tests
          2. Unit test support in Visual Studio
          3. Using FsUnit to implement a unit test in F#
      4. Summary
    10. 3. Optimizing Data Structures
      1. Overview and best practices of types in F#
        1. Static typing versus dynamic typing
      2. Quick introduction to types in F#
      3. Quick overview of generic type support in F#
        1. Overall runtime strategy of handling data types
      4. Introduction to memory storage allocation
        1. Register
        2. Stack
        3. Heap
        4. Best practices of types for storing and accessing data
      5. Best practices in using F# collections
      6. Comparing F# List to .NET List<T>
      7. Comparing F# Map to Dictionary<TKey,TValue>
      8. Choosing a value evaluation strategy
      9. Scenarios involving evaluation strategy and memory allocations
      10. Summary
    11. 4. Introduction to Concurrency in F#
      1. Introducing concurrency support in F# 4
      2. Identifying blocking threads
        1. Overview of the background technical reasons for the blocking nature of I/O
        2. Obvious trait of a blocking thread
      3. Introducing asynchronous workflow
        1. Getting to know asynchronous workflow
        2. Using asynchronous with Dispose pattern
        3. Operations in asynchronous workflow
        4. Creating child asynchronous workflow
        5. F# asynchronous workflow support for legacy .NET APM and EAP
        6. Ignoring asynchronous operation asynchronously
        7. Delaying asynchronous workflow
        8. Handling cancellation in asynchronous workflow
        9. Common conventions when implementing asynchronous operations
      4. Introduction to interop with .NET TPL
        1. A quick overview of asynchronous programming in .NET TPL
      5. Summary
    12. 5. Advanced Concurrency Support in F#
      1. Using F# MailboxProcessor
        1. Background case of having message agent
        2. Introducing fire and forget pattern
        3. Overview of a message agent
          1. Overview of serialization
        4. Introduction to F# MailboxProcessor
          1. Overview of MailboxProcesor features
          2. Further implementations of MailboxProcessor
      2. Managing side effects inside MailboxProcessor and asynchronous workflows
      3. Parallel programming with .NET TPL
        1. Overview of task-based parallelism
          1. Quick start-using the parallel debugging tool in Visual Studio
        2. Overview of data parallelism
        3. Common potential pitfalls in parallel programming
          1. Overview of handling mutable state
      4. Summary
    13. 6. Optimizing Type Provider
      1. Overview of F# type provider
        1. Comparing the type provider with other type generators
        2. Interoperability with other managed programming languages
      2. Understanding the type provider building blocks
        1. Minimum requirements of type providers
        2. Strategies of type provider implementation
          1. Choosing strategies of type provider implementation
        3. Sample usage of built-in type provider in F#
          1. Quick sample of using SqlDataConnection type provider
      3. Implementing your own type provider
        1. Building the type's building blocks
          1. Building type metadata infrastructure
            1. Implementing assembly to provide base assembly
            2. Implementing parameters for methods and constructors
            3. Implementing generated constructor
            4. Implementing generated methods
            5. Implementing generated properties
          2. Basic minimal implementation of type provider
      4. Common pitfalls in implementing type provider
      5. Summary
    14. 7. Language Features and Constructs Optimization
      1. Overview of language features and constructs optimization
      2. Optimizing common F# language constructs
        1. Best practices of interoperability of F# delegate with .NET delegate
          1. Passing a .NET delegate as an F# function
          2. Calling .NET delegate within F#
        2. Best practices in pattern matching and active pattern
          1. Comparing constant pattern matching with if constructs
          2. Best practices in using active patterns
          3. Considerations in catching exceptions in active patterns
      3. Optimizing inline functions
        1. Background overview of inline functions and related type inferences
        2. Overview of F# type inference mechanism of generic type inference
          1. Overview of automatic generalization in F#
        3. Best practices in implementing inline functions
      4. Identifying tail call in recursive constructs
        1. Overview of recursion in functional programming
        2. Tail call recursion in F# and .NET CLR
          1. Identifying tail call optimized implementation
        3. Advantages of having tail call recursive implementation
        4. Limitations of tail call recursion in F#
      5. Summary
    15. 8. Optimizing Computation Expressions
      1. Quick introduction to F# computation expression
        1. Introduction to builders of computation expression
          1. Using F# function/delegate over .NET Func
          2. Relation of F# function computation expressions with arguments restriction
        2. Walkthrough of planning and writing a computation expression
          1. Understanding the use of type returned in computation expression
      2. General computation expression design considerations
        1. Implications of the design considerations of computation expression
      3. Considerations of computation expression optimization
      4. Summary