Q: Forcing repeated evaluation

Colin Runciman colin@cs.york.ac.uk
Thu, 12 Sep 2002 11:01:10 +0100


Jan Kybic wrote:

>Hello,
>        I have another question regarding the optimisation of Haskell code:
>I have a relatively inexpensive function generating a long list, imagine
>something like (I simplified a lot):
>
>l = [ i*i*i | i <- [0..n] ]   -- for very large n
>
>This long list is consumed several times in the program:
>
>x1 = f1 l
>x2 = f2 x1 l
>x3 = f3 x2 l
>
>I found that the list l is calculated just once and that the
>computational time is dominated by the allocations and garbage
>collection. I want to try to force l to be generated on-the-fly
>every time it is needed, to see if it improves performance.
>What is a good way to do it? Would something like
>
>unsafePerformIO $ return l
>
>do the job? Isn'it there any flag for the compiler (ghc) to suggest
>this optimisation? Thank you for your feedback.
>
>Jan
>
A simple solution is to "decaf" the offending definition.
Give l an argument of trivial type: replace l both in its
definition and at all point of use by the application l ().

>
>