on master help sign in
Use lower upper bound for pre-allocation in `encodeUtf8`
d87a8cfHerbert Valerio Riedel
Data/Text/Encoding.hs+1−1
@@ -373,7 +373,7 @@ encodeUtf8 :: Text -> ByteString
373encodeUtf8 (Text arr off len)
373encodeUtf8 (Text arr off len)
374 | len == 0 = B.empty
374 | len == 0 = B.empty
375 | otherwise = unsafeDupablePerformIO $ do
375 | otherwise = unsafeDupablePerformIO $ do
376− fp <- mallocByteString (len*4)
376+ fp <- mallocByteString (len*3) -- see https://github.com/haskell/text/issues/194 for why len*3 is enough
377 withForeignPtr fp $ \ptr ->
377 withForeignPtr fp $ \ptr ->
378 with ptr $ \destPtr -> do
378 with ptr $ \destPtr -> do
379 c_encode_utf8 destPtr (A.aBA arr) (fromIntegral off) (fromIntegral len)
379 c_encode_utf8 destPtr (A.aBA arr) (fromIntegral off) (fromIntegral len)