on master help sign in
Add benchmarks for Unbox instance derivation
commit1d6b85f5423e0eeaca8afa4db9cacd74e309aa01 authorAdithya Kumar <adithya@composewell.com> dateMon 10 Jul 2023, 13:49 (3 years ago) parent4f61eb4
2 files changed+108−1
benchmark/Streamly/Benchmark/Data/Unbox.hs+102 benchmark/streamly-benchmarks.cabal+6−1
benchmark/Streamly/Benchmark/Data/Unbox.hsnew file+102
@@ -0,0 +1,102 @@
1+{-# LANGUAGE TemplateHaskell #-}
2+
3+-- |
4+-- Module : Streamly.Benchmark.Data.Unbox
5+-- Copyright : (c) 2023 Composewell
6+-- License : BSD-3-Clause
7+-- Maintainer : streamly@composewell.com
8+
9+module Main (main) where
10+
11+-------------------------------------------------------------------------------
12+-- Imports
13+-------------------------------------------------------------------------------
14+
15+import Control.DeepSeq (NFData(..))
16+import Control.Exception (assert)
17+import Control.Monad (replicateM_)
18+import Data.Proxy (Proxy(..))
19+import GHC.Generics (Generic)
20+import System.Random (randomRIO)
21+
22+import Streamly.Internal.Data.Unbox
23+import Streamly.Internal.Data.Unbox.TH
24+
25+import Gauge
26+import Streamly.Benchmark.Common
27+
28+-------------------------------------------------------------------------------
29+-- Types
30+-------------------------------------------------------------------------------
31+
32+data CustomDT1 a b
33+ = CDT1C1
34+ | CDT1C2 a
35+ | CDT1C3 a b
36+ deriving (Generic, Show, Eq)
37+type CustomDT1_ = CustomDT1 Int Bool
38+
39+instance (Unbox a, Unbox b) => Unbox (CustomDT1 a b)
40+
41+data CustomDT2 a b
42+ = CDT2C1
43+ | CDT2C2 a
44+ | CDT2C3 a b
45+ deriving (Show, Eq)
46+type CustomDT2_ = CustomDT2 Int Bool
47+
48+$(deriveUnbox ''CustomDT2)
49+
50+-------------------------------------------------------------------------------
51+-- Helpers
52+-------------------------------------------------------------------------------
53+
54+serializeDeserializeTimes :: forall a. (Eq a, Unbox a) => a -> Int -> IO ()
55+serializeDeserializeTimes val times = do
56+ arr <- newBytes (sizeOf (Proxy :: Proxy a))
57+ replicateM_ times $ do
58+ pokeByteIndex 0 arr val
59+ val1 <- peekByteIndex 0 arr
60+ assert (val == val1) (pure ())
61+
62+benchSink :: NFData b => String -> Int -> (Int -> IO b) -> Benchmark
63+benchSink name times f = bench name (nfIO (randomRIO (times, times) >>= f))
64+
65+-------------------------------------------------------------------------------
66+-- Benchmarks
67+-------------------------------------------------------------------------------
68+
69+allBenchmarks :: Int -> [Benchmark]
70+allBenchmarks times =
71+ [ benchSink
72+ "serializeDeserializeTimes CDT1C1 (Generic)"
73+ times
74+ (serializeDeserializeTimes (CDT1C1 :: CustomDT1_))
75+ , benchSink
76+ "serializeDeserializeTimes CDT2C1 (TH)"
77+ times
78+ (serializeDeserializeTimes (CDT2C1 :: CustomDT2_))
79+ , benchSink
80+ "serializeDeserializeTimes CDT1C2 (Generic)"
81+ times
82+ (serializeDeserializeTimes ((CDT1C2 (5 :: Int)) :: CustomDT1_))
83+ , benchSink
84+ "serializeDeserializeTimes CDT2C2 (TH)"
85+ times
86+ (serializeDeserializeTimes ((CDT2C2 (5 :: Int)) :: CustomDT2_))
87+ , benchSink
88+ "serializeDeserializeTimes CDT1C3 (Generic)"
89+ times
90+ (serializeDeserializeTimes ((CDT1C3 (5 :: Int) True) :: CustomDT1_))
91+ , benchSink
92+ "serializeDeserializeTimes CDT2C3 (TH)"
93+ times
94+ (serializeDeserializeTimes ((CDT2C3 (5 :: Int) True) :: CustomDT2_))
95+ ]
96+
97+-------------------------------------------------------------------------------
98+-- Driver
99+-------------------------------------------------------------------------------
100+
101+main :: IO ()
102+main = runWithCLIOpts defaultStreamSize allBenchmarks
benchmark/streamly-benchmarks.cabal+6−1
@@ -209,7 +209,7 @@ common bench-depends
209209 build-depends:
210210 fusion-plugin >= 0.2 && < 0.3
211211 if flag(inspection)
212− build-depends: template-haskell >= 2.14 && < 2.20
212+ build-depends: template-haskell >= 2.14 && < 2.21
213213 , inspection-testing >= 0.4 && < 0.5
214214 -- Array uses a Storable constraint in dev build making several inspection
215215 -- tests fail
@@ -356,6 +356,11 @@ benchmark Data.Stream.ConcurrentOrdered
356356 else
357357 buildable: True
358358
359+benchmark Data.Unbox
360+ import: bench-options
361+ type: exitcode-stdio-1.0
362+ hs-source-dirs: .
363+ main-is: Streamly/Benchmark/Data/Unbox.hs
359364
360365benchmark Data.Unfold
361366 import: bench-options