-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
207 lines (191 loc) · 6.33 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE NoImplicitPrelude #-}
module Main (main) where
import Control.Category ((.))
import Control.DeepSeq (force)
import Control.Exception (evaluate)
import Data.Bits (unsafeShiftL)
import Data.ByteString (ByteString, cons, replicate, snoc)
import Data.Foldable (foldl')
import Data.Function (($))
import Data.Functor (fmap)
import Data.Int (Int)
import Data.Semigroup ((<>))
import Data.String (String)
import GHC.ByteOrder (ByteOrder (BigEndian, LittleEndian))
import GHC.Num (Integer, (*), (-))
import Logical.Naive qualified as Naive
import Logical.Optimized qualified as Optimized
import Naive (fromByteString, toByteString)
import System.IO (IO)
import Test.Tasty.Bench
( Benchmark,
bench,
bgroup,
defaultMain,
env,
nf,
)
import Text.Show (show)
import Prelude (Bool (False, True), fromIntegral)
main :: IO ()
main =
defaultMain
[ bgroup
"toByteString"
[ bgroup
"no padding"
[ bgroup "little endian" . fmap (mkNoPaddingTBS LittleEndian) $ sizes,
bgroup "big endian" . fmap (mkNoPaddingTBS BigEndian) $ sizes
],
bgroup
"with padding"
[ bgroup "little endian" . fmap (mkPaddingTBS LittleEndian) $ sizes,
bgroup "big endian" . fmap (mkPaddingTBS BigEndian) $ sizes
]
],
bgroup
"fromByteString"
[ bgroup
"no padding"
[ bgroup "little endian" . fmap (mkNoPaddingFBS LittleEndian) $ sizes,
bgroup "big endian" . fmap (mkNoPaddingFBS BigEndian) $ sizes
],
bgroup
"with padding"
[ bgroup "little endian" . fmap (mkPaddingFBS LittleEndian) $ sizes,
bgroup "big endian" . fmap (mkPaddingFBS BigEndian) $ sizes
]
],
bgroup
"complement"
[ bgroup "naive" . fmap mkComplementNaive $ sizes,
bgroup "optimized" . fmap mkComplementOptimized $ sizes
],
bgroup
"and"
[ bgroup "naive (symmetric)" . fmap mkAndSymmetricNaive $ sizes,
bgroup "optimized (symmetric)" . fmap mkAndSymmetricOptimized $ sizes
],
bgroup
"setBit"
[ bgroup "naive" . fmap mkSetBitNaive $ sizes,
bgroup "optimized" . fmap mkSetBitOptimized $ sizes
],
bgroup
"setBits"
[ bgroup "naive" . fmap mkSetBitsNaive $ [1 .. 32],
bgroup "optimized" . fmap mkSetBitsOptimized $ [1 .. 32]
]
]
mkSetBitsNaive :: Int -> Benchmark
mkSetBitsNaive toSet =
env (evaluate . force $ mkData) $ \dat ->
bench showToSet . nf (foldl' (\bs (i, b) -> Optimized.setBit bs i b) (replicate 32 0x00)) $ dat
where
showToSet :: String
showToSet = show toSet <> " bits to set"
mkData :: [(Integer, Bool)]
mkData = fmap (,True) [0 .. fromIntegral (toSet - 1)]
mkSetBitsOptimized :: Int -> Benchmark
mkSetBitsOptimized toSet =
env (evaluate . force $ mkData) $ \dat ->
bench showToSet . nf (Optimized.setBits (replicate 32 0x00)) $ dat
where
showToSet :: String
showToSet = show toSet <> " bits to set"
mkData :: [(Integer, Bool)]
mkData = fmap (,True) [0 .. fromIntegral (toSet - 1)]
mkSetBitNaive :: Int -> Benchmark
mkSetBitNaive len =
env (evaluate . force $ mkData) $ \dat ->
bench showBytes . nf (Naive.setBit dat 0) $ True
where
showBytes :: String
showBytes = show len <> " bytes"
mkData :: ByteString
mkData = replicate len 0x00
mkSetBitOptimized :: Int -> Benchmark
mkSetBitOptimized len =
env (evaluate . force $ mkData) $ \dat ->
bench showBytes . nf (Optimized.setBit dat 0) $ True
where
showBytes :: String
showBytes = show len <> " bytes"
mkData :: ByteString
mkData = replicate len 0x00
mkAndSymmetricNaive :: Int -> Benchmark
mkAndSymmetricNaive len =
env (evaluate . force $ mkData) $ \dat ->
bench showBytes . nf (Naive.and False dat) $ dat
where
showBytes :: String
showBytes = show (len - 1) <> " bytes"
mkData :: ByteString
mkData = replicate (len - 1) 0x00
mkAndSymmetricOptimized :: Int -> Benchmark
mkAndSymmetricOptimized len =
env (evaluate . force $ mkData) $ \dat ->
bench showBytes . nf (Optimized.and False dat) $ dat
where
showBytes :: String
showBytes = show (len - 1) <> " bytes"
mkData :: ByteString
mkData = replicate (len - 1) 0x00
mkComplementNaive :: Int -> Benchmark
mkComplementNaive len =
env (evaluate . force $ mkData) $ \dat ->
bench showBytes $ nf Naive.complement dat
where
showBytes :: String
showBytes = show (len - 1) <> " bytes"
mkData :: ByteString
mkData = replicate (len - 1) 0x00
mkComplementOptimized :: Int -> Benchmark
mkComplementOptimized len =
env (evaluate . force $ mkData) $ \dat ->
bench showBytes $ nf Optimized.complement dat
where
showBytes :: String
showBytes = show (len - 1) <> " bytes"
mkData :: ByteString
mkData = replicate (len - 1) 0x00
mkNoPaddingTBS :: ByteOrder -> Int -> Benchmark
mkNoPaddingTBS bo bytes =
env (evaluate . force $ mkData) $ \dat ->
bench showBytes $ nf (toByteString 0 bo) dat
where
showBytes :: String
showBytes = show bytes <> " bytes"
mkData :: Integer
mkData = (1 `unsafeShiftL` (8 * bytes)) - 1
mkPaddingTBS :: ByteOrder -> Int -> Benchmark
mkPaddingTBS bo bytes =
env (evaluate . force $ 127) $ \dat ->
bench showBytes $ nf (toByteString bytes bo) dat
where
showBytes :: String
showBytes = show bytes <> " bytes (" <> show (bytes - 1) <> " padding)"
mkNoPaddingFBS :: ByteOrder -> Int -> Benchmark
mkNoPaddingFBS bo bytes =
env (evaluate . force $ mkData) $ \dat ->
bench showBytes $ nf (fromByteString bo) dat
where
showBytes :: String
showBytes = show bytes <> " bytes"
mkData :: ByteString
mkData = replicate bytes 0x8F
mkPaddingFBS :: ByteOrder -> Int -> Benchmark
mkPaddingFBS bo bytes =
env (evaluate . force $ mkData) $ \dat ->
bench showBytes $ nf (fromByteString bo) dat
where
showBytes :: String
showBytes = show bytes <> " bytes (" <> show (bytes - 1) <> " padding)"
mkData :: ByteString
mkData = case bo of
LittleEndian -> cons 0x8F (replicate (bytes - 1) 0x0)
BigEndian -> snoc (replicate (bytes - 1) 0x0) 0x8F
sizes :: [Int]
sizes = [1, 2 .. 8] <> [16, 32, 64, 128, 256, 512, 1024, 2048, 4096]