Skip to content

Commit 9c4dd1f

Browse files
committed
Do merge work in batches
See `Note [Credits]`
1 parent 77cb0ba commit 9c4dd1f

File tree

6 files changed

+559
-134
lines changed

6 files changed

+559
-134
lines changed

bench/micro/Bench/Database/LSMTree/Normal.hs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ benchmarks :: Benchmark
3131
benchmarks = bgroup "Bench.Database.LSMTree.Normal" [
3232
benchLargeValueVsSmallValueBlob
3333
, benchCursorScanVsRangeLookupScan
34+
, benchInsertBatches
3435
]
3536

3637
{-------------------------------------------------------------------------------
@@ -215,6 +216,50 @@ benchCursorScanVsRangeLookupScan =
215216
Normal.closeSession s
216217
cleanupFiles (tmpDir, hfs, hbio)
217218

219+
220+
{-------------------------------------------------------------------------------
221+
Benchmark batches of inserts
222+
-------------------------------------------------------------------------------}
223+
224+
benchInsertBatches :: Benchmark
225+
benchInsertBatches =
226+
env genInserts $ \iss ->
227+
withEnv $ \ ~(_, _, _, _, t :: Normal.Table IO Word64 Word64 Void) -> do
228+
bench "benchInsertBatches" $ whnfIO $
229+
V.mapM_ (flip Normal.inserts t) iss
230+
where
231+
!initialSize = 100_000
232+
!batchSize = 256
233+
234+
_benchConfig :: Common.TableConfig
235+
_benchConfig = Common.defaultTableConfig {
236+
Common.confWriteBufferAlloc = Common.AllocNumEntries (Common.NumEntries 1000)
237+
}
238+
239+
randomInserts :: Int -> V.Vector (Word64, Word64, Maybe Void)
240+
randomInserts n = V.unfoldrExactN n f (mkStdGen 17)
241+
where f !g = let (!k, !g') = uniform g
242+
in ((k, v, Nothing), g')
243+
-- The exact value does not matter much, so we pick an arbitrary
244+
-- hardcoded one.
245+
!v = 17
246+
247+
genInserts :: IO (V.Vector (V.Vector (Word64, Word64, Maybe Void)))
248+
genInserts = pure $ vgroupsOfN batchSize $ randomInserts initialSize
249+
250+
withEnv = envWithCleanup initialise cleanup
251+
252+
initialise = do
253+
(tmpDir, hfs, hbio) <- mkFiles
254+
s <- Normal.openSession nullTracer hfs hbio (FS.mkFsPath [])
255+
t <- Normal.new s _benchConfig
256+
pure (tmpDir, hfs, hbio, s, t)
257+
258+
cleanup (tmpDir, hfs, hbio, s, t) = do
259+
Normal.close t
260+
Normal.closeSession s
261+
cleanupFiles (tmpDir, hfs, hbio)
262+
218263
{-------------------------------------------------------------------------------
219264
Setup
220265
-------------------------------------------------------------------------------}

src-extras/Database/LSMTree/Extras/NoThunks.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,18 @@ deriving anyclass instance NoThunks MergePolicyForLevel
284284
deriving stock instance Generic NumRuns
285285
deriving anyclass instance NoThunks NumRuns
286286

287+
deriving stock instance Generic (UnspentCreditsVar s)
288+
deriving anyclass instance Typeable s => NoThunks (UnspentCreditsVar s)
289+
290+
deriving stock instance Generic (TotalStepsVar s)
291+
deriving anyclass instance Typeable s => NoThunks (TotalStepsVar s)
292+
293+
deriving stock instance Generic (SpentCreditsVar s)
294+
deriving anyclass instance Typeable s => NoThunks (SpentCreditsVar s)
295+
296+
deriving stock instance Generic MergeKnownCompleted
297+
deriving anyclass instance NoThunks MergeKnownCompleted
298+
287299
{-------------------------------------------------------------------------------
288300
Entry
289301
-------------------------------------------------------------------------------}

src/Database/LSMTree/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ snapshot resolve snap label tableType t = do
11201120
-- credits as if the buffer was full, and then flush the (possibly)
11211121
-- underfull buffer. However, note that this bit of code
11221122
-- here is probably going to change anyway because of #392
1123-
supplyCredits (unNumEntries $ case confWriteBufferAlloc conf of AllocNumEntries x -> x) (tableLevels content)
1123+
supplyCredits conf (Credit $ unNumEntries $ case confWriteBufferAlloc conf of AllocNumEntries x -> x) (tableLevels content)
11241124
content' <- flushWriteBuffer
11251125
(TraceMerge `contramap` tableTracer t)
11261126
conf

0 commit comments

Comments
 (0)