Bools are not unboxed

Tomasz Zielonka t.zielonka at
Sun Oct 3 12:24:56 EDT 2004

On Sun, Oct 03, 2004 at 04:03:55PM +0200, Carsten Schultz wrote:
> Hi Tomasz!

Hi Carsten!

> > To my surprise, it was much slower and made many allocations:
> [...
> > Then I noticed the cause: 
> >     GHC.Prim.<# returns a boxed, heap allocated Bool, and so do other
> >     primitive comparison operators.

I should have asked one fundamental question first: am I right? ;)

> That's not really the cause.  A function returning a boxed value does
> not necessarily have to allocate it, it is just a vectored return
> afaik.

I haven't heard about 'vectored return' before. I will try to find
more information about it. Maybe you can recommend something for me
to read?

> (Notice that $wfor again take three arguments, the last one being the
> state.)

Hmmm, I noticed that the number of arguments differed, there were even
some quiet alarm bells in my head, but I ignored it.

> for4 :: Int -> IO () -> IO ()
> for4 n x = if n `gt` 0 == 0 then return () else x >> (for4 (n-1) x)
> gt :: Int -> Int -> Int
> gt x y = if x > y then 1 else 0
> If you test it, it should be fast.

It is even slightly faster than my fastest version :)

> BTW, although counting upwards (and not solving the problem
> generally), the following is ok too:
> for2 :: Int -> IO () -> IO ()
> for2 n x = sequence_ [x | i <- [1..n]]

This one is amazing. It's 3 times faster than the previous one in spite
of being written in high level style.

I guess it's worth checking idiomatic Haskell style first, because there
is a big chance that GHC was optimised for it :)

However, it would be nice if all versions were as efficient... 

> Playing with the code generated by ghc is a great way to waste time
> for me.

Well, but you seem to be very good at it. Maybe it won't be such a waste
of time in the long term :)

> Wait until you have found the RULES-pragma :-)

I've already found it some time ago. I even tried to use them to
optimise vector/matrix expressions (to eliminate intermediate vectors),
but I remember that sometimes the rules didn't fire and I didn't
understand why.

> Have fun,
> Carsten

Hope this will teach me to avoid premature conclusions :-/


.signature: Too many levels of symbolic links

More information about the Glasgow-haskell-users mailing list