Bools are not unboxed

Tomasz Zielonka t.zielonka at students.mimuw.edu.pl
Sun Oct 3 09:07:01 EDT 2004


Hello!

I was playing with monadic looping a'la replicateM_ and I created this
function:

    for :: Int -> IO () -> IO ()
    for 0 _ = return ()
    for n x = x >> for (n - 1) x

Compiled with -O2, it is really fast and makes no unnecessary
allocations. Tested with this main

    main = for 10000000 (return ())

it gives the following stats

    <<ghc: 1024 bytes, 0 GCs, 0/0 avg/max bytes residency (0 samples),
    1M in use, 0,00 INIT (0,00 elapsed), 0,33 MUT (0,33 elapsed), 0,00 GC
    (0,00 elapsed) :ghc>>

Cool! 

( this is still 10 times slower than g++ -O3, but a similar pure function
  is only 3 times slower, and I am satisfied with such results (at this
  moment ;) )

Unfortunately, the program I was playing with could call 'for' with
negative n, for which it was supposed to make 0 iterations, and this
version definitely makes too many iterations.

So I made another version:

    for :: Int -> IO () -> IO ()
    for n x | n > 0     = x >> for (n - 1) x
	    | otherwise = return ()

To my surprise, it was much slower and made many allocations:

    <<ghc: 240927488 bytes, 920 GCs, 1036/1036 avg/max bytes residency (1
    samples), 1M in use, 0,00 INIT (0,00 elapsed), 2,48 MUT (2,50 elapsed),
    0,04 GC (0,05 elapsed) :ghc>>

I checked in -ddump-simpl that Ints are getting unboxed in both
versions. 

Then I noticed the cause: 
    GHC.Prim.<# returns a boxed, heap allocated Bool, and so do other
    primitive comparison operators.

Would it be difficult to add Bool unboxing to GHC?
Maybe it would suffice to use preallocated False and True?

Best regards,
Tom

-- 
.signature: Too many levels of symbolic links


More information about the Glasgow-haskell-users mailing list