on master help sign in
Drop the Builder-based encodeUtf8 implementation

While it is very cool indeed, it is slower than the new C code under all circumstances, sometimes by a factor of two or more.

commita9fa48eeb8596b7c748bd62c026959efa48d36a6 authorBryan O'Sullivan <bos@serpentine.com> dateWed 8 Jan 2014, 13:57 (13 years ago) parent614e82e
1 file changed+3−18
Data/Text/Encoding.hs+3−18
Data/Text/Encoding.hs+3−18
@@ -311,22 +311,8 @@ decodeUtf8' :: ByteString -> Either UnicodeException Text
311311decodeUtf8' = unsafeDupablePerformIO . try . evaluate . decodeUtf8With strictDecode
312312{-# INLINE decodeUtf8' #-}
313313
314−-- | Encode text using UTF-8 encoding.
315−encodeUtf8 :: Text -> ByteString
316314#if MIN_VERSION_bytestring(0,10,4)
317315
318−encodeUtf8 t@(Text _arr _off len) =
319− B.copy -- copy to trim and avoid wasting memory
320− $ BL.toStrict
321− $ B.toLazyByteStringWith strategy BL.empty
322− $ encodeUtf8Builder t
323− where
324− -- We use a strategy that allocates a buffer that is guaranteed to be
325− -- large enough for the whole result. This ensures that we always stay in
326− -- the fast 'goPartial' loop in the 'encodeUtf8BuilderEscaped' function.
327− strategy = B.untrimmedStrategy (4 * (len + 1)) B.defaultChunkSize
328−
329−
330316-- | Encode text to a ByteString 'B.Builder' using UTF-8 encoding.
331317encodeUtf8Builder :: Text -> B.Builder
332318encodeUtf8Builder = encodeUtf8BuilderEscaped (BP.liftFixedToBounded BP.word8)
@@ -387,13 +373,12 @@ encodeUtf8BuilderEscaped be =
387373 outerLoop i (B.BufferRange op ope)
388374 where
389375 poke8 j v = poke (op `plusPtr` j) (fromIntegral v :: Word8)
376+#endif
390377
391−#else
392−
378+-- | Encode text using UTF-8 encoding.
379+encodeUtf8 :: Text -> ByteString
393380encodeUtf8 = encodeUtf8_0
394381
395−#endif
396−
397382encodeUtf8_0 :: Text -> ByteString
398383encodeUtf8_0 (Text arr off len) = unsafeDupablePerformIO $ do
399384 let size0 = max len 4