2-- Module : Streamly.Benchmark.FileIO.Array
3-- Copyright : (c) 2019 Composewell Technologies
6-- Maintainer : harendra.kumar@gmail.com
7-- Stability : experimental
12#ifdef __HADDOCK_VERSION__
17{-# LANGUAGE TemplateHaskell #-}
18{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
21module Streamly.Benchmark.FileIO.Array
33 , copyCodecUtf8Lenient
37import Data.Functor.Identity (runIdentity)
38import Data.Word (Word8)
39import System.IO (Handle)
40import Prelude hiding (last)
42import qualified Streamly.FileSystem.Handle as FH
43import qualified Streamly.Memory.Array as A
44import qualified Streamly.Prelude as S
45import qualified Streamly.Data.String as SS
46import qualified Streamly.Internal.Prelude as Internal
49import Foreign.Storable (Storable)
50import Streamly.Streams.StreamD.Type (Step(..))
54-- | Get the last byte from a file bytestream.
56last :: Handle -> IO (Maybe Word8)
58 let s = FH.readArrays inh
62 Just arr -> A.readIndex arr (A.length arr - 1)
65inspect $ hasNoTypeClasses 'last
66inspect $ 'last `hasNoType` ''Step
69-- | Count the number of bytes in a file.
70{-# INLINE countBytes #-}
71countBytes :: Handle -> IO Int
73 let s = FH.readArrays inh
74 in S.sum (S.map A.length s)
77inspect $ hasNoTypeClasses 'countBytes
78inspect $ 'countBytes `hasNoType` ''Step
81-- | Count the number of lines in a file.
82{-# INLINE countLines #-}
83countLines :: Handle -> IO Int
84countLines = S.length . A.splitOnSuffix 10 . FH.readArrays
87inspect $ hasNoTypeClasses 'countLines
88inspect $ 'countLines `hasNoType` ''Step
91-- XXX use a word splitting combinator instead of splitOn and test it.
92-- | Count the number of lines in a file.
93{-# INLINE countWords #-}
94countWords :: Handle -> IO Int
95countWords = S.length . A.splitOn 32 . FH.readArrays
98inspect $ hasNoTypeClasses 'countWords
99inspect $ 'countWords `hasNoType` ''Step
102-- | Sum the bytes in a file.
103{-# INLINE sumBytes #-}
104sumBytes :: Handle -> IO Word8
106 let foldlArr' f z = runIdentity . S.foldl' f z . A.read
107 let s = FH.readArrays inh
108 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
111inspect $ hasNoTypeClasses 'sumBytes
112inspect $ 'sumBytes `hasNoType` ''Step
115-- | Send the file contents to /dev/null
117cat :: Handle -> Handle -> IO ()
119 S.runFold (FH.writeArrays devNull) $ FH.readArraysOf (256*1024) inh
122inspect $ hasNoTypeClasses 'cat
123inspect $ 'cat `hasNoType` ''Step
128copy :: Handle -> Handle -> IO ()
130 let s = FH.readArrays inh
131 in S.runFold (FH.writeArrays outh) s
134inspect $ hasNoTypeClasses 'copy
135inspect $ 'copy `hasNoType` ''Step
138-- | Lines and unlines
139{-# INLINE linesUnlinesCopy #-}
140linesUnlinesCopy :: Handle -> Handle -> IO ()
141linesUnlinesCopy inh outh =
142 S.runFold (FH.writeArraysInChunksOf (1024*1024) outh)
143 $ Internal.insertAfterEach (return $ A.fromList [10])
145 $ FH.readArraysOf (1024*1024) inh
148inspect $ hasNoTypeClassesExcept 'linesUnlinesCopy [''Storable]
149-- inspect $ 'linesUnlinesCopy `hasNoType` ''Step
152-- | Words and unwords
153{-# INLINE wordsUnwordsCopy #-}
154wordsUnwordsCopy :: Handle -> Handle -> IO ()
155wordsUnwordsCopy inh outh =
156 S.runFold (FH.writeArraysInChunksOf (1024*1024) outh)
157 $ S.intersperse (A.fromList [32])
158 -- XXX use a word splitting combinator
160 $ FH.readArraysOf (1024*1024) inh
163inspect $ hasNoTypeClassesExcept 'wordsUnwordsCopy [''Storable]
164-- inspect $ 'wordsUnwordsCopy `hasNoType` ''Step
167{-# INLINE decodeUtf8Lenient #-}
168decodeUtf8Lenient :: Handle -> IO ()
169decodeUtf8Lenient inh =
171 $ SS.decodeUtf8ArraysLenient
172 $ FH.readArraysOf (1024*1024) inh
175inspect $ hasNoTypeClasses 'decodeUtf8Lenient
176-- inspect $ 'decodeUtf8Lenient `hasNoType` ''Step
177-- inspect $ 'decodeUtf8Lenient `hasNoType` ''AT.FlattenState
178-- inspect $ 'decodeUtf8Lenient `hasNoType` ''D.ConcatMapUState
182{-# INLINE copyCodecUtf8Lenient #-}
183copyCodecUtf8Lenient :: Handle -> Handle -> IO ()
184copyCodecUtf8Lenient inh outh =
185 S.runFold (FH.write outh)
187 $ SS.decodeUtf8ArraysLenient
188 $ FH.readArraysOf (1024*1024) inh
191inspect $ hasNoTypeClasses 'copyCodecUtf8Lenient
192-- inspect $ 'copyCodecUtf8Lenient `hasNoType` ''Step
193-- inspect $ 'copyCodecUtf8Lenient `hasNoType` ''AT.FlattenState
194-- inspect $ 'copyCodecUtf8Lenient `hasNoType` ''D.ConcatMapUState
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list