Haskell optimization of a function looking for a bytestring terminator

Posted by me2 on Stack Overflow See other posts from Stack Overflow or by me2
Published on 2010-03-20T11:38:42Z Indexed on 2010/03/20 11:51 UTC
Read the original article Hit count: 323

Filed under:
|

Profiling of some code showed that about 65% of the time I was inside the following code.

What it does is use the Data.Binary.Get monad to walk through a bytestring looking for the terminator. If it detects 0xff, it checks if the next byte is 0x00. If it is, it drops the 0x00 and continues. If it is not 0x00, then it drops both bytes and the resulting list of bytes is converted to a bytestring and returned.

Any obvious ways to optimize this? I can't see it.

parseECS = f [] False
    where
    f acc ff = do
        b <- getWord8
        if ff
            then if b == 0x00
                then f (0xff:acc) False
                else return $ L.pack (reverse acc)
            else if b == 0xff
                then f acc True
                else f (b:acc) False

© Stack Overflow or respective owner

Related posts about haskell

Related posts about optimization