[Haskell] Space behaviour & hyperseq
Arjan van IJzendoorn
afie at cs.uu.nl
Thu Jun 17 08:45:30 EDT 2004
I supervise a student who uses Haskell for simulating neural nets. A lot of
computation goes on there and the program cannot handle as many iterations
as we would like. We have improved performance by a factor of more than ten
by putting strictness annotations in all data types. But the problem is that
there are lists within these data structures and they are not strict. By
using a trick I have now found that making the whole program state strict
the program gets a much better looking heap profile. The trick I use is by
writing a function hyperseq and then applying it before the next iterations
hyperseq x y = if x==x then y else error "this is very unlikely"
This is very expensive and I chose to apply the function only once every 100
iterations. This gives reasonable performance.
Two questions remain:
1) Is there a more efficient definition of hyperseq that does not traverse
the data structure twice? The "show" function traverses the structure once
but I found it to be much slower.
2) In this application the uses of lazy evaluation are rare and easily
eliminated (zip xs [1..] and so on); is there some hidden GHC option that
evaluates everything strictly? I realise that this would invalidate
optimisations relying on certain laws but I just wonder how difficult this
would be. Somebody must have given this a thought at one point.
More information about the Haskell