Unexpected lack of optimisation

Neil Mitchell ndmitchell at gmail.com
Tue Apr 29 15:52:08 EDT 2008


> | {-# INLINE foo #-}
>  | foo = large
>  |
>  | bar x = if x then res else res
>  |     where res = foo
>  |
>  | By putting an INLINE on foo I am able to persuade it to be inlined
>  | into the binding of bar, but I can't then persuade it to be inlined at
>  | the let expression.
> I'm not certain what you mean here.  I think you mean that in the above code you end up with
>         bar x = let res = large in if x then res else res
>  whereas what you wanted was
>         bar x = if x then large else large

Yes, that is exactly it.

>  In your example, you want 'res' to inline "first".  You can get that by explicit control:
>  {-# NOINLINE [0] foo #-}
>  That says "don't inline foo before phase 0", which in turn gives time for 'res' to get inlined first.  I'm not certain whether that'll help in your actual example.

It worked, with:

{-# INLINE [1] begin1 #-}
{-# INLINE begin2 #-}

I don't think this approach will compose particularly well, and in the
real case I was trying (not this reduced example) I don't think it
will work because there is some recursion and RULES involved. I'll
have another go with the full example in future.

I did however notice an issue while doing this work - in GHC 6.8.2 and
HEAD from Christmas.

{-# INLINE begin2
Temp.hs:7:1: lexical error at character '}'

In particular if you use the RULES example from the manual, then it
doesn't work.



More information about the Glasgow-haskell-users mailing list