on 43dda21 help sign in
94 lines 3.0 kB history raw
1-- |
2-- Module : Streamly.Benchmark.Data.Array.Foreign.Mut
3-- Copyright : (c) 2021 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
7{-# LANGUAGE CPP #-}
8{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE ScopedTypeVariables #-}
11#ifdef __HADDOCK_VERSION__
12#undef INSPECTION
13#endif
15#ifdef INSPECTION
16{-# LANGUAGE TemplateHaskell #-}
17{-# OPTIONS_GHC -fplugin Test.Inspection.Plugin #-}
18#endif
20module Main (main) where
22import Control.DeepSeq (NFData(..))
23import Streamly.Prelude (MonadAsync, SerialT, IsStream)
24import System.Random (randomRIO)
26import qualified Streamly.Prelude as Stream
27import qualified Streamly.Internal.Data.Array.Foreign as Array
28import qualified Streamly.Internal.Data.Array.Foreign.Mut as MArray
30import Gauge hiding (env)
31import Streamly.Benchmark.Common
33-------------------------------------------------------------------------------
34-- Utilities
35-------------------------------------------------------------------------------
37{-# INLINE sourceUnfoldrM #-}
38sourceUnfoldrM :: (IsStream t, MonadAsync m) => Int -> Int -> t m Int
39sourceUnfoldrM value n = Stream.unfoldrM step n
40 where
41 step cnt =
42 if cnt > n + value
43 then return Nothing
44 else return (Just (cnt, cnt + 1))
46{-# INLINE benchIO #-}
47benchIO
48 :: NFData b
49 => String -> (Int -> a) -> (a -> IO b) -> Benchmark
50benchIO name src sink =
51 bench name $ nfIO $ randomRIO (1,1) >>= sink . src
53o_1_space_serial_marray ::
54 Int -> (MArray.Array Int, Array.Array Int) -> [Benchmark]
55o_1_space_serial_marray value ~(array, indices) =
56 [ benchIO "partitionBy (< 0)" (const array)
57 $ MArray.partitionBy (< 0)
58 , benchIO "partitionBy (> 0)" (const array)
59 $ MArray.partitionBy (> 0)
60 , benchIO "partitionBy (< value/2)" (const array)
61 $ MArray.partitionBy (< (value `div` 2))
62 , benchIO "partitionBy (> value/2)" (const array)
63 $ MArray.partitionBy (> (value `div` 2))
64 , benchIO "strip (< value/2 || > value/2)" (const array)
65 $ MArray.strip (\x -> x < value `div` 2 || x > value `div` 2)
66 , benchIO "strip (> 0)" (const array)
67 $ MArray.strip (> 0)
68 , benchIO "modifyIndices (+ 1)" (const indices)
69 $ Stream.fold (MArray.modifyIndices (\_idx val -> val + 1) array)
70 . Stream.unfold Array.read
71 ]
73-------------------------------------------------------------------------------
74-- Driver
75-------------------------------------------------------------------------------
77moduleName :: String
78moduleName = "Data.Array.Foreign.Mut"
80main :: IO ()
81main = do
82 runWithCLIOptsEnv defaultStreamSize alloc allBenchmarks
84 where
86 alloc value = do
87 marr <- MArray.fromStream (sourceUnfoldrM value 0 :: SerialT IO Int)
88 indices <- Array.fromStream (sourceUnfoldrM value 0 :: SerialT IO Int)
89 return (marr, indices)
91 allBenchmarks array value =
92 [ bgroup (o_1_space_prefix moduleName) $
93 o_1_space_serial_marray value array
94 ]