@@ -1,3 +1,5 @@
1+{-# LANGUAGE ForeignFunctionInterface #-}
2+
13-- | Test decoding of UTF-8
24--
35-- Tested in this benchmark:
@@ -16,7 +18,13 @@ module Data.Text.Benchmarks.DecodeUtf8
1618 ( benchmark
1719 ) where
1820
19−import Criterion (Benchmark, bgroup, bench, nf)
21+import Foreign.C.Types (CInt, CSize)
22+import Data.ByteString.Internal (ByteString(..))
23+import Foreign.Ptr (Ptr, plusPtr)
24+import Foreign.ForeignPtr (withForeignPtr)
25+import Data.Word (Word8)
26+import qualified Criterion as C
27+import Criterion (Benchmark, bgroup, nf)
2028import qualified Codec.Binary.UTF8.Generic as U8
2129import qualified Data.ByteString as B
2230import qualified Data.ByteString.Lazy as BL
@@ -25,12 +33,14 @@ import qualified Data.Text.Encoding as T
2533import qualified Data.Text.Lazy as TL
2634import qualified Data.Text.Lazy.Encoding as TL
2735
28−benchmark :: FilePath -> IO Benchmark
29−benchmark fp = do
36+benchmark :: String -> FilePath -> IO Benchmark
37+benchmark kind fp = do
3038 bs <- B.readFile fp
3139 lbs <- BL.readFile fp
40+ let bench name = C.bench (name ++ "+" ++ kind)
3241 return $ bgroup "DecodeUtf8"
3342 [ bench "Strict" $ nf T.decodeUtf8 bs
43+ , bench "IConv" $ iconv bs
3444 , bench "StrictLength" $ nf (T.length . T.decodeUtf8) bs
3545 , bench "StrictInitLength" $ nf (T.length . T.init . T.decodeUtf8) bs
3646 , bench "Lazy" $ nf TL.decodeUtf8 lbs
@@ -41,3 +51,9 @@ benchmark fp = do
4151 , bench "LazyStringUtf8" $ nf U8.toString lbs
4252 , bench "LazyStringUtf8Length" $ nf (length . U8.toString) lbs
4353 ]
54+
55+iconv :: ByteString -> IO CInt
56+iconv (PS fp off len) = withForeignPtr fp $ \ptr ->
57+ time_iconv (ptr `plusPtr` off) (fromIntegral len)
58+
59+foreign import ccall unsafe time_iconv :: Ptr Word8 -> CSize -> IO CInt