Skip to content

Commit 62d1046

Browse files
committed
Randomize positions of amounts
1 parent 8d65e18 commit 62d1046

File tree

1 file changed

+36
-14
lines changed
  • plutus-core/cost-model/budgeting-bench/Benchmarks

1 file changed

+36
-14
lines changed

plutus-core/cost-model/budgeting-bench/Benchmarks/Values.hs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Prelude
1111

1212
import Common
1313
import Control.Monad (replicateM)
14+
import Control.Monad.State.Strict (State)
1415
import Criterion.Main (Benchmark)
1516
import Data.Bits (shiftR, (.&.))
1617
import Data.ByteString (ByteString)
@@ -25,7 +26,7 @@ import PlutusCore.Evaluation.Machine.ExMemoryUsage (ValueLogOuterSizeAddLogMaxIn
2526
ValueTotalSize (..))
2627
import PlutusCore.Value (K, Quantity (..), Value)
2728
import PlutusCore.Value qualified as Value
28-
import System.Random.Stateful (StatefulGen, StdGen, runStateGen_, uniformRM)
29+
import System.Random.Stateful (StateGenM, StatefulGen, StdGen, runStateGen_, uniformRM)
2930

3031
----------------------------------------------------------------------------------------------------
3132
-- Benchmarks --------------------------------------------------------------------------------------
@@ -48,10 +49,10 @@ lookupCoinBenchmark gen =
4849
(id, id, ValueLogOuterSizeAddLogMaxInnerSize) -- Wrap Value argument to report sum of log sizes
4950
LookupCoin -- the builtin fun
5051
[] -- no type arguments needed (monomorphic builtin)
51-
(lookupCoinArgs gen) -- the argument combos to generate benchmarks for
52+
(runBenchGen gen lookupCoinArgs) -- the argument combos to generate benchmarks for
5253

53-
lookupCoinArgs :: StdGen -> [(ByteString, ByteString, Value)]
54-
lookupCoinArgs gen = runStateGen_ gen \(g :: g) -> do
54+
lookupCoinArgs :: (StatefulGen g m) => g -> m [(ByteString, ByteString, Value)]
55+
lookupCoinArgs gen = do
5556
{- Exhaustive power-of-2 combinations for BST worst-case benchmarking.
5657
5758
Tests all combinations of sizes from powers and half-powers of 2.
@@ -82,7 +83,7 @@ lookupCoinArgs gen = runStateGen_ gen \(g :: g) -> do
8283

8384
sequence
8485
-- Generate worst-case lookups for each size combination
85-
[ withWorstCaseSearchKeys (generateConstrainedValueWithMaxPolicy numPolicies tokensPerPolicy g)
86+
[ withWorstCaseSearchKeys (generateConstrainedValueWithMaxPolicy numPolicies tokensPerPolicy gen)
8687
| numPolicies <- sizes
8788
, tokensPerPolicy <- sizes
8889
]
@@ -221,15 +222,31 @@ insertCoinBenchmark gen =
221222
(id, id, id, ValueLogOuterSizeAddLogMaxInnerSize)
222223
InsertCoin
223224
[]
224-
(insertCoinArgs gen)
225-
226-
insertCoinArgs :: StdGen -> [(ByteString, ByteString, Integer, Value)]
227-
insertCoinArgs gen =
228-
let lookupArgs = lookupCoinArgs gen
229-
noOfBenchs = length lookupArgs
230-
maxAmounts = replicate (noOfBenchs `div` 2) (unQuantity maxBound)
231-
zeroAmounts = replicate (noOfBenchs `div` 2) (0 :: Integer)
232-
in (\((b1, b2, v), a) -> (b1, b2, a, v)) <$> zip lookupArgs (maxAmounts <> zeroAmounts)
225+
(runBenchGen gen insertCoinArgs)
226+
227+
insertCoinArgs :: (StatefulGen g m) => g -> m [(ByteString, ByteString, Integer, Value)]
228+
insertCoinArgs gen = do
229+
lookupArgs <- lookupCoinArgs gen
230+
let noOfBenchs = length lookupArgs
231+
amounts <- genZeroOrMaxAmount gen noOfBenchs
232+
pure $ reorderArgs <$> zip lookupArgs amounts
233+
where
234+
reorderArgs ((b1, b2, val), am) = (b1, b2, am, val)
235+
236+
-- | Generate either zero or maximum amount Integer values, the probability of each is 50%
237+
genZeroOrMaxAmount
238+
:: (StatefulGen g m)
239+
=> g
240+
-> Int
241+
-- ^ Number of amounts to generate
242+
-> m [Integer]
243+
genZeroOrMaxAmount gen n =
244+
replicateM n $ do
245+
coinType <- uniformRM (0 :: Int, 1) gen
246+
pure $ case coinType of
247+
0 -> 0
248+
1 -> unQuantity maxBound
249+
_ -> error "genZeroOrMaxAmount: impossible"
233250

234251
----------------------------------------------------------------------------------------------------
235252
-- Value Generators --------------------------------------------------------------------------------
@@ -380,3 +397,8 @@ unsafeFromBuiltinResult = \case
380397
BuiltinSuccess x -> x
381398
BuiltinSuccessWithLogs _ x -> x
382399
BuiltinFailure _ err -> error $ "BuiltinResult failed: " <> show err
400+
401+
-- | Abstracted runner for computations using stateful random generator 'StdGen'
402+
runBenchGen :: StdGen -> (StateGenM StdGen -> State StdGen a) -> a
403+
runBenchGen gen ma = runStateGen_ gen \g -> ma g
404+

0 commit comments

Comments
 (0)