[Haskell-cafe] Slower with ByteStrings?

Donald Bruce Stewart dons at cse.unsw.edu.au
Sun May 27 01:57:14 EDT 2007


bos:
> Jason Dagit wrote:
> 
> >I think, given my simple algorithm that means that (==) for
> >ByteStrings is slower than (==) for String.  Is this possible?
> 
> Yes indeed.  Over ByteStrings, (==) is implemented as a call to memcmp. 
>  For small strings, this loses by a large margin because it has to go 
> through the FFI.
> 

Yes, a non-memcmp version can sometimes be profitably used here.

Something like this Core:

    eq !n (Ptr p) (Ptr q) = inlinePerformIO $ IO $ go n p q
      where 
        go !n p q s
            | n == 0    = (# s , True #)
            | otherwise = case readInt8OffAddr# p 0# s of
                    (# s, a #) -> case readInt8OffAddr# q 0# s of
                        (# s, b #) | a /=# b   -> (# s, False #)
                                   | otherwise -> go (n-1) (plusAddr# p 1#) (plusAddr# q 1#) s

Ok, so that's not Core, but it could be ;)

-- Don


More information about the Haskell-Cafe mailing list