mapM_ for bytestring

Shachaf Ben-Kiki shachaf at gmail.com
Sun Sep 1 18:44:17 CEST 2013


On Sun, Sep 1, 2013 at 6:16 AM, Henning Thielemann
<lemming at henning-thielemann.de> wrote:
>>     mapM  :: Monad m => (Word8 -> m Word8) -> ByteString -> m ByteString
>
> This one can become very inefficient. Think of m = [], then many ByteStrings
> have to be constructed by repreated ByteString.cons which requires a lot of
> copying. For lists "cons" aka (:) is efficient because the tails are shared.
>

You don't need to cons -- you can allocate one ByteString and write
into it unsafely (just like map does). See
<http://hackage.haskell.org/packages/archive/lens/3.9.0.3/doc/html/src/Control-Lens-Internal-ByteString.html>.

    Shachaf




More information about the Libraries mailing list