2-- Module : Streamly.Benchmark.FileSystem.Handle
3-- Copyright : (c) 2019 Composewell Technologies
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
11{-# LANGUAGE ScopedTypeVariables #-}
13#ifdef __HADDOCK_VERSION__
18{-# LANGUAGE TemplateHaskell #-}
19{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
36import Control.DeepSeq (NFData(rnf))
37import Data.Char (ord, chr)
38import Data.Word (Word8)
39import System.IO (openFile, IOMode(..), Handle, hClose)
42import Gauge hiding (env)
45scratchDir = "benchmark-tmp"
48outfile = scratchDir ++ "out.txt"
51inFileSmall = scratchDir ++ "in-10MB.txt"
54inFileBig = scratchDir ++ "in-100MB.txt"
56data RefHandles = RefHandles
62data Handles = Handles !Handle !Handle
64instance NFData Handles where
67data BenchEnv = BenchEnv
68 { href :: IORef RefHandles
74withScaling :: BenchEnv -> String -> String
76 let factor = round (fromIntegral (bigSize env)
77 / (fromIntegral (smallSize env) :: Double)) :: Int
80 else str ++ " (1/" ++ show factor ++ ")"
84 => (RefHandles -> Handles)
87 -> (Handle -> Handle -> IO b)
89mkBenchCommon mkHandles name env action =
90 bench name $ perRunEnv (do
91 r <- readIORef $ href env
99 smallInHandle <- openFile inFileSmall ReadMode
100 bigInHandle <- openFile inFileBig ReadMode
101 outHandle <- openFile outfile WriteMode
103 let refHandles = RefHandles
104 { smallInH = smallInHandle
105 , bigInH = bigInHandle
106 , outputH = outHandle
110 writeIORef (href env) $ refHandles
111 return $ mkHandles refHandles
113 (\(Handles h1 h2) -> action h1 h2)
116 NFData b => String -> BenchEnv -> (Handle -> Handle -> IO b) -> Benchmark
117mkBench name env action = mkBenchCommon useBigH name env action
121 useBigH (RefHandles {bigInH = inh, outputH = outh}) = Handles inh outh
124 NFData b => String -> BenchEnv -> (Handle -> Handle -> IO b) -> Benchmark
125mkBenchSmall name env action =
126 mkBenchCommon useSmallH (withScaling env name) env action
130 useSmallH (RefHandles {smallInH = inh, outputH = outh}) = Handles inh outh
132foreign import ccall unsafe "u_iswspace"
133 iswspace :: Int -> Int
135-- Code copied from base/Data.Char to INLINE it
136{-# INLINE isSpace #-}
137isSpace :: Char -> Bool
138-- isSpace includes non-breaking space
139-- The magic 0x377 isn't really that magical. As of 2014, all the codepoints
140-- at or below 0x377 have been assigned, so we shouldn't have to worry about
141-- any new spaces appearing below there. It would probably be best to
142-- use branchless ||, but currently the eqLit transformation will undo that,
143-- so we'll do it like this until there's a way around that.
145 | uc <= 0x377 = uc == 32 || uc - 0x9 <= 4 || uc == 0xa0
146 | otherwise = iswspace (ord c) /= 0
148 uc = fromIntegral (ord c) :: Word
152isSp = isSpace . chr . fromIntegral
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