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
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
44import Streamly.Streams.StreamD.Type (Step(..))
48-- | Get the last byte from a file bytestream.
50last :: Handle -> IO (Maybe Word8)
52 let s = FH.readArrays inh
56 Just arr -> A.readIndex arr (A.length arr - 1)
59inspect $ hasNoTypeClasses 'last
60inspect $ 'last `hasNoType` ''Step
63-- | Count the number of bytes in a file.
64{-# INLINE countBytes #-}
65countBytes :: Handle -> IO Int
67 let s = FH.readArrays inh
68 in S.sum (S.map A.length s)
71inspect $ hasNoTypeClasses 'countBytes
72inspect $ 'countBytes `hasNoType` ''Step
75-- | Count the number of lines in a file.
76{-# INLINE countLines #-}
77countLines :: Handle -> IO Int
78countLines = S.length . A.splitOn 10 . FH.readArrays
81inspect $ hasNoTypeClasses 'countLines
82inspect $ 'countLines `hasNoType` ''Step
85-- | Sum the bytes in a file.
86{-# INLINE sumBytes #-}
87sumBytes :: Handle -> IO Word8
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
94inspect $ hasNoTypeClasses 'sumBytes
95inspect $ 'sumBytes `hasNoType` ''Step
98-- | Send the file contents to /dev/null
100cat :: Handle -> Handle -> IO ()
102 S.runFold (FH.writeArrays devNull) $ FH.readArraysOf (256*1024) inh
105inspect $ hasNoTypeClasses 'cat
106inspect $ 'cat `hasNoType` ''Step
111copy :: Handle -> Handle -> IO ()
113 let s = FH.readArrays inh
114 in S.runFold (FH.writeArrays outh) s
117inspect $ hasNoTypeClasses 'copy
118inspect $ 'copy `hasNoType` ''Step
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])
128 $ FH.readArraysOf (1024*1024) inh
131-- inspect $ hasNoTypeClasses 'linesUnlinesCopy
132-- inspect $ 'linesUnlinesCopy `hasNoType` ''Step
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