-
Notifications
You must be signed in to change notification settings - Fork 496
BuiltinValue: insertCoin and unionValue and scaleValue costing #7384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ana-pantilie
wants to merge
5
commits into
master
Choose a base branch
from
ana/value-costing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0429991
Add insertCoin and unionValue costing skeleton
ana-pantilie 5033d01
Tmp: add benchmark generators
ana-pantilie 551af6a
Add support for unique keys, WIP benchmarking
ana-pantilie 4c71846
Fix
ana-pantilie 52d9d21
Fix
ana-pantilie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
plutus-core/cost-model/budgeting-bench/Benchmarks/Values.hs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| module Benchmarks.Values ( | ||
| makeBenchmarks, | ||
| ) where | ||
|
|
||
| import Prelude | ||
|
|
||
| import Common | ||
|
|
||
| import PlutusCore (DefaultFun (InsertCoin, LookupCoin, UnValueData, ValueContains, ValueData)) | ||
| import PlutusCore.Value (Value) | ||
| import PlutusCore.Value qualified as Value | ||
|
|
||
| import Control.Monad.Trans.Class (lift) | ||
| import Control.Monad.Trans.State.Strict (State, StateT, evalState, gets, modify) | ||
| import Criterion.Main (Benchmark) | ||
| import Data.ByteString (ByteString) | ||
| import Data.ByteString qualified as BS | ||
| import Data.Text qualified as Text | ||
| import Data.Text.Encoding (encodeUtf8) | ||
| import Data.Word (Word8) | ||
| import System.Random.Stateful (StateGenM, StatefulGen, StdGen, UniformRange (uniformRM), | ||
| runStateGenT_, uniformByteStringM) | ||
|
|
||
| makeBenchmarks :: StdGen -> [Benchmark] | ||
| makeBenchmarks gen = | ||
| [ benchInsertCoin gen | ||
| -- , benchUnionValue gen | ||
| ] | ||
|
|
||
| newtype PolicyId = PolicyId ByteString | ||
| newtype TokenName = TokenName ByteString | ||
| newtype Amount = Amount Integer | ||
| type Counter = Integer | ||
|
|
||
| data GenState = GenState | ||
| { policyIdCounter :: Counter | ||
| , tokenNameCounter :: Counter | ||
| } | ||
|
|
||
| type BenchState = StateT StdGen (State GenState) | ||
|
|
||
| -- | An insertCoin benchmark is a concrete set of arguments we apply to the | ||
| -- InsertCoin builtin function to measure its runtime cost. | ||
| data InsertCoinBenchmark = InsertCoinBenchmark | ||
| { icPolicyId :: PolicyId | ||
| , icTokenName :: TokenName | ||
| , icAmount :: Amount | ||
| , icValue :: Value | ||
| } | ||
|
|
||
| icToRawTuple :: InsertCoinBenchmark -> (ByteString, ByteString, Integer, Value) | ||
| icToRawTuple (InsertCoinBenchmark (PolicyId p) (TokenName t) (Amount a) v) = (p, t, a, v) | ||
|
|
||
| benchInsertCoin :: StdGen -> Benchmark | ||
| benchInsertCoin gen = | ||
| createFourTermBuiltinBenchElementwiseWithWrappers | ||
| (id, id, id, id) -- TODO: use proper wrappers | ||
| InsertCoin | ||
| [] | ||
| (icToRawTuple <$> insertCoinBenchGen gen) | ||
|
|
||
| -- | Generate a set of benchmarks for the InsertCoin builtin function. | ||
| -- It includes the following scenarios: | ||
| -- 1. Inserting into an empty Value. | ||
| -- 2. Inserting a new TokenName into an existing PolicyId. Randomly extracting a PolicyId from the Value. | ||
| -- 3. Inserting into an existing TokenName. Randomly extracting a (PolicyId, TokenName) pair from the Value. | ||
| -- 4. Inserting a new PolicyId. | ||
| -- 5. Deleting a TokenName by inserting a 0 amount. Randomly extracting a (PolicyId, TokenName) pair from the Value. | ||
| -- 6. Deleting a PolicyId by inserting a 0 amount into its last TokenName. Should generate a Value with multiple such PolicyIds, and randomly picking which PolicyId to delete. | ||
| -- We're interested in the worst case performance, so we'll use the largest key values possible. | ||
| -- We should also run randomized benchmarks, where we insert random values into random Values. | ||
| -- We actually want to see how the performance scales with the size of the Value, so we should generate Values of varying sizes. | ||
| -- We want to make sure we are also hitting the worst case scenarios and various edge cases. | ||
| insertCoinBenchGen | ||
| :: StdGen | ||
| -> [InsertCoinBenchmark] | ||
| insertCoinBenchGen g = flip evalState (GenState 0 0) $ runStateGenT_ g $ \gen -> do | ||
| policyId <- newPolicyId gen | ||
| tokenName <- newTokenName gen | ||
| amount <- uniformAmount gen | ||
| let emptyValueBench = InsertCoinBenchmark policyId tokenName amount Value.empty | ||
| pure [emptyValueBench] | ||
|
|
||
| -- | Generate a unique PolicyId on a uniform distribution. Note that the size of the | ||
| -- generated bytestring is going to be larger than Value.maxKeyLen, because we | ||
| -- append a counter integer to ensure uniqueness. This is acceptable for benchmarking | ||
| -- purposes, as we're interested in the worst-case performance. | ||
| newPolicyId :: StateGenM StdGen -> BenchState PolicyId | ||
| newPolicyId gen = do | ||
| bs <- uniformByteStringM Value.maxKeyLen gen | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's better to fix the first 30 bytes, and randomize the last 2 bytes. That aligns better with worst case. I think that's what Yura's generator does, so you can probably reuse that. |
||
| c <- lift $ gets policyIdCounter | ||
| let newbs = BS.append bs (encodeUtf8 . Text.pack . show $ c) | ||
| lift $ modify $ \s -> s { policyIdCounter = c + 1 } | ||
| pure $ PolicyId newbs | ||
|
|
||
| -- | Generate a unique TokenName on a uniform distribution. Note that the size of the | ||
| -- generated bytestring is going to be larger than Value.maxKeyLen, because we | ||
| -- append a counter integer to ensure uniqueness. This is acceptable for benchmarking | ||
| -- purposes, as we're interested in the worst-case performance. | ||
| -- Actually, this wouldn't be acceptable if we were to measure based on the size of the | ||
| -- keys, because we would want to view how key size affects performance! | ||
| newTokenName :: StateGenM StdGen -> BenchState TokenName | ||
| newTokenName gen = do | ||
| bs <- uniformByteStringM Value.maxKeyLen gen | ||
| c <- lift $ gets tokenNameCounter | ||
| let newbs = BS.append bs (encodeUtf8 . Text.pack . show $ c) | ||
| lift $ modify $ \s -> s { tokenNameCounter = c + 1 } | ||
| pure $ TokenName newbs | ||
|
|
||
| uniformAmount :: StateGenM StdGen -> BenchState Amount | ||
| uniformAmount gen = | ||
| Amount <$> uniformRM (0, 100) gen -- TODO: tweak the range | ||
|
|
||
| newValue :: StateGenM StdGen -> BenchState Value | ||
| newValue gen = undefined | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't it make
insertCoinfail? It enforces key lengths.