commit b354968bd4103177e675f864d3ff47417fafb0a7
parent afa8db9dc35be31df65592809b3872dac3810657
Author: Jared Tobin <jared@jtobin.io>
Date: Wed, 4 Nov 2026 19:17:26 +0400
bench: add criterion and weigh suites
Wall-time (bench/Main.hs) and allocation (bench/Weight.hs) suites for
insert, delete, lookup, prove, verify, and tree construction, over a
shared 1024-leaf fixture (bench/Fixtures.hs). The dev shell now
provides the benchmark dependencies.
Diffstat:
5 files changed, 222 insertions(+), 1 deletion(-)
diff --git a/bench/Fixtures.hs b/bench/Fixtures.hs
@@ -0,0 +1,72 @@
+{-# OPTIONS_GHC -fno-warn-orphans #-}
+
+module Fixtures (
+ Fixtures(..)
+ , fixtures
+ , leaves
+ , build
+ ) where
+
+import Control.DeepSeq
+import Control.Monad (foldM)
+import qualified Crypto.Hash.SHA256 as SHA256
+import qualified Data.ByteString as BS
+import qualified Data.ByteString.Char8 as B8
+import Data.MSSMT
+import Data.Word (Word64)
+
+-- all fields are strict, so WHNF is NF
+instance NFData Key where
+ rnf = rwhnf
+
+instance NFData Pair where
+ rnf = rwhnf
+
+instance NFData Tree where
+ rnf = rwhnf
+
+instance NFData Proof where
+ rnf (Proof ps) = rnf ps
+
+-- | A tree of 1024 leaves, its root, a leaf in it, a key not in it,
+-- and their proofs.
+data Fixtures = Fixtures {
+ f_tree :: !Tree
+ , f_root :: !Pair
+ , f_key :: !Key
+ , f_value :: !BS.ByteString
+ , f_sum :: !Word64
+ , f_proof :: !Proof
+ , f_out :: !Key
+ , f_xproof :: !Proof
+ }
+
+instance NFData Fixtures where
+ rnf (Fixtures t r k v s p o x) =
+ rnf t `seq` rnf r `seq` rnf k `seq` rnf v `seq` rnf s
+ `seq` rnf p `seq` rnf o `seq` rnf x
+
+fixtures :: Fixtures
+fixtures = Fixtures t (root t) k v s (prove k t) o (prove o t) where
+ t = build (leaves 1024)
+ (k, v, s) = leaf_of 1
+ o = key_of 0
+
+-- | n distinct leaves.
+leaves :: Int -> [(Key, BS.ByteString, Word64)]
+leaves n = fmap leaf_of [1 .. n]
+
+leaf_of :: Int -> (Key, BS.ByteString, Word64)
+leaf_of i =
+ (key_of i, SHA256.hash (B8.pack ('v' : show i)), fromIntegral i)
+
+key_of :: Int -> Key
+key_of i = case key (SHA256.hash (B8.pack (show i))) of
+ Just k -> k
+ Nothing -> error "bang"
+
+-- | Build a tree from leaves.
+build :: [(Key, BS.ByteString, Word64)] -> Tree
+build ls = case foldM (\t (k, v, s) -> insert k v s t) empty ls of
+ Just t -> t
+ Nothing -> error "bang"
diff --git a/bench/Main.hs b/bench/Main.hs
@@ -0,0 +1,56 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RecordWildCards #-}
+
+module Main where
+
+import Criterion.Main
+import Data.MSSMT
+import Fixtures
+import Prelude hiding (lookup)
+
+main :: IO ()
+main = defaultMain [
+ env (pure fixtures) $ \ ~f -> bgroup "mssmt" [
+ bench_insert f
+ , bench_delete f
+ , bench_lookup f
+ , bench_prove f
+ , bench_verify f
+ , bench_build
+ ]
+ ]
+
+bench_insert :: Fixtures -> Benchmark
+bench_insert ~Fixtures {..} = bgroup "insert" [
+ bench "empty tree" $ nf (insert f_out "v" 1) empty
+ , bench "1024 leaves, fresh key" $ nf (insert f_out "v" 1) f_tree
+ , bench "1024 leaves, replace" $ nf (insert f_key "v" 1) f_tree
+ ]
+
+bench_delete :: Fixtures -> Benchmark
+bench_delete ~Fixtures {..} = bgroup "delete" [
+ bench "1024 leaves, present" $ nf (delete f_key) f_tree
+ , bench "1024 leaves, absent" $ nf (delete f_out) f_tree
+ ]
+
+bench_lookup :: Fixtures -> Benchmark
+bench_lookup ~Fixtures {..} = bgroup "lookup" [
+ bench "1024 leaves, present" $ nf (lookup f_key) f_tree
+ , bench "1024 leaves, absent" $ nf (lookup f_out) f_tree
+ ]
+
+bench_prove :: Fixtures -> Benchmark
+bench_prove ~Fixtures {..} = bgroup "prove" [
+ bench "1024 leaves, inclusion" $ nf (prove f_key) f_tree
+ , bench "1024 leaves, exclusion" $ nf (prove f_out) f_tree
+ ]
+
+bench_verify :: Fixtures -> Benchmark
+bench_verify ~Fixtures {..} = bgroup "verify" [
+ bench "inclusion" $ nf (verify f_key f_value f_sum f_proof) f_root
+ , bench "exclusion" $ nf (verify f_out mempty 0 f_xproof) f_root
+ ]
+
+bench_build :: Benchmark
+bench_build = env (pure (leaves 64)) $ \ls ->
+ bench "build (64 leaves)" $ nf build ls
diff --git a/bench/Weight.hs b/bench/Weight.hs
@@ -0,0 +1,50 @@
+{-# LANGUAGE BangPatterns #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RecordWildCards #-}
+
+module Main where
+
+import Control.DeepSeq
+import Data.MSSMT
+import Fixtures
+import Prelude hiding (lookup)
+import Weigh
+
+-- note that 'weigh' doesn't work properly in a repl
+main :: IO ()
+main = do
+ let !f = force fixtures
+ !ls = force (leaves 64)
+ mainWith $ do
+ insert_ f
+ delete_ f
+ lookup_ f
+ prove_ f
+ verify_ f
+ func "build (64 leaves)" build ls
+
+insert_ :: Fixtures -> Weigh ()
+insert_ Fixtures {..} = wgroup "insert" $ do
+ func "empty tree" (insert f_out "v" 1) empty
+ func "1024 leaves, fresh key" (insert f_out "v" 1) f_tree
+ func "1024 leaves, replace" (insert f_key "v" 1) f_tree
+
+delete_ :: Fixtures -> Weigh ()
+delete_ Fixtures {..} = wgroup "delete" $ do
+ func "1024 leaves, present" (delete f_key) f_tree
+ func "1024 leaves, absent" (delete f_out) f_tree
+
+lookup_ :: Fixtures -> Weigh ()
+lookup_ Fixtures {..} = wgroup "lookup" $ do
+ func "1024 leaves, present" (lookup f_key) f_tree
+ func "1024 leaves, absent" (lookup f_out) f_tree
+
+prove_ :: Fixtures -> Weigh ()
+prove_ Fixtures {..} = wgroup "prove" $ do
+ func "1024 leaves, inclusion" (prove f_key) f_tree
+ func "1024 leaves, exclusion" (prove f_out) f_tree
+
+verify_ :: Fixtures -> Weigh ()
+verify_ Fixtures {..} = wgroup "verify" $ do
+ func "inclusion" (verify f_key f_value f_sum f_proof) f_root
+ func "exclusion" (verify f_out mempty 0 f_xproof) f_root
diff --git a/flake.nix b/flake.nix
@@ -32,6 +32,7 @@
lib = "mssmt";
pkgs = import nixpkgs { inherit system; };
+ hlib = pkgs.haskell.lib;
hpkgs = pkgs.haskell.packages.ghc910.extend (new: old: {
ppad-base16 = ppad-base16.packages.${system}.default;
@@ -49,12 +50,16 @@
packages.default = hpkgs.${lib};
devShells.default = hpkgs.shellFor {
- packages = p: [ p.${lib} ];
+ packages = p: [
+ (hlib.doBenchmark p.${lib})
+ ];
buildInputs = [
cabal
];
+ doBenchmark = true;
+
shellHook = ''
PS1="[${lib}] \w$ "
echo "entering ${system} shell, using"
diff --git a/mssmt.cabal b/mssmt.cabal
@@ -52,3 +52,41 @@ test-suite mssmt-tests
, tasty
, tasty-hunit
, tasty-quickcheck
+
+benchmark mssmt-bench
+ type: exitcode-stdio-1.0
+ default-language: Haskell2010
+ hs-source-dirs: bench
+ main-is: Main.hs
+ other-modules:
+ Fixtures
+
+ ghc-options:
+ -rtsopts -O2 -Wall
+
+ build-depends:
+ base
+ , bytestring
+ , criterion
+ , deepseq
+ , mssmt
+ , ppad-sha256
+
+benchmark mssmt-weigh
+ type: exitcode-stdio-1.0
+ default-language: Haskell2010
+ hs-source-dirs: bench
+ main-is: Weight.hs
+ other-modules:
+ Fixtures
+
+ ghc-options:
+ -rtsopts -O2 -Wall
+
+ build-depends:
+ base
+ , bytestring
+ , deepseq
+ , mssmt
+ , ppad-sha256
+ , weigh