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
47import qualified Streamly.Internal.FileSystem.Handle as IFH
48import qualified Streamly.Internal.Prelude as Internal
49import qualified Streamly.Internal.Memory.Array as IA
50import qualified Streamly.Internal.Memory.ArrayStream as AS
53import Foreign.Storable (Storable)
54import Streamly.Streams.StreamD.Type (Step(..))
58-- | Get the last byte from a file bytestream.
60last :: Handle -> IO (Maybe Word8)
62 let s = IFH.toStreamArrays inh
66 Just arr -> A.readIndex arr (A.length arr - 1)
69inspect $ hasNoTypeClasses 'last
70inspect $ 'last `hasNoType` ''Step
73-- | Count the number of bytes in a file.
74{-# INLINE countBytes #-}
75countBytes :: Handle -> IO Int
77 let s = IFH.toStreamArrays inh
78 in S.sum (S.map A.length s)
81inspect $ hasNoTypeClasses 'countBytes
82inspect $ 'countBytes `hasNoType` ''Step
85-- | Count the number of lines in a file.
86{-# INLINE countLines #-}
87countLines :: Handle -> IO Int
88countLines = S.length . AS.splitOnSuffix 10 . IFH.toStreamArrays
91inspect $ hasNoTypeClasses 'countLines
92inspect $ 'countLines `hasNoType` ''Step
95-- XXX use a word splitting combinator instead of splitOn and test it.
96-- | Count the number of lines in a file.
97{-# INLINE countWords #-}
98countWords :: Handle -> IO Int
99countWords = S.length . AS.splitOn 32 . IFH.toStreamArrays
102inspect $ hasNoTypeClasses 'countWords
103inspect $ 'countWords `hasNoType` ''Step
106-- | Sum the bytes in a file.
107{-# INLINE sumBytes #-}
108sumBytes :: Handle -> IO Word8
110 let foldlArr' f z = runIdentity . S.foldl' f z . IA.toStream
111 let s = IFH.toStreamArrays inh
112 S.foldl' (\acc arr -> acc + foldlArr' (+) 0 arr) 0 s
115inspect $ hasNoTypeClasses 'sumBytes
116inspect $ 'sumBytes `hasNoType` ''Step
119-- | Send the file contents to /dev/null
121cat :: Handle -> Handle -> IO ()
123 S.fold (IFH.writeArrays devNull) $ IFH.toStreamArraysOf (256*1024) inh
126inspect $ hasNoTypeClasses 'cat
127inspect $ 'cat `hasNoType` ''Step
132copy :: Handle -> Handle -> IO ()
134 let s = IFH.toStreamArrays inh
135 in S.fold (IFH.writeArrays outh) s
138inspect $ hasNoTypeClasses 'copy
139inspect $ 'copy `hasNoType` ''Step
142-- | Lines and unlines
143{-# INLINE linesUnlinesCopy #-}
144linesUnlinesCopy :: Handle -> Handle -> IO ()
145linesUnlinesCopy inh outh =
146 S.fold (IFH.writeArraysInChunksOf (1024*1024) outh)
147 $ Internal.insertAfterEach (return $ A.fromList [10])
148 $ AS.splitOnSuffix 10
149 $ IFH.toStreamArraysOf (1024*1024) inh
152inspect $ hasNoTypeClassesExcept 'linesUnlinesCopy [''Storable]
153-- inspect $ 'linesUnlinesCopy `hasNoType` ''Step
156-- | Words and unwords
157{-# INLINE wordsUnwordsCopy #-}
158wordsUnwordsCopy :: Handle -> Handle -> IO ()
159wordsUnwordsCopy inh outh =
160 S.fold (IFH.writeArraysInChunksOf (1024*1024) outh)
161 $ S.intersperse (A.fromList [32])
162 -- XXX use a word splitting combinator
164 $ IFH.toStreamArraysOf (1024*1024) inh
167inspect $ hasNoTypeClassesExcept 'wordsUnwordsCopy [''Storable]
168-- inspect $ 'wordsUnwordsCopy `hasNoType` ''Step
171{-# INLINE decodeUtf8Lenient #-}
172decodeUtf8Lenient :: Handle -> IO ()
173decodeUtf8Lenient inh =
175 $ SS.decodeUtf8ArraysLenient
176 $ IFH.toStreamArraysOf (1024*1024) inh
179inspect $ hasNoTypeClasses 'decodeUtf8Lenient
180-- inspect $ 'decodeUtf8Lenient `hasNoType` ''Step
181-- inspect $ 'decodeUtf8Lenient `hasNoType` ''AT.FlattenState
182-- inspect $ 'decodeUtf8Lenient `hasNoType` ''D.ConcatMapUState
186{-# INLINE copyCodecUtf8Lenient #-}
187copyCodecUtf8Lenient :: Handle -> Handle -> IO ()
188copyCodecUtf8Lenient inh outh =
189 S.fold (FH.write outh)
191 $ SS.decodeUtf8ArraysLenient
192 $ IFH.toStreamArraysOf (1024*1024) inh
195inspect $ hasNoTypeClasses 'copyCodecUtf8Lenient
196-- inspect $ 'copyCodecUtf8Lenient `hasNoType` ''Step
197-- inspect $ 'copyCodecUtf8Lenient `hasNoType` ''AT.FlattenState
198-- 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