on b9e99db help sign in
133 lines 3.2 kB history raw
1-- |
2-- Module : Streamly.Benchmark.FileIO.Array
3-- Copyright : (c) 2019 Composewell Technologies
4--
5-- License : BSD3
6-- Maintainer : harendra.kumar@gmail.com
7-- Stability : experimental
8-- Portability : GHC
10{-# LANGUAGE CPP #-}
12#ifdef __HADDOCK_VERSION__
13#undef INSPECTION
14#endif
16#ifdef INSPECTION
17{-# LANGUAGE TemplateHaskell #-}
18{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
19#endif
21module Streamly.Benchmark.FileIO.Array
22 (
23 last
24 , countBytes
25 , countLines
26 , sumBytes
27 , cat
28 , copy
29 , linesUnlinesCopy
30 )
31where
33import Data.Functor.Identity (runIdentity)
34import Data.Word (Word8)
35import System.IO (Handle)
36import Prelude hiding (last)
38import qualified Streamly.FileSystem.Handle as FH
39import qualified Streamly.Memory.Array as A
40import qualified Streamly.Prelude as S
41import qualified Streamly.Internal as Internal
43#ifdef INSPECTION
44import Streamly.Streams.StreamD.Type (Step(..))
45import Test.Inspection
46#endif
48-- | Get the last byte from a file bytestream.
49{-# INLINE last #-}
50last :: Handle -> IO (Maybe Word8)
51last inh = do
52 let s = FH.readArrays inh
53 larr <- S.last s
54 return $ case larr of
55 Nothing -> Nothing
56 Just arr -> A.readIndex arr (A.length arr - 1)
58#ifdef INSPECTION
59inspect $ hasNoTypeClasses 'last
60inspect $ 'last `hasNoType` ''Step
61#endif
63-- | Count the number of bytes in a file.
64{-# INLINE countBytes #-}
65countBytes :: Handle -> IO Int
66countBytes inh =
67 let s = FH.readArrays inh
68 in S.sum (S.map A.length s)
70#ifdef INSPECTION
71inspect $ hasNoTypeClasses 'countBytes
72inspect $ 'countBytes `hasNoType` ''Step
73#endif
75-- | Count the number of lines in a file.
76{-# INLINE countLines #-}
77countLines :: Handle -> IO Int
78countLines = S.length . A.splitOn 10 . FH.readArrays
80#ifdef INSPECTION
81inspect $ hasNoTypeClasses 'countLines
82inspect $ 'countLines `hasNoType` ''Step
83#endif
85-- | Sum the bytes in a file.
86{-# INLINE sumBytes #-}
87sumBytes :: Handle -> IO Word8
88sumBytes inh = do
89 let foldlArr' f z = runIdentity . S.foldl' f z . A.read
90 let s = FH.readArrays inh
91 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
93#ifdef INSPECTION
94inspect $ hasNoTypeClasses 'sumBytes
95inspect $ 'sumBytes `hasNoType` ''Step
96#endif
98-- | Send the file contents to /dev/null
99{-# INLINE cat #-}
100cat :: Handle -> Handle -> IO ()
101cat devNull inh =
102 S.runFold (FH.writeArrays devNull) $ FH.readArraysOf (256*1024) inh
104#ifdef INSPECTION
105inspect $ hasNoTypeClasses 'cat
106inspect $ 'cat `hasNoType` ''Step
107#endif
109-- | Copy file
110{-# INLINE copy #-}
111copy :: Handle -> Handle -> IO ()
112copy inh outh =
113 let s = FH.readArrays inh
114 in S.runFold (FH.writeArrays outh) s
116#ifdef INSPECTION
117inspect $ hasNoTypeClasses 'copy
118inspect $ 'copy `hasNoType` ''Step
119#endif
121-- | Lines and unlines
122{-# INLINE linesUnlinesCopy #-}
123linesUnlinesCopy :: Handle -> Handle -> IO ()
124linesUnlinesCopy inh outh =
125 S.runFold (FH.writeArraysInChunksOf (1024*1024) outh)
126 $ Internal.insertAfterEach (return $ A.fromList [10])
127 $ A.splitOn 10
128 $ FH.readArraysOf (1024*1024) inh
130#ifdef INSPECTION
131-- inspect $ hasNoTypeClasses 'linesUnlinesCopy
132-- inspect $ 'linesUnlinesCopy `hasNoType` ''Step
133#endif