Skip to content

Commit 44e5404

Browse files
authored
Tidy up benchmarks a bit (#5209)
* Tidy up benchmarks a bit - Compute evaluation context once and share between benchmarks more simply. - Use simpler way of computing the evaluation context since we're not benchmarking it. - Add some missing strictness to other benchmarks to check that they are measuring things correctly. * Try this?
1 parent d37445b commit 44e5404

File tree

6 files changed

+35
-34
lines changed

6 files changed

+35
-34
lines changed

plutus-benchmark/plutus-benchmark.cabal

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ benchmark validation
272272
, plutus-benchmark-common
273273
, plutus-core ^>=1.3
274274
, plutus-ledger-api ^>=1.3
275-
, transformers
276275

277276
---------------- validation-decode ----------------
278277

@@ -286,14 +285,14 @@ benchmark validation-decode
286285
, base >=4.9 && <5
287286
, bytestring
288287
, criterion >=1.5.9.0
288+
, deepseq
289289
, directory
290290
, filepath
291291
, flat <0.5
292292
, optparse-applicative
293293
, plutus-benchmark-common
294294
, plutus-core ^>=1.3
295295
, plutus-ledger-api ^>=1.3
296-
, transformers
297296

298297
---------------- validation-full ----------------
299298

@@ -307,14 +306,14 @@ benchmark validation-full
307306
, base >=4.9 && <5
308307
, bytestring
309308
, criterion >=1.5.9.0
309+
, deepseq
310310
, directory
311311
, filepath
312312
, flat <0.5
313313
, optparse-applicative
314314
, plutus-benchmark-common
315315
, plutus-core ^>=1.3
316316
, plutus-ledger-api:{plutus-ledger-api, plutus-ledger-api-testlib} ^>=1.3
317-
, transformers
318317

319318
---------------- Cek cost model calibration ----------------
320319

plutus-benchmark/validation/BenchCek.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import UntypedPlutusCore as UPLC
1717
`cabal bench -- plutus-benchmark:validation --benchmark-options crowdfunding`.
1818
-}
1919
main :: IO ()
20-
main = evaluate (force getEvalCtx) *> benchWith mkCekBM
21-
where
22-
mkCekBM file program =
20+
main = do
21+
evalCtx <- evaluate (force mkEvalCtx)
22+
let mkCekBM file program =
2323
-- don't count the undebruijn . unflat cost
2424
-- `force` to try to ensure that deserialiation is not included in benchmarking time.
2525
let !nterm = force (toNamedDeBruijnTerm $ UPLC._progTerm $ unsafeUnflat file program)
26-
in whnf evaluateCekLikeInProd nterm
26+
in whnf (evaluateCekLikeInProd evalCtx) nterm
27+
benchWith mkCekBM

plutus-benchmark/validation/BenchDec.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
{-# LANGUAGE BangPatterns #-}
12
module Main where
23

34
import PlutusLedgerApi.V1
45
import UntypedPlutusCore qualified as UPLC
56

67
import Common
8+
import Control.DeepSeq (force)
79
import Control.Exception
810
import Criterion
911
import Data.ByteString as BS
@@ -32,7 +34,8 @@ main = benchWith mkDecBM
3234
(unsaturated, _args) = peelDataArguments fullyApplied
3335

3436
-- we then have to re-encode and serialise it
35-
benchScript :: SerialisedScript = serialiseUPLC $ UPLC.Program () v unsaturated
37+
!(benchScript :: SerialisedScript) =
38+
force (serialiseUPLC $ UPLC.Program () v unsaturated)
3639

3740
-- Deserialize using 'FakeNamedDeBruijn' to get the fake names added
3841
in whnf (either throw id . assertScriptWellFormed (ProtocolVersion 6 0)

plutus-benchmark/validation/BenchFull.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE BangPatterns #-}
12
module Main where
23

34
import PlutusCore.Evaluation.Machine.ExBudget
@@ -6,6 +7,7 @@ import PlutusLedgerApi.V1
67
import UntypedPlutusCore qualified as UPLC
78

89
import Common
10+
import Control.DeepSeq (force)
911
import Criterion
1012
import Data.ByteString as BS
1113
import Data.Either
@@ -37,7 +39,7 @@ main = benchWith mkFullBM
3739
(term, args) = peelDataArguments body
3840

3941
-- strictify and "short" the result cbor to create a real `SerialisedScript`
40-
benchScript :: SerialisedScript = serialiseUPLC $ UPLC.Program () v term
42+
!(benchScript :: SerialisedScript) = force (serialiseUPLC $ UPLC.Program () v term)
4143

4244
in whnf (\ script ->
4345
(isRight $ snd $ evaluateScriptRestricting

plutus-benchmark/validation/Common.hs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@
33
module Common (
44
benchWith
55
, unsafeUnflat
6-
, getEvalCtx
6+
, mkEvalCtx
77
, evaluateCekLikeInProd
88
, peelDataArguments
99
, Term
1010
) where
1111

12-
import PlutusPrelude
13-
1412
import PlutusBenchmark.Common (getConfig, getDataDir)
1513
import PlutusBenchmark.NaturalSort
1614

1715
import PlutusCore qualified as PLC
1816
import PlutusCore.Builtin qualified as PLC
1917
import PlutusCore.Data qualified as PLC
18+
import PlutusCore.Default qualified as PLC (BuiltinVersion (DefaultFunV1))
2019
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults qualified as PLC
21-
import PlutusCore.Evaluation.Result
22-
import PlutusLedgerApi.Common (PlutusLedgerLanguage (PlutusV1), evaluateTerm)
23-
import PlutusLedgerApi.Common.Versions (ledgerLanguageIntroducedIn)
24-
import PlutusLedgerApi.V1 (EvaluationContext, ParamName, VerboseMode (..), mkEvaluationContext)
20+
import PlutusLedgerApi.Common (PlutusLedgerLanguage (PlutusV1), evaluateTerm,
21+
ledgerLanguageIntroducedIn, mkDynEvaluationContext)
22+
import PlutusLedgerApi.V1 (EvaluationContext, VerboseMode (..))
2523
import UntypedPlutusCore qualified as UPLC
2624
import UntypedPlutusCore.Evaluation.Machine.Cek qualified as UPLC
2725

@@ -30,8 +28,6 @@ import Criterion.Main.Options (Mode, parseWith)
3028
import Criterion.Types (Config (..))
3129
import Options.Applicative
3230

33-
import Control.Monad.Trans.Except
34-
import Control.Monad.Trans.Writer.Strict
3531
import Data.ByteString qualified as BS
3632
import Data.List (isPrefixOf)
3733
import Flat
@@ -136,28 +132,26 @@ benchWith act = do
136132
env (BS.readFile $ dir </> file) $ \scriptBS ->
137133
bench (dropExtension file) $ act file scriptBS
138134

139-
getEvalCtx
140-
:: Either
141-
(UPLC.CekEvaluationException UPLC.NamedDeBruijn UPLC.DefaultUni UPLC.DefaultFun)
142-
EvaluationContext
143-
getEvalCtx = do
144-
costParams <-
145-
maybe
146-
(Left evaluationFailure)
147-
(Right . take (length $ enumerate @ParamName) . toList)
148-
PLC.defaultCostModelParams
149-
either (const $ Left evaluationFailure) (Right . fst) . runExcept . runWriterT $
150-
mkEvaluationContext costParams
151-
{-# NOINLINE getEvalCtx #-}
135+
-- | Create the evaluation context for the benchmarks. This doesn't exactly match how it's done
136+
-- on-chain, but that's okay because the evaluation context is cached by the ledger, so we're
137+
-- deliberately not including it in the benchmarks.
138+
mkEvalCtx :: EvaluationContext
139+
mkEvalCtx =
140+
case PLC.defaultCostModelParams of
141+
-- The validation benchmarks were all created from PlutusV1 scripts
142+
Just p -> case mkDynEvaluationContext PLC.DefaultFunV1 p of
143+
Right ec -> ec
144+
Left err -> error $ show err
145+
Nothing -> error "Couldn't get cost model params"
152146

153147
-- | Evaluate a term as it would be evaluated using the on-chain evaluator.
154148
evaluateCekLikeInProd
155-
:: UPLC.Term PLC.NamedDeBruijn PLC.DefaultUni PLC.DefaultFun ()
149+
:: EvaluationContext
150+
-> UPLC.Term PLC.NamedDeBruijn PLC.DefaultUni PLC.DefaultFun ()
156151
-> Either
157152
(UPLC.CekEvaluationException UPLC.NamedDeBruijn UPLC.DefaultUni UPLC.DefaultFun)
158153
(UPLC.Term UPLC.NamedDeBruijn UPLC.DefaultUni UPLC.DefaultFun ())
159-
evaluateCekLikeInProd term = do
160-
evalCtx <- getEvalCtx
154+
evaluateCekLikeInProd evalCtx term = do
161155
let (getRes, _, _) =
162156
-- The validation benchmarks were all created from PlutusV1 scripts
163157
evaluateTerm UPLC.restrictingEnormous (ledgerLanguageIntroducedIn PlutusV1) Quiet evalCtx term

plutus-ledger-api/src/PlutusLedgerApi/Common.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module PlutusLedgerApi.Common
2828
, Version (..)
2929
, builtinsIntroducedIn
3030
, builtinsAvailableIn
31+
, ledgerLanguageIntroducedIn
32+
, ledgerLanguagesAvailableIn
3133

3234
-- * Network's costing parameters
3335
{-| A less drastic approach (that does not rely on a HF)

0 commit comments

Comments
 (0)