@@ -20,6 +20,7 @@ module Benchmarks.DecodeUtf8
21import Foreign.C.Types
21import Foreign.C.Types
22import Data.ByteString.Internal (ByteString(..))
22import Data.ByteString.Internal (ByteString(..))
23+import Data.ByteString.Lazy.Internal (ByteString(..))
23import Foreign.Ptr (Ptr, plusPtr)
24import Foreign.Ptr (Ptr, plusPtr)
24import Foreign.ForeignPtr (withForeignPtr)
25import Foreign.ForeignPtr (withForeignPtr)
25import Data.Word (Word8)
26import Data.Word (Word8)
@@ -38,8 +39,15 @@ benchmark kind fp = do
38 bs <- B.readFile fp
39 bs <- B.readFile fp
39 lbs <- BL.readFile fp
40 lbs <- BL.readFile fp
40 let bench name = C.bench (name ++ "+" ++ kind)
41 let bench name = C.bench (name ++ "+" ++ kind)
42+ decodeStream (Chunk b0 bs0) = case T.streamDecodeUtf8 b0 of
43+ T.Some t0 _ f0 -> t0 : go f0 bs0
44+ where go f (Chunk b bs1) = case f b of
45+ T.Some t1 _ f1 -> t1 : go f1 bs1
41 return $ bgroup "DecodeUtf8"
48 return $ bgroup "DecodeUtf8"
42 [ bench "Strict" $ nf T.decodeUtf8 bs
49 [ bench "Strict" $ nf T.decodeUtf8 bs
50+ , bench "Stream" $ nf decodeStream lbs
43 , bench "IConv" $ iconv bs
51 , bench "IConv" $ iconv bs
44 , bench "StrictLength" $ nf (T.length . T.decodeUtf8) bs
52 , bench "StrictLength" $ nf (T.length . T.decodeUtf8) bs
45 , bench "StrictInitLength" $ nf (T.length . T.init . T.decodeUtf8) bs
53 , bench "StrictInitLength" $ nf (T.length . T.init . T.decodeUtf8) bs
@@ -52,7 +60,7 @@ benchmark kind fp = do
52 , bench "LazyStringUtf8Length" $ nf (length . U8.toString) lbs
60 , bench "LazyStringUtf8Length" $ nf (length . U8.toString) lbs
55−iconv :: ByteString -> IO CInt
63+iconv :: B.ByteString -> IO CInt
56iconv (PS fp off len) = withForeignPtr fp $ \ptr ->
64iconv (PS fp off len) = withForeignPtr fp $ \ptr ->
57 time_iconv (ptr `plusPtr` off) (fromIntegral len)
65 time_iconv (ptr `plusPtr` off) (fromIntegral len)