The following code example takes approx 2 seconds to execute. When the bang pattern in line 14 is removed however, it takes 60s. Can anyone explain what is going on?
I am using strict MVar so whatever is put into the MVar, should be fully evaluated to normal form. I would not expect a Bang pattern before inserting into the MVar to have any noticable effect.
{-# LANGUAGE BangPatterns #-}
import Control.Concurrent.MVar.Strict
import qualified Data.Text as T
import Data.Text.Encoding
main :: IO ()
main = do
mvar <- newMVar T.empty
let bsArr = map (\i -> encodeUtf8 $ T.pack $ "some strange string " ++ show i) [0 .. 30000 :: Int]
mvarWriter =
\lbs ->
let !decoded = decodeUtf8 lbs
in modifyMVar_ mvar (\oldText -> return $ oldText <> decoded)
mapM_ (\lbs -> mvarWriter lbs) bsArr
print . T.length =<< readMVar mvar