Performance/Overloading

From HaskellWiki
< Performance
Revision as of 11:22, 10 January 2006 by Simonmar (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Overloaded functions are not your friend

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. Each type class constraint on an overloaded function corresponds to an extra argument passed at runtime (called a dictionary), and a reference to an overloaded function is implemented by extracting a field from the dictionary. The presence of overloading can prevent many other optimisations, such as Strictness, from kicking in.

How can you squash overloading?

Give explicit type signatures: Signatures are the basic trick; putting them on exported, top-level functions is good software-engineering practice, anyway. (GHC 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.

Use SPECIALIZE pragmas: Specialize the overloading on key functions in your program. (Supported by: GHC, JHC).