packages feed

papa-0.7.0: bench/Text.hs

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

{- HLINT ignore "Avoid restricted function" -}
-- Reason: these benchmark the total head, maximum and index of the Text and
-- ByteString modules against the partial ones of text and bytestring, whose
-- names HLint restricts whatever module they come from.

-- | Benchmarks of the Text and ByteString modules against the functions of
-- text and bytestring that they replace.
module Text (
  textBenchmarks,
) where

import Criterion.Main (Benchmark, bench, bgroup, env, nf, nfIO, whnf)
import qualified Data.ByteString as SB
import qualified Data.ByteString.Char8 as SC
import qualified Data.Text as ST
import qualified Data.Text.Encoding as STE
import qualified Data.Text.IO as STIO
import qualified Data.Text.Read as STR
import Papa
import qualified Papa.Bytestring.Data.ByteString as B
import qualified Papa.Bytestring.Data.ByteString.Char8 as BC
import qualified Papa.Text.Data.Text as T
import qualified Papa.Text.Data.Text.Encoding as TE
import qualified Papa.Text.Data.Text.IO as TIO
import qualified Papa.Text.Data.Text.Read as TR
import qualified System.Directory as D
import qualified System.IO as IO

-- | 100000 characters, some of them not ASCII.
bigText :: Text
bigText =
  ST.replicate 20000 "ab\233\955c"

-- | 100000 bytes.
bigBytes :: ByteString
bigBytes =
  SB.take 100000 (SB.concat (replicate 400 (SB.pack [0 .. 255])))

-- | 100000 ASCII characters, which "Data.Text.IO" writes in any locale.
asciiText :: Text
asciiText =
  ST.replicate 20000 "abcde"

-- | 10000 short lines, some of their characters not ASCII.
shortLines :: [Text]
shortLines =
  replicate 10000 "hello, w\246rld \955"

-- | 100 lines of 10000 characters, some of them not ASCII.
longLines :: [Text]
longLines =
  replicate 100 (ST.replicate 2000 "ab\233\955c")

-- | A handle opened for writing UTF-8, block-buffered, as "Data.Text.IO"
-- needs to write any character; and the same for reading.
withUtf8 :: FilePath -> IO.IOMode -> (IO.Handle -> IO a) -> IO a
withUtf8 file mode k =
  IO.withFile file mode (\h -> IO.hSetEncoding h IO.utf8 >> IO.hSetBuffering h (IO.BlockBuffering Nothing) >> k h)

-- | Write lines with Papa's hPutStrLn.
papaPutLines :: FilePath -> [Text] -> IO ()
papaPutLines file ls =
  withUtf8 file IO.WriteMode (\h -> traverse_ (runExceptT . TIO.hPutStrLn h) ls)

-- | Write lines with "Data.Text.IO"'s hPutStrLn.
textPutLines :: FilePath -> [Text] -> IO ()
textPutLines file ls =
  withUtf8 file IO.WriteMode (\h -> traverse_ (STIO.hPutStrLn h) ls)

-- | Read every line with Papa's hGetLine.
papaGetLines :: FilePath -> IO Int
papaGetLines file =
  withUtf8 file IO.ReadMode (\h -> let go n = runExceptT (TIO.hGetLine h) >>= either (\_ -> pure n) (\t -> go $! n + ST.length t) in go 0)

-- | Read every line with "Data.Text.IO"'s hGetLine.
textGetLines :: FilePath -> IO Int
textGetLines file =
  withUtf8 file IO.ReadMode (\h -> let go n = IO.hIsEOF h >>= bool (STIO.hGetLine h >>= \t -> go $! n + ST.length t) (pure n) in go 0)

-- | A file in the temporary directory, of lines.
tempFile :: FilePath -> [Text] -> IO FilePath
tempFile name ls = do
  file <- fmap (<> name) D.getTemporaryDirectory
  file <$ textPutLines file ls

-- | The benchmarks of the Text and ByteString modules.
textBenchmarks :: [Benchmark]
textBenchmarks =
  [ bgroup
      "text"
      [ bench "Papa Text.head" (nf T.head bigText)
      , bench "Data.Text head" (nf ST.head bigText)
      , bench "Papa Text.maximum" (nf T.maximum bigText)
      , bench "Data.Text maximum" (nf ST.maximum bigText)
      , bench "Papa Text.index (last)" (nf (T.index bigText) 99999)
      , bench "Data.Text index (last)" (nf (ST.index bigText) 99999)
      , bench "Papa Text.splitOn" (nf (T.splitOn "\955") bigText)
      , bench "Data.Text splitOn" (nf (ST.splitOn "\955") bigText)
      , bench "Papa Text.replace" (nf (T.replace "\955" "x") bigText)
      , bench "Data.Text replace" (nf (ST.replace "\955" "x") bigText)
      , bench "Papa Encoding.decodeUtf16LE" (nf TE.decodeUtf16LE (STE.encodeUtf16LE bigText))
      , bench "Data.Text.Encoding decodeUtf16LE" (nf STE.decodeUtf16LE (STE.encodeUtf16LE bigText))
      , bench "Papa Read.decimal (Int)" (nf (\t -> TR.decimal t :: Either [Char] (Int, Text)) "1234567890123")
      , bench "Data.Text.Read decimal (Int)" (nf (\t -> STR.decimal t :: Either [Char] (Int, Text)) "1234567890123")
      ]
  , bgroup
      "bytestring"
      [ bench "Papa ByteString.maximum" (nf B.maximum bigBytes)
      , bench "Data.ByteString maximum" (whnf SB.maximum bigBytes)
      , bench "Papa ByteString.foldl1'" (nf (B.foldl1' max) bigBytes)
      , bench "Data.ByteString foldl1'" (whnf (SB.foldl1' max) bigBytes)
      , bench "Papa Char8.pack (100000)" (nf BC.pack (SC.unpack bigBytes))
      , bench "Data.ByteString.Char8 pack (100000)" (nf SC.pack (SC.unpack bigBytes))
      , bench "Papa Char8.elem (absent)" (nf (BC.elem '\0') (SB.filter (/= 0) bigBytes))
      , bench "Data.ByteString.Char8 elem (absent)" (nf (SC.elem '\0') (SB.filter (/= 0) bigBytes))
      ]
  , env
      (fmap (<> "/papa-bench-text.txt") D.getTemporaryDirectory)
      ( \file ->
          bgroup
            "text io"
            [ bench "Papa TextIO.writeFile and readFile (100000 characters)" (nfIO (fmap (preview _Right) (runExceptT (TIO.writeFile file asciiText >> TIO.readFile file))))
            , bench "Data.Text.IO writeFile and readFile (100000 characters)" (nfIO (STIO.writeFile file asciiText >> STIO.readFile file))
            , bench "Papa TextIO.hPutStrLn (10000 short lines)" (nfIO (papaPutLines file shortLines))
            , bench "Data.Text.IO hPutStrLn (10000 short lines)" (nfIO (textPutLines file shortLines))
            , bench "Papa TextIO.hPutStrLn (100 lines of 10000)" (nfIO (papaPutLines file longLines))
            , bench "Data.Text.IO hPutStrLn (100 lines of 10000)" (nfIO (textPutLines file longLines))
            ]
      )
  , env
      (tempFile "/papa-bench-lines.txt" shortLines)
      ( \file ->
          bgroup
            "text io read"
            [ bench "Papa TextIO.hGetLine (10000 short lines)" (nfIO (papaGetLines file))
            , bench "Data.Text.IO hGetLine (10000 short lines)" (nfIO (textGetLines file))
            ]
      )
  , env
      (tempFile "/papa-bench-big.txt" (concat (replicate 10 longLines)))
      ( \file ->
          bgroup
            "text io readFile"
            [ bench "Papa TextIO.readFile (10 MB)" (nfIO (fmap (preview _Right) (runExceptT (TIO.readFile file))))
            , bench "Data.Text.IO readFile (10 MB)" (nfIO (withUtf8 file IO.ReadMode STIO.hGetContents))
            ]
      )
  ]