Skip to content

Commit b581ccd

Browse files
committed
feat(lsm-tree-micro-bench): add salt
1 parent 31ed3cc commit b581ccd

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

bench/micro/Bench/Database/LSMTree.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Control.DeepSeq
66
import Control.Exception
77
import Control.Tracer
88
import Criterion.Main
9+
import qualified Data.BloomFilter.Hash as Bloom
910
import Data.ByteString.Short (ShortByteString)
1011
import qualified Data.ByteString.Short as SBS
1112
import Data.Foldable
@@ -82,6 +83,9 @@ benchConfig = defaultTableConfig
8283
, confFencePointerIndex = CompactIndex
8384
}
8485

86+
bloomSalt :: Bloom.Salt
87+
bloomSalt = 4
88+
8589
{-------------------------------------------------------------------------------
8690
Large Value vs. Small Value Blob
8791
-------------------------------------------------------------------------------}
@@ -135,7 +139,7 @@ benchLargeValueVsSmallValueBlob =
135139

136140
initialise inss = do
137141
(tmpDir, hfs, hbio) <- mkFiles
138-
s <- openSession nullTracer hfs hbio (FS.mkFsPath [])
142+
s <- openSession nullTracer hfs hbio bloomSalt (FS.mkFsPath [])
139143
t <- newTableWith benchConfig s
140144
V.mapM_ (inserts t) inss
141145
pure (tmpDir, hfs, hbio, s, t)
@@ -220,7 +224,7 @@ benchCursorScanVsRangeLookupScan =
220224

221225
initialise inss = do
222226
(tmpDir, hfs, hbio) <- mkFiles
223-
s <- openSession nullTracer hfs hbio (FS.mkFsPath [])
227+
s <- openSession nullTracer hfs hbio bloomSalt (FS.mkFsPath [])
224228
t <- newTableWith benchConfig s
225229
V.mapM_ (inserts t) inss
226230
pure (tmpDir, hfs, hbio, s, t)
@@ -265,7 +269,7 @@ benchInsertBatches =
265269

266270
initialise = do
267271
(tmpDir, hfs, hbio) <- mkFiles
268-
s <- openSession nullTracer hfs hbio (FS.mkFsPath [])
272+
s <- openSession nullTracer hfs hbio bloomSalt (FS.mkFsPath [])
269273
t <- newTableWith _benchConfig s
270274
pure (tmpDir, hfs, hbio, s, t)
271275

@@ -451,7 +455,7 @@ mkTable ::
451455
, Table IO K V3 B3
452456
)
453457
mkTable hfs hbio conf = do
454-
sesh <- openSession nullTracer hfs hbio (FS.mkFsPath [])
458+
sesh <- openSession nullTracer hfs hbio bloomSalt (FS.mkFsPath [])
455459
t <- newTableWith conf sesh
456460
pure (sesh, t)
457461

bench/micro/Bench/Database/LSMTree/Internal/BloomFilter.hs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ benchmarks = bgroup "Bench.Database.LSMTree.Internal.BloomFilter" [
4545
]
4646
]
4747

48+
bloomSalt :: Bloom.Salt
49+
bloomSalt = 4
50+
4851
-- | Input environment for benchmarking 'Bloom.elem'.
4952
elemEnv ::
5053
Double -- ^ False positive rate
@@ -61,7 +64,7 @@ elemEnv fpr nbloom nelemsPositive nelemsNegative = do
6164
$ uniformWithoutReplacement @UTxOKey g1 (nbloom + nelemsNegative)
6265
ys2 = sampleUniformWithReplacement @UTxOKey g2 nelemsPositive xs
6366
zs = shuffle (ys1 ++ ys2) g3
64-
pure ( Bloom.fromList (Bloom.policyForFPR fpr) (fmap serialiseKey xs)
67+
pure ( Bloom.fromList (Bloom.policyForFPR fpr) bloomSalt (fmap serialiseKey xs)
6568
, fmap serialiseKey zs
6669
)
6770

@@ -86,5 +89,5 @@ constructBloom ::
8689
constructBloom fpr m =
8790
-- For faster construction, avoid going via lists and use Bloom.create,
8891
-- traversing the map inserting the keys
89-
Bloom.create (Bloom.sizeForFPR fpr (Map.size m)) $ \b ->
92+
Bloom.create (Bloom.sizeForFPR fpr (Map.size m)) bloomSalt $ \b ->
9093
BiFold.bifoldMap (\k -> Bloom.insert b k) (\_v -> pure ()) m

bench/micro/Bench/Database/LSMTree/Internal/Lookup.hs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import Database.LSMTree.Internal.Lookup (bloomQueries, indexSearches,
2727
intraPageLookupsWithWriteBuffer, lookupsIOWithWriteBuffer,
2828
prepLookups)
2929
import Database.LSMTree.Internal.Page (getNumPages)
30-
import Database.LSMTree.Internal.Paths (RunFsPaths (..))
30+
import Database.LSMTree.Internal.Paths (RunFsPaths (..),
31+
SessionSalt (..))
3132
import Database.LSMTree.Internal.Run (Run)
3233
import qualified Database.LSMTree.Internal.Run as Run
3334
import qualified Database.LSMTree.Internal.RunAcc as RunAcc
@@ -84,6 +85,9 @@ benchmarks = bgroup "Bench.Database.LSMTree.Internal.Lookup" [
8485
}
8586
]
8687

88+
sessionSalt :: SessionSalt
89+
sessionSalt = SessionSalt 4
90+
8791
benchLookups :: Config -> Benchmark
8892
benchLookups conf@Config{name} =
8993
withEnv $ \ ~(_dir, arenaManager, _hasFS, hasBlockIO, wbblobs, rs, ks) ->
@@ -96,23 +100,23 @@ benchLookups conf@Config{name} =
96100
-- The bloomfilter is queried for all lookup keys. The result is an
97101
-- unboxed vector, so only use @whnf@.
98102
bench "Bloomfilter query" $
99-
whnf (\ks' -> bloomQueries blooms ks') ks
103+
whnf (\ks' -> bloomQueries sessionSalt blooms ks') ks
100104
-- The compact index is only searched for (true and false) positive
101105
-- lookup keys. We use whnf here because the result is
102-
, env (pure $ bloomQueries blooms ks) $ \rkixs ->
106+
, env (pure $ bloomQueries sessionSalt blooms ks) $ \rkixs ->
103107
bench "Compact index search" $
104108
whnfAppIO (\ks' -> withArena arenaManager $ \arena -> stToIO $ indexSearches arena indexes kopsFiles ks' rkixs) ks
105109
-- prepLookups combines bloom filter querying and index searching.
106110
-- The implementation forces the results to WHNF, so we use
107111
-- whnfAppIO here instead of nfAppIO.
108112
, bench "Lookup preparation in memory" $
109-
whnfAppIO (\ks' -> withArena arenaManager $ \arena -> stToIO $ prepLookups arena blooms indexes kopsFiles ks') ks
113+
whnfAppIO (\ks' -> withArena arenaManager $ \arena -> stToIO $ prepLookups arena sessionSalt blooms indexes kopsFiles ks') ks
110114
-- Submit the IOOps we get from prepLookups to HasBlockIO. We use
111115
-- perRunEnv because IOOps contain mutable buffers, so we want fresh
112116
-- ones for each run of the benchmark. We manually evaluate the
113117
-- result to WHNF since it is unboxed vector.
114118
, bench "Submit IOOps" $
115-
perRunEnv (withArena arenaManager $ \arena -> stToIO $ prepLookups arena blooms indexes kopsFiles ks) $ \ ~(_rkixs, ioops) -> do
119+
perRunEnv (withArena arenaManager $ \arena -> stToIO $ prepLookups arena sessionSalt blooms indexes kopsFiles ks) $ \ ~(_rkixs, ioops) -> do
116120
!_ioress <- FS.submitIO hasBlockIO ioops
117121
pure ()
118122
-- When IO result have been collected, intra-page lookups searches
@@ -125,7 +129,7 @@ benchLookups conf@Config{name} =
125129
, bench "Perform intra-page lookups" $
126130
perRunEnvWithCleanup
127131
( do arena <- newArena arenaManager
128-
(rkixs, ioops) <- stToIO (prepLookups arena blooms indexes kopsFiles ks)
132+
(rkixs, ioops) <- stToIO (prepLookups arena sessionSalt blooms indexes kopsFiles ks)
129133
ioress <- FS.submitIO hasBlockIO ioops
130134
pure (rkixs, ioops, ioress, arena)
131135
)
@@ -141,7 +145,7 @@ benchLookups conf@Config{name} =
141145
, bench "Lookups in IO" $
142146
whnfAppIO (\ks' -> lookupsIOWithWriteBuffer
143147
hasBlockIO arenaManager resolveV
144-
WB.empty wbblobs
148+
sessionSalt WB.empty wbblobs
145149
rs blooms indexes kopsFiles ks') ks
146150
]
147151
-- TODO: consider adding benchmarks that also use the write buffer
@@ -192,7 +196,7 @@ lookupsInBatchesEnv Config {..} = do
192196
wbblobs <- WBB.new hasFS (FS.mkFsPath ["0.wbblobs"])
193197
wb <- WB.fromMap <$> traverse (traverse (WBB.addBlob hasFS wbblobs)) storedKeys
194198
let fsps = RunFsPaths (FS.mkFsPath []) (RunNumber 0)
195-
r <- Run.fromWriteBuffer hasFS hasBlockIO runParams fsps wb wbblobs
199+
r <- Run.fromWriteBuffer hasFS hasBlockIO sessionSalt runParams fsps wb wbblobs
196200
let NumEntries nentriesReal = Run.size r
197201
assertEqual nentriesReal nentries $ pure ()
198202
-- 42 to 43 entries per page

bench/micro/Bench/Database/LSMTree/Internal/Merge.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import Database.LSMTree.Internal.Entry
2020
import qualified Database.LSMTree.Internal.Index as Index (IndexType (Compact))
2121
import Database.LSMTree.Internal.Merge (MergeType (..))
2222
import qualified Database.LSMTree.Internal.Merge as Merge
23-
import Database.LSMTree.Internal.Paths (RunFsPaths (..))
23+
import Database.LSMTree.Internal.Paths (RunFsPaths (..),
24+
SessionSalt (..))
2425
import Database.LSMTree.Internal.Run (Run)
2526
import qualified Database.LSMTree.Internal.Run as Run
2627
import qualified Database.LSMTree.Internal.RunAcc as RunAcc
@@ -220,6 +221,9 @@ benchmarks = bgroup "Bench.Database.LSMTree.Internal.Merge" [
220221
| w <- weights
221222
]
222223

224+
sessionSalt :: SessionSalt
225+
sessionSalt = SessionSalt 4
226+
223227
runParams :: RunBuilder.RunParams
224228
runParams =
225229
RunBuilder.RunParams {
@@ -273,7 +277,7 @@ merge ::
273277
merge fs hbio Config {..} targetPaths runs = do
274278
let f = fromMaybe const mergeResolve
275279
m <- fromMaybe (error "empty inputs, no merge created") <$>
276-
Merge.new fs hbio runParams mergeType f targetPaths runs
280+
Merge.new fs hbio sessionSalt runParams mergeType f targetPaths runs
277281
Merge.stepsToCompletion m stepSize
278282

279283
fsPath :: FS.FsPath
@@ -397,7 +401,7 @@ randomRuns ::
397401
randomRuns hasFS hasBlockIO config@Config {..} rng0 = do
398402
counter <- inputRunPathsCounter
399403
fmap V.fromList $
400-
mapM (unsafeCreateRun hasFS hasBlockIO runParams fsPath counter) $
404+
mapM (unsafeCreateRun hasFS hasBlockIO sessionSalt runParams fsPath counter) $
401405
zipWith
402406
(randomRunData config)
403407
nentries
@@ -446,5 +450,5 @@ randomRunData Config {..} runentries g0 =
446450
-- Each run entry needs a distinct key.
447451
randomWord64OutOf :: Int -> Rnd SerialisedKey
448452
randomWord64OutOf possibleKeys =
449-
first (serialiseKey . Hash.hash64)
453+
first (serialiseKey . Hash.hashSalt64 0)
450454
. uniformR (0, fromIntegral possibleKeys :: Word64)

0 commit comments

Comments
 (0)