Write fast haskell code

I am learning Haskell as my first functional language. I like it, although the learning curve is not like that of other languages.
The problem is that if I write a program in haskell, and it does not perform satisfactorily, it is hard for me to even imagine how to optimize it. Are there techniques to speed up programs? For example, the julia language has a section of the documentation, “performance tips”, devoted to getting performant code. Is there something similar with Haskell? Any recommendations?
I just know that I should compile with -O2 and explicitly annotate the functions.

2 Likes

Try Performance - HaskellWiki

1 Like

Some resources:

3 Likes

There’s also the work-in-progress Haskell Optimization Handbook

3 Likes

BTW, don’t take this to the extreme and annotate every function with an INLINE pragma, this will not do much besides increasing compilation times.

If you don’t know why inlining a specific function would be beneficial (and since you’re a beginner, you probably don’t in most cases), just leave it to the compiler. You can always profile and add some pragmas to crucial functions later.

2 Likes

So funny story, I know this is the general advice, but I was actually working on a project recently that was running more slowly than I thought it should, and on a whim I, uh, annotated every function (in a particular module) with an INLINE pragma, and my benchmarks suddenly started running in a quarter of the time.

No, INLINABLE wasn’t enough. Neither was the more arcane combination of using -fexpose-all-unfoldings on that module and bumping up all the -funfolding-*-discounts and thresholds by various ridiculous amounts. Only the sledgehammer produced the best results.

I don’t know if that means there’s a level of advanced optimization I have yet to attain because I don’t yet fully understand what I’m doing, or if the moral of the story is just that while spamming INLINE is not usually the right thing to do, very occasionally it is?

2 Likes