Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bench/Bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Main (main) where
-- import Chainweb.Pact.Backend.Bench qualified as Checkpointer
-- import Chainweb.Pact.Backend.ForkingBench qualified as ForkingBench
import Chainweb.MempoolBench qualified as MempoolBench
import Chainweb.BlockHeaderDBBench qualified as BlockHeaderDBBench
import Chainweb.CutDBBench qualified as CutDBBench
-- import Chainweb.Pact.Backend.PactService qualified as PactService
-- import Chainweb.Storage.Table.RocksDB (withTempRocksDb)
Expand All @@ -24,6 +25,7 @@ main :: IO ()
main = defaultMain
[ JSONEncoding.benchmarks
, MempoolBench.bench
, BlockHeaderDBBench.bench
, CutDBBench.bench
]

Expand Down
142 changes: 142 additions & 0 deletions bench/Chainweb/BlockHeaderDBBench.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

-- |
-- Module: Chainweb.BlockHeaderDBBench
-- Copyright: Copyright © 2025 Kadena LLC.
-- License: MIT
--
-- Benchmarks for 'BlockHeaderDb' queries, in particular the 'TreeDb'
-- branch traversal code paths that back header synchronization.
--
module Chainweb.BlockHeaderDBBench (bench) where

import Control.Exception
import Control.Monad
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Trans.Resource (InternalState, ResourceT)
import Control.Monad.Trans.Resource qualified as ResourceT

import Criterion.Main qualified as C

import Data.HashSet qualified as HS

import Numeric.Natural

import Streaming.Prelude qualified as S

-- internal modules

import Chainweb.BlockHeader
import Chainweb.BlockHeaderDB
import Chainweb.Test.Utils
import Chainweb.TreeDB
import Chainweb.Utils
import Chainweb.Utils.Bench
import Chainweb.Version

-- -------------------------------------------------------------------------- --
-- Fixture

-- | A linear trunk of this many blocks on top of genesis.
trunkLength :: Natural
trunkLength = 1_000

-- | The fork branches off from the trunk at this height.
forkStartHeight :: Natural
forkStartHeight = 950

-- | The fork extends this many blocks beyond the fork point, so the fork tip
-- has the same height as the trunk tip.
forkLength :: Natural
forkLength = 50

data BenchDb = BenchDb
{ _bdbDb :: !BlockHeaderDb
, _bdbTrunkTip :: !BlockHeader
, _bdbForkPoint :: !BlockHeader
, _bdbForkTip :: !BlockHeader
}

-- | Keep a 'ResourceT' allocation alive until Criterion invokes the matching
-- cleanup action.
acquire :: ResourceT IO a -> IO (NoopNFData (a, InternalState))
acquire action = mask $ \restore -> do
state <- ResourceT.createInternalState
result <- restore (ResourceT.runInternalState action state)
`onException` ResourceT.closeInternalState state
return $ NoopNFData (result, state)

release :: NoopNFData (a, InternalState) -> IO ()
release (NoopNFData (_, state)) = ResourceT.closeInternalState state

benchDbResource :: HasVersion => ResourceT IO BenchDb
benchDbResource = do
rocksDb <- withRocksResource
db <- withTestBlockHeaderDb rocksDb genesis
trunk <- liftIO $ insertN_ (Nonce 0) trunkLength genesis db
let trunkTip = last trunk
forkPoint = trunk !! (int forkStartHeight - 1)
fork <- liftIO $ insertN_ (Nonce 1) forkLength forkPoint db
return $ BenchDb db trunkTip forkPoint (last fork)
where
genesis = genesisBlockHeader toyChainId

-- -------------------------------------------------------------------------- --
-- Benchmarks

bench :: C.Benchmark
bench = withVersion toyVersion
$ C.envWithCleanup (acquire benchDbResource) release
$ \ ~(NoopNFData (bdb, _)) -> benchmarks bdb

benchmarks :: HasVersion => BenchDb -> C.Benchmark
benchmarks ~(BenchDb db trunkTip forkPoint forkTip) = C.bgroup "block-header-db"
[ C.bgroup "getBranch"
[ C.bench "ancestors/trunk-tip (1000 blocks)"
$ C.nfIO
$ S.length_ (ancestors db (key trunkTip))
, C.bench "branchEntries/page (limit 250, maxRank=tip)"
$ C.nfIO
$ branchEntries db Nothing (Just 250) Nothing (Just tipRank)
mempty (upper trunkTip)
$ S.length_ . void
, C.bench "branchEntries/page (limit 250, maxRank=tip, lower=fork-point)"
$ C.nfIO
$ branchEntries db Nothing (Just 250) Nothing (Just tipRank)
(lower forkPoint) (upper trunkTip)
$ S.length_ . void
, C.bench "branchEntries/page (limit 250, maxRank=tip-500)"
$ C.nfIO
$ branchEntries db Nothing (Just 250) Nothing (Just midRank)
mempty (upper trunkTip)
$ S.length_ . void
]
, C.bgroup "seekAncestor"
[ C.bench "unique rank (tip - 500)"
$ C.nfIO
$ seekAncestor db trunkTip (rank trunkTip - 500)
, C.bench "fork region (tip - 25)"
$ C.nfIO
$ seekAncestor db trunkTip (rank trunkTip - 25)
]
, C.bgroup "fork"
[ C.bench "forkEntry (50 block fork)"
$ C.nfIO
$ forkEntry db trunkTip forkTip
, C.bench "branchDiff (50 block fork)"
$ C.nfIO
$ S.length_ (branchDiff db trunkTip forkTip)
, C.bench "getBranchIncreasing (last 100 blocks)"
$ C.nfIO
$ getBranchIncreasing db trunkTip (rank trunkTip - 100) S.length_
]
]
where
tipRank = int (rank trunkTip)
midRank = int (rank trunkTip - 500)
upper = HS.singleton . UpperBound . key
lower = HS.singleton . LowerBound . key
4 changes: 2 additions & 2 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ source-repository-package
source-repository-package
type: git
location: https://github.com/kda-community/rocksdb-haskell.git
tag: cede9de2932a4ead1bd82fd7709b19ab7b19b33d
--sha256: 1dngd44va6h66vwpdpwmnj0zcky87m4vzykjwv49p2km12lwq9mf
tag: 177187da7941d1b85afebaa5b278fbd5d017e182
--sha256: 037cr62rj86k037hs1pphdnpc4dg12nvdy53az9xndxk0msrl9ry

source-repository-package
type: git
Expand Down
2 changes: 2 additions & 0 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ test-suite chainweb-tests
Chainweb.Test.BlockHeader.Validation
Chainweb.Test.BlockHeaderDB
Chainweb.Test.BlockHeaderDB.PruneForks
Chainweb.Test.BlockHeaderDBBranch
Chainweb.Test.Chainweb.Utils.Paging
Chainweb.Test.CutDB
Chainweb.Test.Difficulty
Expand Down Expand Up @@ -930,6 +931,7 @@ benchmark bench
main-is: Bench.hs
type: exitcode-stdio-1.0
other-modules:
Chainweb.BlockHeaderDBBench
Chainweb.CutDBBench
Chainweb.MempoolBench
-- Chainweb.Pact.Backend.ApplyCmd
Expand Down
2 changes: 2 additions & 0 deletions libs/chainweb-storage/src/Chainweb/Storage/DedupStore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ newDedupStore rdb vc kc n = DedupStore
unless (B.length l == dedupHashSize)
$ throwM $ DedupStoreCorruptedData "Internal key has wrong size"
return (DedupHash l)
, _codecUnsafeIter = False
}

dedupChunkCodec = Codec
Expand All @@ -148,6 +149,7 @@ newDedupStore rdb vc kc n = DedupStore
| B.length c < dedupHashSize
-> throwM $ DedupStoreCorruptedData "Got too short chunk"
| otherwise -> return $ Chunk a (DedupHash c) d
, _codecUnsafeIter = False
}

-- | Insert a new items into a 'DedupStore'.
Expand Down
87 changes: 72 additions & 15 deletions libs/chainweb-storage/src/Chainweb/Storage/Table/RocksDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ data Codec a = Codec
-- ^ encode a value.
, _codecDecode :: !(forall m. MonadThrow m => B.ByteString -> m a)
-- ^ decode a value. Throws an exception of decoding fails.
, _codecUnsafeIter :: !Bool
-- ^ Whether the decode function is compatible with the zero-copy
-- 'Database.RocksDB.Iterator.unsafeIterKey' and
-- 'Database.RocksDB.Iterator.unsafeIterValue' iterators from
-- rocksdb-haskell.
--
-- Those iterators wrap the RocksDB-owned buffer directly instead of
-- copying it out and only force the result of the decode to weak head
-- normal form. Using them is therefore only sound when the decode
-- function never retains a reference to (never aliases) its input
-- 'B.ByteString' -- not even inside an unevaluated thunk. Only set this
-- to 'True' for codecs whose decode produces a value that is fully
-- independent of the input bytes (for instance decoders that copy the
-- input or build fresh values from it). When in doubt, leave it
-- 'False', in which case the (copying) iterators are used instead.
}

instance NoThunks (Codec a) where
Expand Down Expand Up @@ -428,21 +443,46 @@ instance Iterator (RocksDbTableIter k v) k v where

iterPrev = I.iterPrev . _rocksDbTableIter

iterEntry it =
I.iterEntry (_rocksDbTableIter it) >>= \case
Nothing -> return Nothing
Just (k, v) -> do
k' <- decIterKey it k
v' <- decIterVal it v
return $! Just $! Entry k' v'

iterValue it = I.iterValue (_rocksDbTableIter it) >>= \case
Nothing -> return Nothing
Just v -> Just <$> (evaluate =<< decIterVal it v)

iterKey it = I.iterKey (_rocksDbTableIter it) >>= \case
Nothing -> return Nothing
Just k -> Just <$> (evaluate =<< decIterKey it k)
iterEntry it
| _codecUnsafeIter (_rocksDbTableIterKeyCodec it)
, _codecUnsafeIter (_rocksDbTableIterValueCodec it) =
I.unsafeIterKey (_rocksDbTableIter it) (decIterKeyE it) >>= \case
Nothing -> return Nothing
Just (Left e) -> throwM e
Just (Right k) ->
I.unsafeIterValue (_rocksDbTableIter it) (decIterValE it) >>= \case
Nothing -> return Nothing
Just (Left e) -> throwM e
Just (Right v) -> return $! Just $! Entry k v
| otherwise =
I.iterEntry (_rocksDbTableIter it) >>= \case
Nothing -> return Nothing
Just (k, v) -> do
k' <- decIterKey it k
v' <- decIterVal it v
return $! Just $! Entry k' v'

iterValue it
| _codecUnsafeIter (_rocksDbTableIterValueCodec it) =
I.unsafeIterValue (_rocksDbTableIter it) (decIterValE it) >>= \case
Nothing -> return Nothing
Just (Left e) -> throwM e
Just (Right v) -> Just <$> evaluate v
| otherwise =
I.iterValue (_rocksDbTableIter it) >>= \case
Nothing -> return Nothing
Just v -> Just <$> (evaluate =<< decIterVal it v)

iterKey it
| _codecUnsafeIter (_rocksDbTableIterKeyCodec it) =
I.unsafeIterKey (_rocksDbTableIter it) (decIterKeyE it) >>= \case
Nothing -> return Nothing
Just (Left e) -> throwM e
Just (Right k) -> Just <$> evaluate k
| otherwise =
I.iterKey (_rocksDbTableIter it) >>= \case
Nothing -> return Nothing
Just k -> Just <$> (evaluate =<< decIterKey it k)

instance IterableTable (RocksDbTable k v) (RocksDbTableIter k v) k v where
withTableIterator db k = R.withReadOptions readOptions $ \opts_ptr ->
Expand Down Expand Up @@ -647,6 +687,23 @@ decIterKey it k =
where
prefix = namespaceFirst $ _rocksDbTableIterNamespace it

-- | Pure variants of 'decIterKey' and 'decIterVal' that run the codec's decode
-- in the @Either SomeException@ monad. They are used as the (pure) continuation
-- for the zero-copy 'I.unsafeIterKey' / 'I.unsafeIterValue' iterators.
--
-- These must only be used with codecs that set '_codecUnsafeIter' to 'True',
-- i.e. codecs whose decode never retains a reference to its input
-- 'B.ByteString'.
--
decIterKeyE :: RocksDbTableIter k v -> B.ByteString -> Either SomeException k
decIterKeyE it k =
_codecDecode (_rocksDbTableIterKeyCodec it) (B.drop (B.length prefix) k)
where
prefix = namespaceFirst $ _rocksDbTableIterNamespace it

decIterValE :: RocksDbTableIter k v -> B.ByteString -> Either SomeException v
decIterValE i = _codecDecode (_rocksDbTableIterValueCodec i)

checked :: HasCallStack => String -> (Ptr CString -> IO a) -> IO a
checked whatWasIDoing act = alloca $ \errPtr -> do
poke errPtr (nullPtr :: CString)
Expand Down
1 change: 1 addition & 0 deletions libs/chainweb-storage/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ intCodec :: Codec Int
intCodec = Codec
(B8.pack . show)
(either (throwM . CodecException) return . readEither @Int . B8.unpack)
True

intTable :: RocksDb -> B8.ByteString -> RocksDbTable Int Int
intTable db tableName = newTable db intCodec intCodec [tableName]
Expand Down
Loading
Loading