Oh noes! I was miscalculating the initial buffer size!
When performance testing encodeUtf8, I noticed that for some reason I
was still seeing "ensure" show up in the profile, when I expected it
shouldn't have been.
Turns out I was using a "min" where I should have been using a "max",
and thus allocating an initial bytestring that would almost always be
too small, thus forcing reallocations and copying. Boo!
commit9dec3d6953ea636d206b164946da84f118bea9de
authorBryan O'Sullivan <bos@serpentine.com>
dateMon 27 Jun 2011, 18:45 (15 years ago)
parent4050227
Data/Text/Encoding.hs+1−1
@@ -144,7 +144,7 @@ decodeUtf8' = unsafePerformIO . try . evaluate . decodeUtf8With strictDecode
144144-- | Encode text using UTF-8 encoding.
145145encodeUtf8 :: Text -> ByteString
146146encodeUtf8 (Text arr off len) = unsafePerformIO $ do
147− let size0 = min len 4
147+ let size0 = max len 4
148148 mallocByteString size0 >>= start size0 off 0
149149 where
150150 start size n0 m0 fp = withForeignPtr fp $ loop n0 m0