Annotation for unfolding wanted

Simon Peyton-Jones simonpj at microsoft.com
Tue Jul 31 05:22:14 EDT 2007


| I was wondering why we don't have an annotation or pragma for function to tell
| the compiler that we _need_ this particular recursive function to be unfolded.
| If the compiler cannot do this for some reason it should produce an error
| message to help you modifying your code. I have regularly problems that my
| code is either not strict enough or my functions are not unfolded. I find it
| annoying that this is a regular show stopper and consumes much time to fix.
| Here is an example: a search function for strings, which should return the
| index and the rest of the string after the first occurrence: search0 will not
| be unfolded by ghc -O even with {#- INLINE search0 -#} (I don't know why, it
| looks tail-recursive to me)

GHC never inlines recursive functions.  Why not?  Because doing so exposes a new inline opportunity.  How many times would you like it inlined?  Not forever, I assume!

Some compilers unroll recursive functions by inlining them N times, for some fixed N (say 3 or so).  This reduces the loop overheads.  GHC doesn't do this, although it'd be nice, because it makes repeated traversals of the code, and it's hard to spot when the function has been unrolled 3 times and then stop.  If you look at the code after unrolling 3 times, from scratch, there's another call just waiting to be inlined.

I'm not saying this couldn't be improved -- but I don't see an easy way to improve it.

Simon


More information about the Glasgow-haskell-users mailing list