[clean-list] Matrix-Matrix-Multiplication and Cleans memory

Witold J. Wnuk w.wnuk@zodiac.mimuw.edu.pl
Fri, 03 Nov 2000 18:23:20 +0100 (CET)


I don't know whether this is the source of your problems or not but
array comprehension itself can both slow down execution and
increase memory usage.


Example:

In raytracer we wrote, there was a function to prepare output to be
written to file:


imageToString :: ![(Char, Char, Char)] -> !{#Char} // !DEF
imageToString im = { k c \\ (c, k) <- [ (x, y) \\ x <- im, y <- [red,
         green, blue] ] }
        where red (r, _, _) = r; green (_, g, _) = g; blue (_, _, b) = b



It took about 15MB of memory for 320 x 240 picture. For 640 x 480 it
was ~ 80MB.

Adding this hack:

emptya :: !.{#Char}
emptya = createArray (XSIZE * YSIZE * 3) (toChar 0)

imageToString_ :: ![(Char, Char, Char)] -> !{#Char} // !DEF
imageToString_ im = { emptya & [i] = k c \\
         i <- [1..(XSIZE * YSIZE * 3)] &
         (c, k) <- [ (x, y) \\ x <- im, y <- [red, green, blue] ] }
        where red (r, _, _) = r; green (_, g, _) = g; blue (_, _, b) = b

would save lots of memory - default 2MB of heap would be enough now.

Eventually we solved it in yet another way.


I guess it is because during array comprehension (when the final size
is unknown) the array is copied many times (as it grows). Creating
"empty" array with known size and than updating saves this work. It's
not clear for me why it takes so much heap space though.. (why GC
doesn't free it in the mean time) - of course I'm speculating here -
can somebody with knowledge of Clean internals speak up?


While it might not be direct cause of your problem, it may matter in
other cases.


Greetings,


        Witold J. Wnuk


PS: It was raytracer for ICFP - we haven't won though.