I'm trying to do reasonably fast character escaping in Haskell (that is, within a few factors of C). Using plain ByteString manipulation, my Haskell implementation is 125 times slower than my C implementation. Then I tried to use blaze-builder. That was even slower.
I eventually managed to get it to about 1.5 times slower than the C version (very good!). The key function is this:
concatMapWrite :: (Word8 -> Write) -> ByteString -> ByteString
It is like concatMap from Data.ByteString. However, instead of the callback returning a ByteString, it returns a Write which is used to write the data directly into a buffer.
However, there are two problems:
-
It uses a lot of ugly buffer manipulation,
-
The performance boost is very sensitive to what gets inlined and what doesn't.
Is there a better way to translate a sequence of bytes in Haskell than this?