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
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?