Skip to content

Commit 50c77fd

Browse files
committed
Setup benchmarks for fromList with Bytes and Int keys
1 parent 8bcbf8a commit 50c77fd

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

benchmarks/FineGrained.hs

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,96 @@
1+
{-# LANGUAGE DeriveAnyClass #-}
2+
{-# LANGUAGE DeriveGeneric #-}
3+
{-# LANGUAGE ExplicitForAll #-}
4+
{-# LANGUAGE NumericUnderscores #-}
5+
{-# LANGUAGE TupleSections #-}
6+
{-# LANGUAGE TypeApplications #-}
7+
18
module Main where
29

10+
import Control.Monad (replicateM)
11+
import qualified Data.HashMap.Internal as HMI
12+
import qualified Data.HashMap.Strict as HM
13+
import Data.Hashable (Hashable, hash)
14+
import Key.Bytes
15+
import System.Random.Stateful
316
import Test.Tasty.Bench
17+
import Prelude hiding (Foldable (..), lookup)
418

519
main :: IO ()
6-
main = defaultMain []
20+
main =
21+
defaultMain
22+
[ bFromList
23+
-- bgroup "insert" bInsert
24+
-- union
25+
]
26+
27+
defaultGen :: StdGen
28+
defaultGen = mkStdGen 42
29+
30+
defaultSizes :: [Int]
31+
defaultSizes = [0, 1, 10, 100, 1000, 10_000, 100_000]
32+
33+
{-
34+
bInsert = [ env m $ \d -> bench (show s) $ whnf (\(k, v, m) -> HM.insert k v m) d ]
35+
where m s = do
36+
g <- newIOGenM defaultGen
37+
let hm = HM.empty
38+
forM_ [1..s] $ \v -> do
39+
b <- genBytes 32 g
40+
HMI.unsafeInsert b v hm
41+
return (m, newKeys) -- separate existing, new
42+
-}
43+
44+
{-
45+
matrix :: (NFData env) => [Int] -> (Int -> IO env) -> (env -> Benchmarkable) -> Benchmark
46+
matrix sizes e x = b -- [ b @Bytes, b @Int] -- , b @SlowInt, b @Colli ]
47+
where
48+
b = bgroup "bla" [runTemplate @Int e x s | s <- sizes]
49+
50+
runTemplate :: forall env. (NFData env) => (Int -> IO env) -> (env -> Benchmarkable) -> Int -> Benchmark
51+
runTemplate e b s = env (e s) $ \x -> bench (show s) (b x)
52+
-}
53+
54+
bFromList :: Benchmark
55+
bFromList =
56+
bgroup
57+
"fromList"
58+
[ bgroup
59+
"Bytes"
60+
[ env
61+
( do
62+
g <- newIOGenM defaultGen
63+
genNBytes s 32 g
64+
)
65+
$ \keys ->
66+
bench (show s) $ whnf (HM.fromList . map (, ())) keys
67+
| s <- sizes
68+
],
69+
bgroup
70+
"Int"
71+
[ env
72+
( do
73+
g <- newIOGenM defaultGen
74+
genInts s g
75+
)
76+
$ \keys -> bench (show s) $ whnf (HM.fromList . map (,())) keys
77+
| s <- sizes
78+
]
79+
]
80+
where
81+
sizes = defaultSizes
82+
83+
genInts ::
84+
(StatefulGen g m) =>
85+
Int ->
86+
g ->
87+
m [Int]
88+
genInts n = do
89+
replicateM n . uniformM
90+
91+
{-
92+
bFromList = matrix defaultSizes e' b'
93+
where
94+
e' s = uniformListM s defaultGen
95+
b' = whnf HM.fromList
96+
-}

benchmarks/Key/Bytes.hs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2+
3+
module Key.Bytes where
4+
5+
import Control.Monad (replicateM)
6+
import Data.ByteString.Short
7+
import Data.Hashable
8+
import System.Random.Stateful
9+
import Control.DeepSeq
10+
11+
newtype Bytes = Bytes {unBytes :: ShortByteString}
12+
deriving (Eq, Hashable, Show, NFData)
13+
14+
genBytes ::
15+
(StatefulGen g m) =>
16+
Int ->
17+
g ->
18+
m Bytes
19+
genBytes len gen = Bytes <$> uniformShortByteStringM len gen
20+
21+
genNBytes ::
22+
(StatefulGen g m) =>
23+
Int ->
24+
Int ->
25+
g ->
26+
m [Bytes]
27+
genNBytes n len = replicateM n . genBytes len
28+
29+
instance Uniform Bytes where
30+
uniformM = genBytes 32

unordered-containers.cabal

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,14 @@ benchmark fine-grained
142142
type: exitcode-stdio-1.0
143143

144144
other-modules:
145+
Key.Bytes
145146

146147
build-depends:
147148
base,
149+
bytestring,
150+
deepseq,
148151
hashable,
152+
random,
149153
tasty-bench,
150154
unordered-containers
151155

0 commit comments

Comments
 (0)