The Glasgow Haskell Compiler User's Guide, Version 4.06 | ||
---|---|---|
Prev | Chapter 5. Advice on: sooner, faster, smaller, stingier | Next |
The key tool to use in making your Haskell program run faster are GHC's profiling facilities, described separately in Chapter 4. There is no substitute for finding where your program's time/space is really going, as opposed to where you imagine it is going.
Another point to bear in mind: By far the best way to improve a program's performance dramatically is to use better algorithms. Once profiling has thrown the spotlight on the guilty time-consumer(s), it may be better to re-think your program than to try all the tweaks listed below.
Another extremely efficient way to make your program snappy is to use library code that has been Seriously Tuned By Someone Else. You might be able to write a better quicksort than the one in the HBC library, but it will take you much longer than typing import QSort. (Incidentally, it doesn't hurt if the Someone Else is Lennart Augustsson.)
Please report any overly-slow GHC-compiled programs. The current definition of “overly-slow” is “the HBC-compiled version ran faster”…
This is the most basic way to make your program go faster. Compilation time will be slower, especially with -O2.
At present, -O2 is nearly indistinguishable from -O.
Even with -O, GHC tries to use a native-code generator, if available. But the native code-generator is designed to be quick, not mind-bogglingly clever. Better to let GCC have a go, as it tries much harder on register allocation, etc.
So, when we want very fast code, we use: -O -fvia-C -O2-for-C.
Haskell's overloading (using type classes) is elegant, neat, etc., etc., but it is death to performance if left to linger in an inner loop. How can you squash it?
Signatures are the basic trick; putting them on exported, top-level functions is good software-engineering practice, anyway. (Tip: using -fwarn-missing-signatures can help enforce good signature-practice).
The automatic specialisation of overloaded functions (with -O) should take care of overloaded local and/or unexported functions.
Specialize the overloading on key functions in your program. See Section 6.10.3 and Section 6.10.4.
A low-tech way: grep (search) your interface files for overloaded type signatures; e.g.,:
% egrep '^[a-z].*::.*=>' *.hi |
and, among other things, lazy pattern-matching is your enemy.
(If you don't know what a “strict function” is, please consult a functional-programming textbook. A sentence or two of explanation here probably would not do much good.)
Consider these two code fragments:
f (Wibble x y) = ... # strict f arg = let { (Wibble x y) = arg } in ... # lazy |
A less contrived example shows the use of cases instead of lets to get stricter code (a good thing):
f (Wibble x y) # beautiful but slow = let (a1, b1, c1) = unpackFoo x (a2, b2, c2) = unpackFoo y in ... f (Wibble x y) # ugly, and proud of it = case (unpackFoo x) of { (a1, b1, c1) -> case (unpackFoo y) of { (a2, b2, c2) -> ... }} |
It's all the better if a function is strict in a single-constructor type (a type with only one data-constructor; for example, tuples are single-constructor types).
If your datatype has a single constructor with a single field, use a newtype declaration instead of a data declaration. The newtype will be optimised away in most cases.
Don't guess—look it up.
Look for your function in the interface file, then for the third field in the pragma; it should say _S_ <string>. The <string> gives the strictness of the function's arguments. L is lazy (bad), S and E are strict (good), P is “primitive” (good), U(...) is strict and “unpackable” (very good), and A is absent (very good).
For an “unpackable” U(...) argument, the info inside tells the strictness of its components. So, if the argument is a pair, and it says U(AU(LSS)), that means “the first component of the pair isn't used; the second component is itself unpackable, with three components (lazy in the first, strict in the second \& third).”
If the function isn't exported, just compile with the extra flag -ddump-simpl; next to the signature for any binder, it will print the self-same pragmatic information as would be put in an interface file. (Besides, Core syntax is fun to look at!)
Placing INLINE pragmas on certain functions that are used a lot can have a dramatic effect. See Section 6.10.1.
If you do not have an explicit export list in a module, GHC must assume that everything in that module will be exported. This has various pessimising effects. For example, if a bit of code is actually unused (perhaps because of unfolding effects), GHC will not be able to throw it away, because it is exported and some other module may be relying on its existence.
GHC can be quite a bit more aggressive with pieces of code if it knows they are not exported.
(The form in which GHC manipulates your code.) Just run your compilation with -ddump-simpl (don't forget the -O).
If profiling has pointed the finger at particular functions, look at their Core code. lets are bad, cases are good, dictionaries (d.<Class>.<Unique>) [or anything overloading-ish] are bad, nested lambdas are bad, explicit data constructors are good, primitive operations (e.g., eqInt#) are good,…
When you are really desperate for speed, and you want to get right down to the “raw bits.” Please see Section 6.1 for some information about using unboxed types.
This may take real work, but… There exist piles of massively-tuned library code, and the best thing is not to compete with it, but link with it.
Section 6.4 says a little about how to use C calls.
We don't provide specialisations of Prelude functions for Float (but we do for Double). If you end up executing overloaded code, you will lose on performance, perhaps badly.
Floats (probably 32-bits) are almost always a bad idea, anyway, unless you Really Know What You Are Doing. Use Doubles. There's rarely a speed disadvantage—modern machines will use the same floating-point unit for both. With Doubles, you are much less likely to hang yourself with numerical errors.
One time when Float might be a good idea is if you have a lot of them, say a giant array of Floats. They take up half the space in the heap compared to Doubles. However, this isn't true on a 64-bit machine.
If your program's GC stats (-S RTS option) indicate that it's doing lots of garbage-collection (say, more than 20% of execution time), more memory might help—with the -M<size> or -A<size> RTS options (see Section 3.12.1).