Skip to content

Commit 1559cc8

Browse files
authored
Merge pull request #417 from IntersectMBO/dcoutts/refcount-vs-reference
Convert reference counted resources to a new Ref API
2 parents 32616fe + 72471de commit 1559cc8

File tree

32 files changed

+1213
-707
lines changed

32 files changed

+1213
-707
lines changed

bench/macro/lsm-tree-bench-lookups.hs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Control.Monad
77
import Control.Monad.Class.MonadST
88
import Control.Monad.Primitive
99
import Control.Monad.ST.Strict (ST, runST)
10-
import Control.RefCount (RefCount (..))
10+
import Control.RefCount
1111
import Data.Arena (ArenaManager, newArenaManager, withArena)
1212
import Data.Bits ((.&.))
1313
import Data.BloomFilter (Bloom)
@@ -162,9 +162,9 @@ benchmarks !caching = withFS $ \hfs hbio -> do
162162
putStrLn "<finished>"
163163

164164
traceMarkerIO "Computing statistics for generated runs"
165-
let numEntries = V.map Run.runNumEntries runs
166-
numPages = V.map Run.sizeInPages runs
167-
nhashes = V.map Bloom.hashesN blooms
165+
let numEntries = V.map Run.size runs
166+
numPages = V.map Run.sizeInPages runs
167+
nhashes = V.map Bloom.hashesN blooms
168168
bitsPerEntry = V.zipWith
169169
(\b (NumEntries n) -> fromIntegral (Bloom.length b) / fromIntegral n :: Double)
170170
blooms
@@ -213,7 +213,7 @@ benchmarks !caching = withFS $ \hfs hbio -> do
213213

214214
traceMarkerIO "Cleaning up"
215215
putStrLn "Cleaning up"
216-
V.mapM_ Run.removeReference runs
216+
V.mapM_ releaseRef runs
217217

218218
traceMarkerIO "Computing statistics for prepLookups results"
219219
putStr "<Computing statistics for prepLookups>"
@@ -331,7 +331,7 @@ lookupsEnv ::
331331
-> FS.HasFS IO FS.HandleIO
332332
-> FS.HasBlockIO IO FS.HandleIO
333333
-> Run.RunDataCaching
334-
-> IO ( V.Vector (Run IO FS.HandleIO)
334+
-> IO ( V.Vector (Ref (Run IO FS.HandleIO))
335335
, V.Vector (Bloom SerialisedKey)
336336
, V.Vector IndexCompact
337337
, V.Vector (FS.Handle FS.HandleIO)
@@ -371,11 +371,10 @@ lookupsEnv runSizes keyRng0 hfs hbio caching = do
371371
putStr "DONE"
372372

373373
-- return runs
374-
runs <- V.fromList <$>
375-
mapM (Run.fromMutable caching (RefCount 1)) rbs
376-
let blooms = V.map Run.runFilter runs
377-
indexes = V.map Run.runIndex runs
378-
handles = V.map Run.runKOpsFile runs
374+
runs <- V.fromList <$> mapM (Run.fromMutable caching) rbs
375+
let blooms = V.map (\(DeRef r) -> Run.runFilter r) runs
376+
indexes = V.map (\(DeRef r) -> Run.runIndex r) runs
377+
handles = V.map (\(DeRef r) -> Run.runKOpsFile r) runs
379378
pure $!! (runs, blooms, indexes, handles)
380379

381380
genLookupBatch :: StdGen -> Int -> (V.Vector SerialisedKey, StdGen)
@@ -466,8 +465,8 @@ benchLookupsIO ::
466465
-> ArenaManager RealWorld
467466
-> ResolveSerialisedValue
468467
-> WB.WriteBuffer
469-
-> WBB.WriteBufferBlobs IO h
470-
-> V.Vector (Run IO h)
468+
-> Ref (WBB.WriteBufferBlobs IO h)
469+
-> V.Vector (Ref (Run IO h))
471470
-> V.Vector (Bloom SerialisedKey)
472471
-> V.Vector IndexCompact
473472
-> V.Vector (FS.Handle h)

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Bench.Database.LSMTree.Internal.Lookup (benchmarks) where
55
import Control.Exception (assert)
66
import Control.Monad
77
import Control.Monad.ST.Strict (stToIO)
8+
import Control.RefCount
89
import Criterion.Main (Benchmark, bench, bgroup, env, envWithCleanup,
910
perRunEnv, perRunEnvWithCleanup, whnf, whnfAppIO)
1011
import Data.Arena (ArenaManager, closeArena, newArena,
@@ -20,7 +21,7 @@ import Database.LSMTree.Extras.Random (frequency,
2021
sampleUniformWithReplacement, uniformWithoutReplacement)
2122
import Database.LSMTree.Extras.UTxO
2223
import Database.LSMTree.Internal.BlobRef (BlobSpan (..))
23-
import Database.LSMTree.Internal.Entry (Entry (..), unNumEntries)
24+
import Database.LSMTree.Internal.Entry (Entry (..), NumEntries (..))
2425
import Database.LSMTree.Internal.Lookup (bloomQueries, indexSearches,
2526
intraPageLookups, lookupsIO, prepLookups)
2627
import Database.LSMTree.Internal.Page (getNumPages)
@@ -83,9 +84,9 @@ benchmarks = bgroup "Bench.Database.LSMTree.Internal.Lookup" [
8384
benchLookups :: Config -> Benchmark
8485
benchLookups conf@Config{name} =
8586
withEnv $ \ ~(_dir, arenaManager, hasFS, hasBlockIO, rs, ks) ->
86-
env ( pure ( V.map Run.runFilter rs
87-
, V.map Run.runIndex rs
88-
, V.map Run.runKOpsFile rs
87+
env ( pure ( V.map (\(DeRef r) -> Run.runFilter r) rs
88+
, V.map (\(DeRef r) -> Run.runIndex r) rs
89+
, V.map (\(DeRef r) -> Run.runKOpsFile r) rs
8990
)
9091
) $ \ ~(blooms, indexes, kopsFiles) ->
9192
bgroup name [
@@ -130,7 +131,7 @@ benchLookups conf@Config{name} =
130131
)
131132
(\(_, _, _, arena, wbblobs) -> do
132133
closeArena arenaManager arena
133-
WBB.removeReference wbblobs)
134+
releaseRef wbblobs)
134135
(\ ~(rkixs, ioops, ioress, _, wbblobs_unused) -> do
135136
!_ <- intraPageLookups resolveV WB.empty wbblobs_unused
136137
rs ks rkixs ioops ioress
@@ -180,7 +181,7 @@ lookupsInBatchesEnv ::
180181
, ArenaManager RealWorld
181182
, FS.HasFS IO FS.HandleIO
182183
, FS.HasBlockIO IO FS.HandleIO
183-
, V.Vector (Run IO FS.HandleIO)
184+
, V.Vector (Ref (Run IO FS.HandleIO))
184185
, V.Vector SerialisedKey
185186
)
186187
lookupsInBatchesEnv Config {..} = do
@@ -194,7 +195,7 @@ lookupsInBatchesEnv Config {..} = do
194195
fsps = RunFsPaths (FS.mkFsPath []) (RunNumber 0)
195196
wbblobs <- WBB.new hasFS (FS.mkFsPath [])
196197
r <- Run.fromWriteBuffer hasFS hasBlockIO caching (RunAllocFixed 10) fsps wb wbblobs
197-
let nentriesReal = unNumEntries $ Run.runNumEntries r
198+
let NumEntries nentriesReal = Run.size r
198199
assert (nentriesReal == nentries) $ pure ()
199200
let npagesReal = Run.sizeInPages r
200201
assert (getNumPages npagesReal * 42 <= nentriesReal) $ pure ()
@@ -212,13 +213,13 @@ lookupsInBatchesCleanup ::
212213
, ArenaManager RealWorld
213214
, FS.HasFS IO FS.HandleIO
214215
, FS.HasBlockIO IO FS.HandleIO
215-
, V.Vector (Run IO FS.HandleIO)
216+
, V.Vector (Ref (Run IO FS.HandleIO))
216217
, V.Vector SerialisedKey
217218
)
218219
-> IO ()
219220
lookupsInBatchesCleanup (tmpDir, _arenaManager, _hasFS, hasBlockIO, rs, _) = do
220221
FS.close hasBlockIO
221-
forM_ rs Run.removeReference
222+
forM_ rs releaseRef
222223
removeDirectoryRecursive tmpDir
223224

224225
-- | Generate keys to store and keys to lookup

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module Bench.Database.LSMTree.Internal.Merge (benchmarks) where
22

33
import Control.Monad (when, zipWithM)
4+
import Control.RefCount
45
import Criterion.Main (Benchmark, bench, bgroup)
56
import qualified Criterion.Main as Cr
67
import Data.Bifunctor (first)
@@ -206,7 +207,7 @@ benchMerge conf@Config{name} =
206207
-- Make sure to immediately close resulting runs so we don't run
207208
-- out of file handles. Ideally this would not be measured, but at
208209
-- least it's pretty cheap.
209-
Run.removeReference run
210+
releaseRef run
210211
]
211212
where
212213
withEnv =
@@ -228,7 +229,7 @@ merge ::
228229
-> Config
229230
-> Run.RunFsPaths
230231
-> InputRuns
231-
-> IO (Run IO FS.HandleIO)
232+
-> IO (Ref (Run IO FS.HandleIO))
232233
merge fs hbio Config {..} targetPaths runs = do
233234
let f = fromMaybe const mergeMappend
234235
m <- fromMaybe (error "empty inputs, no merge created") <$>
@@ -241,7 +242,7 @@ outputRunPaths = RunFsPaths (FS.mkFsPath []) (RunNumber 0)
241242
inputRunPaths :: [Run.RunFsPaths]
242243
inputRunPaths = RunFsPaths (FS.mkFsPath []) . RunNumber <$> [1..]
243244

244-
type InputRuns = V.Vector (Run IO FS.HandleIO)
245+
type InputRuns = V.Vector (Ref (Run IO FS.HandleIO))
245246

246247
type Mappend = SerialisedValue -> SerialisedValue -> SerialisedValue
247248

@@ -331,7 +332,7 @@ mergeEnvCleanup ::
331332
)
332333
-> IO ()
333334
mergeEnvCleanup (tmpDir, _hasFS, hasBlockIO, runs) = do
334-
traverse_ Run.removeReference runs
335+
traverse_ releaseRef runs
335336
removeDirectoryRecursive tmpDir
336337
FS.close hasBlockIO
337338

lsm-tree.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ benchmark lsm-tree-micro-bench
476476
, lsm-tree
477477
, lsm-tree:blockio-api
478478
, lsm-tree:bloomfilter
479+
, lsm-tree:control
479480
, lsm-tree:extras
480481
, QuickCheck
481482
, random
@@ -864,6 +865,7 @@ test-suite control-test
864865
, io-classes
865866
, io-sim
866867
, lsm-tree:control
868+
, primitive
867869
, QuickCheck
868870
, tasty
869871
, tasty-quickcheck

0 commit comments

Comments
 (0)