[Haskell-cafe] using Map rather than FiniteMap

Simon Marlow simonmar at microsoft.com
Wed Jan 26 08:14:33 EST 2005


On 25 January 2005 23:27, S. Alexander Jacobson wrote:

> Oops.  It pays to check your checking code before
> making posts like this.
> 
> After actually running the correct test, I am
> still getting semi-ridiculous space behavior
> (6k/pair)!
> 
>     import qualified Map
>     zipped =zip [1..] [1..100000]::[(Int,Int)]
>     untup f (x,y) = f x y
>     produce = foldr (untup Map.insert) Map.empty zipped
>     fm = length  $ Map.keys produce
>     main = print $ fm
> 
> Has this profile:
> 
>  	   example +RTS -p -K5M -RTS
> 
>  	total time  =        5.10 secs   (255 ticks @ 20 ms)
>  	total alloc = 612,534,236 bytes  (excludes profiling overheads)
> 
>     	COST CENTRE                    MODULE               %time %alloc
>  	balance                        Map                   71.8   72.8
>  	insert                         Map                   12.2   10.8
>  	size                           Map                    9.0    9.7
>  	bin                            Map                    2.4    2.2
>  	rotateR                        Map                    1.6    0.3
>  	zipped                         Main                   0.8    1.9
> 
> Notice the 612Mb for storing a mapping from Int
> to Int!!

Notice that's 612Mb *allocation* to create the finite map and
deconstruct it into a list.  Not 612Mb of live heap to store the finite
map.  If you make sure everything is evaluated, and examine the heap
profile I'm sure you'll find that the finite map is taking a reasonable
amount of space (perhaps 20-30 bytes per node for storing integers, I
guess).  

To get a rough idea of the total live heap without profiling, you can
run the program with +RTS -sstderr and look at the memory "in use"
figure.

Cheers,
	Simon


More information about the Haskell-Cafe mailing list