commit d23eb4112bc87f46d8c68278f691c1156deda52e
parent 0cbf7c84735427e7d32bdfd81d7ca7d0eb082f94
Author: Jared Tobin <jared@jtobin.io>
Date: Wed, 4 Nov 2026 21:34:36 +0400
bench: allocation per operation
mssmt-alloc measures the mean bytes allocated per operation on the
1024-leaf fixture tree, over a thousand distinct inputs so that no
result can be shared between runs, reading GHC.Stats around each
loop. weigh reports zero for most cases against the current library.
Diffstat:
2 files changed, 67 insertions(+), 0 deletions(-)
diff --git a/bench/Alloc.hs b/bench/Alloc.hs
@@ -0,0 +1,47 @@
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RecordWildCards #-}
+
+-- Bytes allocated per operation, measured over distinct inputs so that
+-- no result can be shared between runs. Run with +RTS -s for the
+-- process's totals as well.
+
+module Main where
+
+import Control.DeepSeq (NFData, force)
+import Control.Exception (evaluate)
+import Control.Monad (forM_)
+import Data.MSSMT
+import Fixtures
+import GHC.Stats (allocated_bytes, getRTSStats)
+import Prelude hiding (lookup)
+import System.Mem (performMajorGC)
+import Text.Printf (printf)
+
+-- mean bytes allocated by evaluating f on each input to normal form
+per :: NFData b => (a -> b) -> [a] -> IO Double
+per f xs = do
+ performMajorGC
+ a <- allocated_bytes <$> getRTSStats
+ forM_ xs $ \x -> evaluate (force (f x))
+ performMajorGC
+ b <- allocated_bytes <$> getRTSStats
+ pure (fromIntegral (b - a) / fromIntegral (length xs))
+
+main :: IO ()
+main = do
+ Fixtures {..} <- evaluate (force fixtures)
+ -- the keys in the 1024-leaf fixture tree, and as many not in it
+ ins <- evaluate (force [k | (k, _, _) <- leaves 1024])
+ outs <- evaluate (force [k | (k, _, _) <- drop 1024 (leaves 2048)])
+ proofs <- evaluate $
+ force [(k, v, s, prove k f_tree) | (k, v, s) <- leaves 1024]
+ let run name m = m >>= printf "%-24s %8.0f bytes\n" (name :: String)
+ run "baseline" (per (const ()) ins)
+ run "insert, fresh key" (per (\k -> insert k "v" 1 f_tree) outs)
+ run "insert, replace" (per (\k -> insert k "v" 1 f_tree) ins)
+ run "delete, present" (per (`delete` f_tree) ins)
+ run "lookup, present" (per (`lookup` f_tree) ins)
+ run "prove, inclusion" (per (`prove` f_tree) ins)
+ run "prove, exclusion" (per (`prove` f_tree) outs)
+ run "verify, inclusion" $
+ per (\(k, v, s, p) -> verify k v s p f_root) proofs
diff --git a/mssmt.cabal b/mssmt.cabal
@@ -109,3 +109,23 @@ benchmark mssmt-weigh
, mssmt
, ppad-sha256
, weigh
+
+benchmark mssmt-alloc
+ type: exitcode-stdio-1.0
+ default-language: Haskell2010
+ hs-source-dirs: bench
+ main-is: Alloc.hs
+ other-modules:
+ Fixtures
+
+ ghc-options:
+ -rtsopts -O2 -Wall "-with-rtsopts=-T"
+ if flag(llvm)
+ ghc-options: -fllvm
+
+ build-depends:
+ base
+ , bytestring
+ , deepseq
+ , mssmt
+ , ppad-sha256