[Haskell-cafe] Squashing space leaks

Daniel Fischer daniel.is.fischer at web.de
Fri May 6 06:33:57 EDT 2005

Am Freitag, 6. Mai 2005 02:24 schrieb Greg Buchholz:
> Josef Svenningsson wrote:
> > I think the thing that really kills you is the way you shuffle around
> > the list in the advance function. Your commented out rotations
> > function has the same problem. Here is my attempt to solve the problem
> > a little more efficiently:
>     We're heading in the right direction anyway.  I can now compute 1
> million iteration in about 2 minutes (with +RTS -H750M -K100M).  Well
> almost, since it now doesn't compute the right answer, so something must
> be amiss in the shuffling section.  Now if we can get it to us a little
> less than 1G of memory, we'll be in good shape.
> Thanks,
> Greg Buchholz

The problem is that a prime crept in in Josef's code,
so to calculate the positions and velocities, the updated versions of the 
planets are used, it should be

update f newlist (a:as) = a' : update f (newlist . (a:)) as
               where a' = f a (newlist as)

instead of
update f newlist (a:as) = a' : update f (newlist . (a':)) as
               where a' = f a (newlist as).

Besides, offset_momentum does not use the parameter n at all and I think that 
all the indexing in the computation of the potential energy is rather 
inefficient. I rewrote it as

myEnergy :: [Planet] -> Double
myEnergy pps@(p:ps) = kinetic - (pot pps)
      kinetic = 0.5 * (sum $ map (\q -> dot (vel q) (vel q) * mass q) pps)
      pot = sum . pots
      pots [] = []
      pots (p:ps) = sum [m * (mass q) / (dist p q) | q <- ps]: pots ps
	                  m = mass p,

it didn't make it any faster, though. I did not yet space-profile


More information about the Haskell-Cafe mailing list