commit a44d12458da04e27cfa4f45ec38107171e1c6220
parent 295a91b084bf7d17235456ed16ecf493bc2c7692
Author: Jared Tobin <jared@jtobin.io>
Date: Wed, 4 Nov 2026 22:54:04 +0400
bench: init
Criterion benchmarks after those of the taproot-assets SQL-backed tree
store (tapdb/mssmt_bench_test.go): inserting a batch of 100 or 1000
leaves into a fresh store in one transaction, one at a time or
batched, with and without syncing at commit; and proving keys in
stores of 1000, 10000, and 100000 leaves, each in its own read
transaction. Both schemes.
Diffstat:
| A | bench/Main.hs | | | 124 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | mssmt-lmdb.cabal | | | 23 | +++++++++++++++++++++++ |
2 files changed, 147 insertions(+), 0 deletions(-)
diff --git a/bench/Main.hs b/bench/Main.hs
@@ -0,0 +1,124 @@
+{-# OPTIONS_GHC -fno-warn-orphans #-}
+{-# LANGUAGE OverloadedStrings #-}
+
+-- Benchmarks after those of the taproot-assets SQL-backed tree store
+-- (tapdb/mssmt_bench_test.go): inserting a batch of leaves into a fresh
+-- store in one transaction, one at a time or batched, and proving keys
+-- in populated stores, each in its own read transaction.
+
+module Main where
+
+import Control.DeepSeq
+import Control.Exception (evaluate)
+import qualified Crypto.Hash.SHA256 as SHA256
+import qualified Data.Array as A
+import qualified Data.ByteString as BS
+import qualified Data.ByteString.Char8 as B8
+import Data.IORef
+import Data.MSSMT.Internal (Commitment, Hash, Key, Neighbor, Scheme(..))
+import qualified Data.MSSMT.Internal as M
+import qualified Data.MSSMT.LMDB.Internal as I
+import Data.Word (Word64)
+import qualified Database.LMDB as L
+import Criterion.Main
+import System.Directory (getTemporaryDirectory, removeFile)
+import System.IO (hClose, openTempFile)
+
+instance NFData I.Store where
+ rnf = rwhnf
+
+instance NFData Hash where
+ rnf = rwhnf
+
+instance NFData Key where
+ rnf = rwhnf
+
+instance NFData Commitment where
+ rnf = rwhnf
+
+instance NFData Neighbor where
+ rnf = rwhnf
+
+-- n leaves, after tapdb's benchRandLeaves: pseudorandom keys, values of
+-- 1 to 255 bytes, and 32-bit sums
+leaves :: Int -> [(Key, BS.ByteString, Word64)]
+leaves n = fmap leaf [1 .. n] where
+ leaf i =
+ let h = SHA256.hash (B8.pack (show i))
+ v = BS.take (1 + fromIntegral (BS.index h 0) `mod` 255) . BS.concat $
+ [SHA256.hash (h <> B8.pack (show j)) | j <- [0 .. 7 :: Int]]
+ s = fromIntegral (BS.foldl' (\a w -> a * 256 + fromIntegral w)
+ (0 :: Word64) (BS.take 4 h))
+ in case M.key h of
+ Just k -> (k, v, s)
+ Nothing -> error "bang"
+
+-- a fresh store under a scheme, in a temporary file
+fresh :: Bool -> Scheme -> IO (FilePath, I.Store)
+fresh synced sc = do
+ tmp <- getTemporaryDirectory
+ (path, h) <- openTempFile tmp "mssmt-lmdb-bench.mdb"
+ hClose h
+ st <- I.open sc path L.defaultEnvFlags {
+ L.envMapSize = 1024 * 1024 * 1024
+ , L.envNoSubdir = True
+ , L.envNoSync = not synced
+ }
+ pure (path, st)
+
+discard :: (FilePath, I.Store) -> IO ()
+discard (path, st) = do
+ I.close st
+ removeFile path
+ removeFile (path <> "-lock")
+
+main :: IO ()
+main = defaultMain [
+ bgroup "insert, fresh store" [
+ insert_bench synced sc n
+ | n <- [100, 1000], synced <- [True, False], sc <- [Split, Aggregate]
+ ]
+ , bgroup "prove" [
+ prove_bench sc n
+ | n <- [1000, 10000, 100000], sc <- [Split, Aggregate]
+ ]
+ ]
+
+scheme_name :: Scheme -> String
+scheme_name Split = "split"
+scheme_name Aggregate = "aggregate"
+
+insert_bench :: Bool -> Scheme -> Int -> Benchmark
+insert_bench synced sc n = env (pure (leaves n)) $ \ls ->
+ bgroup (scheme_name sc <> ", batch=" <> show n <> sync_name) [
+ bench "one at a time" $ perRunEnvWithCleanup (fresh synced sc) discard $
+ \(_, st) -> L.withWriteTxn (I.store_env st) $ \txn ->
+ mapM_ (\(k, v, s) -> I.insert_in st txn "tree" k v s) ls
+ , bench "batched" $ perRunEnvWithCleanup (fresh synced sc) discard $
+ \(_, st) -> () <$ I.insert_many st "tree" ls
+ ]
+ where
+ sync_name = if synced then "" else ", unsynced"
+
+prove_bench :: Scheme -> Int -> Benchmark
+prove_bench sc n = envWithCleanup populated (discard . fst) $ \ ~(_, ks) ->
+ bench (scheme_name sc <> ", leaves=" <> show n) $
+ whnfIO (next ks)
+ where
+ populated = do
+ fs@(_, st) <- fresh False sc
+ let ls = leaves n
+ _ <- I.insert_many st "tree" ls
+ ref <- newIORef (0 :: Int)
+ let arr = A.listArray (0, n - 1) [k | (k, _, _) <- ls]
+ _ <- evaluate (force arr)
+ pure (fs, (st, arr, ref))
+
+ next (st, arr, ref) = do
+ i <- atomicModifyIORef' ref (\j -> (j + 1, j))
+ let k = arr A.! (i `mod` n)
+ case sc of
+ Split -> I.prove_split st "tree" k >>= evaluate . force
+ >>= \(_, ps) -> pure (length ps)
+ Aggregate -> I.prove_aggregate st "tree" k >>= evaluate . force
+ >>= pure . length
diff --git a/mssmt-lmdb.cabal b/mssmt-lmdb.cabal
@@ -54,3 +54,26 @@ test-suite mssmt-lmdb-tests
, mssmt-lmdb
, tasty
, tasty-quickcheck
+
+benchmark mssmt-lmdb-bench
+ type: exitcode-stdio-1.0
+ default-language: Haskell2010
+ hs-source-dirs: bench
+ main-is: Main.hs
+
+ ghc-options:
+ -rtsopts -O2 -Wall
+ if flag(llvm)
+ ghc-options: -fllvm
+
+ build-depends:
+ array
+ , base
+ , bytestring
+ , criterion
+ , deepseq
+ , directory
+ , mssmt
+ , mssmt-lmdb
+ , ppad-lmdb
+ , ppad-sha256