Haskell High Performance Programming
eBook - ePub

Haskell High Performance Programming

Samuli Thomasson

Buch teilen
  1. 408 Seiten
  2. English
  3. ePUB (handyfreundlich)
  4. Über iOS und Android verfügbar
eBook - ePub

Haskell High Performance Programming

Samuli Thomasson

Angaben zum Buch
Buchvorschau
Inhaltsverzeichnis
Quellenangaben

Über dieses Buch

Boost the performance of your Haskell applications using optimization, concurrency, and parallel programming

About This Book

  • Explore the benefits of lazy evaluation, compiler features, and tools and libraries designed for high performance
  • Write fast programs at extremely high levels of abstraction
  • Work through practical examples that will help you address the challenges of writing efficient code

Who This Book Is For

To get the most out of this book, you need to have a working knowledge of reading and writing basic Haskell. No knowledge of performance, optimization, or concurrency is required.

What You Will Learn

  • Program idiomatic Haskell that's also surprisingly efficient
  • Improve performance of your code with data parallelism, inlining, and strictness annotations
  • Profile your programs to identify space leaks and missed opportunities for optimization
  • Find out how to choose the most efficient data and control structures
  • Optimize the Glasgow Haskell Compiler and runtime system for specific programs
  • See how to smoothly drop to lower abstractions wherever necessary
  • Execute programming for the GPU with Accelerate
  • Implement programming to easily scale to the cloud with Cloud Haskell

In Detail

Haskell, with its power to optimize the code and its high performance, is a natural candidate for high performance programming. It is especially well suited to stacking abstractions high with a relatively low performance cost. This book addresses the challenges of writing efficient code with lazy evaluation and techniques often used to optimize the performance of Haskell programs.

We open with an in-depth look at the evaluation of Haskell expressions and discuss optimization and benchmarking. You will learn to use parallelism and we'll explore the concept of streaming. We'll demonstrate the benefits of running multithreaded and concurrent applications. Next we'll guide you through various profiling tools that will help you identify performance issues in your program. We'll end our journey by looking at GPGPU, Cloud and Functional Reactive Programming in Haskell. At the very end there is a catalogue of robust library recommendations with code samples.

By the end of the book, you will be able to boost the performance of any app and prepare it to stand up to real-world punishment.

Style and approach

This easy-to-follow guide teaches new practices and techniques to optimize your code, and then moves towards more advanced ways to effectively write efficient Haskell code. Small and simple practical examples will help you test the concepts yourself, and you will be able to easily adapt them for any application.

Häufig gestellte Fragen

Wie kann ich mein Abo kündigen?
Gehe einfach zum Kontobereich in den Einstellungen und klicke auf „Abo kündigen“ – ganz einfach. Nachdem du gekündigt hast, bleibt deine Mitgliedschaft für den verbleibenden Abozeitraum, den du bereits bezahlt hast, aktiv. Mehr Informationen hier.
(Wie) Kann ich Bücher herunterladen?
Derzeit stehen all unsere auf Mobilgeräte reagierenden ePub-Bücher zum Download über die App zur Verfügung. Die meisten unserer PDFs stehen ebenfalls zum Download bereit; wir arbeiten daran, auch die übrigen PDFs zum Download anzubieten, bei denen dies aktuell noch nicht möglich ist. Weitere Informationen hier.
Welcher Unterschied besteht bei den Preisen zwischen den Aboplänen?
Mit beiden Aboplänen erhältst du vollen Zugang zur Bibliothek und allen Funktionen von Perlego. Die einzigen Unterschiede bestehen im Preis und dem Abozeitraum: Mit dem Jahresabo sparst du auf 12 Monate gerechnet im Vergleich zum Monatsabo rund 30 %.
Was ist Perlego?
Wir sind ein Online-Abodienst für Lehrbücher, bei dem du für weniger als den Preis eines einzelnen Buches pro Monat Zugang zu einer ganzen Online-Bibliothek erhältst. Mit über 1 Million Büchern zu über 1.000 verschiedenen Themen haben wir bestimmt alles, was du brauchst! Weitere Informationen hier.
Unterstützt Perlego Text-zu-Sprache?
Achte auf das Symbol zum Vorlesen in deinem nächsten Buch, um zu sehen, ob du es dir auch anhören kannst. Bei diesem Tool wird dir Text laut vorgelesen, wobei der Text beim Vorlesen auch grafisch hervorgehoben wird. Du kannst das Vorlesen jederzeit anhalten, beschleunigen und verlangsamen. Weitere Informationen hier.
Ist Haskell High Performance Programming als Online-PDF/ePub verfügbar?
Ja, du hast Zugang zu Haskell High Performance Programming von Samuli Thomasson im PDF- und/oder ePub-Format sowie zu anderen beliebten Büchern aus Computer Science & Software Development. Aus unserem Katalog stehen dir über 1 Million Bücher zur Verfügung.

Information

Jahr
2016
ISBN
9781786464217

Haskell High Performance Programming


Table of Contents

Haskell High Performance Programming
Credits
About the Author
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Identifying Bottlenecks
Meeting lazy evaluation
Writing sum correctly
Weak head normal form
Folding correctly
Memoization and CAFs
Constant applicative form
Recursion and accumulators
The worker/wrapper idiom
Guarded recursion
Accumulator parameters
Inspecting time and space usage
Increasing sharing and minimizing allocation
Compiler code optimizations
Inlining and stream fusion
Polymorphism performance
Partial functions
Summary
2. Choosing the Correct Data Structures
Annotating strictness and unpacking datatype fields
Unbox with UNPACK
Using anonymous tuples
Performance of GADTs and branching
Handling numerical data
Handling binary and textual data
Representing bit arrays
Handling bytes and blobs of bytes
Working with characters and strings
Using the text library
Builders for iterative construction
Builders for strings
Handling sequential data
Using difference lists
Difference list performance
Difference list with the Writer monad
Using zippers
Accessing both ends fast with Seq
Handling tabular data
Using the vector package
Handling sparse data
Using the containers package
Using the unordered-containers package
Ephemeral data structures
Mutable references are slow
Using mutable arrays
Using mutable vectors
Bubble sort with vectors
Working with monads and monad stacks
The list monad and its transformer
Free monads
Working with monad transformers
Speedup via continuation-passing style
Summary
3. Profile and Benchmark to Your Heart's Content
Profiling time and allocations
Setting cost centres manually
Setting cost centres automatically
Installing libraries with profiling
Debugging unexpected crashes with profiler
Heap profiling
Cost centre-based heap profiling
Objects outside the heap
Retainer profiling
Biographical profiling
Benchmarking using the criterion library
Profile and monitor in real time
Monitoring over HTTP with ekg
Summary
4. The Devil's in the Detail
The anatomy of a Haskell project
Useful fields and flags in cabal files
Test suites and benchmarks
Using the stack tool
Multi-package projects
Erroring and handling exceptions
Handling synchronous errors
The exception hierarchy
Handling asynchronous errors
Throw and catch in other monads besides IO
Writing tests for Haskell
Property checks
Unit testing with HUnit
Test frameworks
Trivia at term-level
Coding in GHC PrimOps
Control inlining
Using rewrite rules
Specializing definitions
Phase control
Trivia at type-level
Phantom types
Functional dependencies
Type families and associated types
Useful GHC extensions
Monomorphism Restriction
Extensions for patterns and guards
Strict-by-default Haskell
Summary
5. Parallelize for Performance
Primitive parallelism and the Runtime System
Spark away
Subtle evaluation – pseq
When in doubt, use the force
The Eval monad and strategies
Composing strategies
Fine-tune granularity with chunking and buffering
The Par monad and schedules
spawn for futures and promises
Non-deterministic parallelism with ParIO
Diagnosing parallelism – ThreadScope
Data parallel programming – Repa
Playing with Repa in GHCi
Mapping and delayed arrays
Reduction via folding
Manifest representations
Delayed representation and fusion
Indices, slicing, and extending arrays
Convolution with stencils
Cursored and partitioned arrays
Writing fast Repa code
Additional libraries
Example from image processing
Loading the image from file
Identifying letters with convolution
Extracting strings from an image
Testing and evaluating performance
Summary
6. I/O and Streaming
Reading, writing, and handling resources
Traps of lazy I/O
File handles, buffering, and encoding
Binary I/O
Textual I/O
I/O performance with filesystem objects
Sockets and networking
Acting as a TCP/IP client
Acting as a TCP server (Unix domain sockets)
Raw UDP traffic
Networking above the transport layer
Managing resources with ResourceT
Streaming with side-effects
Choosing a streaming library
Simple streaming using io-streams
Creating input streams
Using combinators and output streams
Handling exceptions and resources in streams
An example of parsing using io-streams and attoparsec
Streaming using pipes
Composing and executing pipes
For loops and category theory in pipes
Handling exceptions in pipes
Strengths and weaknesses of pipes
Streaming using conduits
Handling resources and exceptions in conduits
Resuming conduits
Logging in Haskell
Logging with FastLogger
More abstract loggers
Timed log messages
Monadic logging
Customizing monadic loggers
Summary
7. Concurrency and Performance
Threads and concurrency primitives
Threads and mutable references
Avoid accumulating thunks
Atomic operations with IORefs
MVar
MVars are fair
MVar as a building block
Broadcasting with Chan
Software Transactional Memory
STM example – Bank accounts
Alternative transactions
Exceptions in STM
Runtime System and threads
Masking asynchronous exceptions
Asynchronous processing
Using the Async API
Async example – Timeouts
Composing with Concurrently
Lifting up from I/O
Top-level mutable references
Lifting from a base monad
Lifting base with exception handling
Summary
8. Tweaking the Compiler and Runtime System (GHC)
Using GHC like a pro
Operating GHC
Circular dependencies
Adjusting optimizations and transformations
The state hack
Floating lets in and out
Eliminating common subexpressions
Liberate-case duplicates code
Compiling via the LLVM route
Linking and building shared libraries
Preprocessing Haskell source code
Enforcing type-safety using Safe Haskell
Tuning GHC's Runtime System
Scheduler and green threads
Sparks and spark pool
Bounded threads and affinity
Indefinite blocking...

Inhaltsverzeichnis