Skip to content

Commit

Permalink
Do not require IO to initialize HashMap benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
utdemir committed Jul 24, 2021
1 parent ed59552 commit a3e1a5e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
30 changes: 13 additions & 17 deletions bench/Data/Mutable/HashMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
module Data.Mutable.HashMap (hmbench, getHMInput) where
module Data.Mutable.HashMap (hmbench) where

import Gauge
import qualified System.Random as Random
Expand All @@ -21,14 +21,12 @@ import qualified Data.Unrestricted.Linear as Linear
import Data.List (foldl')
import qualified Prelude.Linear as Linear
import Control.Monad.ST (runST, ST)
import Control.Exception (evaluate)

import qualified Data.HashMap.Mutable.Linear as LMap
import qualified Data.HashMap.Strict as Map
import qualified Data.HashTable.ST.Basic as BasicST
import qualified Data.HashTable.ST.Cuckoo as CuckooST


-- # Exported benchmarks
-------------------------------------------------------------------------------

Expand All @@ -50,6 +48,9 @@ data BenchInput where
, shuffle2 :: ![Key]
, shuffle3 :: ![Key]
} -> BenchInput
deriving Generic

instance NFData BenchInput

hmbench :: BenchInput -> Benchmark
hmbench inp = bgroup "Comparing Linear Hashmaps"
Expand All @@ -62,6 +63,15 @@ hmbench inp = bgroup "Comparing Linear Hashmaps"
, bgroup "hashtables:Data.HashTable.ST.Cuckoo" $
st_cuckoo inp
]
where
!inp = force . flip Random.evalRand (Random.mkStdGen 4541645642) $ do
let keys = map Key $ enumFromTo 1 num_keys
shuff1 <- Random.shuffleM keys
shuff2 <- Random.shuffleM shuff1
shuff3 <- Random.shuffleM shuff2
vals <- Random.getRandomRs (0, num_keys)
let kv_pairs = zip keys vals
return $ BenchInput kv_pairs shuff1 shuff2 shuff3

descriptions :: [String]
descriptions =
Expand All @@ -80,20 +90,6 @@ descriptions =
num_keys :: Int
num_keys = 100_000

getHMInput :: IO BenchInput
getHMInput = do
let keys = map Key $ enumFromTo 1 num_keys
g0 <- Random.getStdGen
let (gx,gc) = Random.split g0
let (ga,gb) = Random.split gx
shuff1 <- evaluate $ force $ Random.shuffle' keys num_keys ga
shuff2 <- evaluate $ force $ Random.shuffle' shuff1 num_keys gb
shuff3 <- evaluate $ force $ Random.shuffle' shuff2 num_keys gc
g1 <- Random.getStdGen
let (vals :: [Int]) = Random.randomRs (0,num_keys) g1
kv_pairs <- evaluate $ force (zip keys vals)
return $ BenchInput kv_pairs shuff1 shuff2 shuff3

modVal :: Maybe Int -> Maybe Int
modVal Nothing = Nothing
modVal (Just !k)
Expand Down
5 changes: 2 additions & 3 deletions bench/Main.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
module Main where

import Gauge
import Data.Mutable.HashMap (hmbench, getHMInput)
import Data.Mutable.HashMap (hmbench)
import qualified Data.Mutable.Array as Array

main :: IO ()
main = do
hmInput <- getHMInput
defaultMain
[ hmbench hmInput
[ hmbench
, Array.benchmarks
]

3 changes: 2 additions & 1 deletion linear-base.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ benchmark mutable-data
linear-base,
random,
random-shuffle,
unordered-containers
unordered-containers,
MonadRandom
ghc-options: -O
default-language: Haskell2010

Expand Down

0 comments on commit a3e1a5e

Please sign in to comment.