-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/input-output-hk/plutus in…
…to effectfully/builtins/both-MajorProtocolVersion-and-multiple-CostModels
- Loading branch information
Showing
155 changed files
with
1,796 additions
and
898 deletions.
There are no files selected for viewing
This file contains 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 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,128 @@ | ||
.. highlight:: haskell | ||
.. _asdata: | ||
|
||
How to use ``AsData`` to optimize scripts | ||
========================================= | ||
|
||
The Plutus libraries contain a ``PlutusTx.asData`` module that contains Template Haskell (TH) code for encoding algebraic data types (ADTs) as ``Data`` objects in Plutus Core, as opposed to sums-of-products terms. | ||
In general, ``asData`` pushes the burden of a computation nearer to where a value is used, in a crude sense making the evaluation less strict and more lazy. | ||
This is intended for expert Plutus developers. | ||
|
||
Purpose | ||
------- | ||
|
||
Values stored in datums or redeemers need to be encoded into ``Data`` objects. | ||
When writing and optimizing a Plutus script, one of the challenges is finding the right approach to handling ``Data`` objects and how expensive that method will be. | ||
To make an informed decision, you may need to benchmark and profile your smart contract code to measure its actual resource consumption. | ||
The primary purpose of ``asData`` is to give you more options for how you want to handle ``Data``. | ||
|
||
Choice of two approoaches | ||
------------------------- | ||
|
||
When handling ``Data`` objects, you have a choice of two pathways. | ||
It is up to you to determine which pathway to use depending on your particular use case. | ||
There are trade offs in performance and where errors occur. | ||
|
||
Approach one: proactively do all of the parsing | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The first approach is to parse the object immediately (using ``fromBuiltinData``) into a native Plutus Core datatype, which will also identify any problems with the structuring of the object. | ||
However, this performs all the work up front. | ||
|
||
This is the normal style that has been promoted in the past. | ||
|
||
Approach two: only do the parsing if and when necessary | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
In the second approach, the script doesn't do any parsing work immediately, and instead does it later, when it needs to. | ||
It might be that this saves you a lot of work, because you may never need to parse the entire object. | ||
Instead, the script will just carry the item around as a ``Data`` object. | ||
|
||
Using this method, every time the script uses the object, it will look at it to find out if it has the right shape. | ||
If it does have the right shape, it will deconstruct the ``Data`` object and do its processing; if not, it will throw an error. | ||
This work may be repeated depending on how your script is written. | ||
In some cases, you might do less work, in some cases you might do more work, depending on your specific use case. | ||
|
||
The Plutus Tx library provides some helper functions to make this second style easier to do, in the form of the ``asData`` function. | ||
|
||
Using ``asData`` | ||
------------------ | ||
|
||
The ``asData`` function takes the definition of a data type and replaces it with an equivalent definition whose representation uses ``Data`` directly. | ||
|
||
For example, if we wanted to use it on the types from the :ref:`auction example<simple_example>`, we would put the datatype declarations inside a Template Haskell quote and call ``asData`` on it. | ||
|
||
.. literalinclude:: ../tutorials/AuctionValidator.hs | ||
:start-after: BLOCK9 | ||
:end-before: BLOCK10 | ||
|
||
This is normal Template Haskell that just generates new Haskell source, so you can see the code that it generates with `{-# OPTIONS_GHC -ddump-splices #-}`, but it will look something like this: | ||
|
||
.. code-block:: | ||
PlutusTx.asData | ||
[d| data Bid' | ||
= Bid' {bBidder' :: PubKeyHash, bAmount' :: Lovelace} | ||
deriving newtype (Eq, Ord, ToBuitinData, FromBuiltinData, UnsafeFromBuiltinData) | ||
data AuctionRedeemer' = NewBid' Bid | Payout' | ||
deriving newtype (Eq, Ord, ToBuitinData, FromBuiltinData, UnsafeFromBuiltinData) |] | ||
======> | ||
newtype Bid' = Bid'2 BuiltinData | ||
deriving newtype (Eq, Ord, PlutusTx.ToData, FromData, UnsafeFromData) | ||
{-# COMPLETE Bid' #-} | ||
pattern Bid' :: PubKeyHash -> Lovelace -> Bid' | ||
pattern Bid' ... | ||
newtype AuctionRedeemer' = AuctionRedeemer'2 BuiltinData | ||
deriving newtype (Eq, Ord, PlutusTx.ToData, FromData, UnsafeFromData) | ||
{-# COMPLETE NewBid', Payout' #-} | ||
pattern NewBid' :: Bid -> AuctionRedeemer' | ||
pattern NewBid' ... | ||
pattern Payout' :: AuctionRedeemer' | ||
pattern Payout' ... | ||
That is: | ||
|
||
- It creates a newtype wrapper around ``BuiltinData`` | ||
- It creates pattern synonyms corresponding to each of the constructors you wrote | ||
|
||
This lets you write code "as if" you were using the original declaration that you wrote, while in fact the pattern synonyms are handling conversion to/from ``Data`` for you. | ||
But any values of this type actually are represented with ``Data``. | ||
That means that when we newtype-derive the instances for converting to and from ``Data`` we get the instances for ``BuiltinData`` - which are free! | ||
|
||
Nested fields | ||
~~~~~~~~~~~~~ | ||
|
||
The most important caveat to using ``asData`` is that ``Data`` objects encoding datatypes must also encode the *fields* of the datatype as ``Data``. | ||
However, ``asData`` tries to make the generated code a drop-in replacement for the original code, which means that when using the pattern synonyms they try to give you the fields as they were originally defined, which means *not* encoded as ``Data``. | ||
|
||
For example, in the ``Bid`` case above the ``bAmount`` field is originally defined to have type ``Lovelace`` which is a newtype around a Plutus Core builtin integer. | ||
However, since we are using ``asData``, we need to encode the field into ``Data`` in order to store it. | ||
That means that when you construct a ``Bid`` object you must take the ``Integer`` that you start with and convert it to ``Data``, and when you pattern match on a ``Bid`` object you do the reverse conversion. | ||
|
||
These conversions are potentially expensive! | ||
If the ``bAmount`` field was a complex data structure, then every time we constructed or deconstructed a ``Bid`` object we would need to convert that datastructure to or from ``Data``. | ||
Whether or not this is a problem depends on the precise situation, but in general: | ||
|
||
- If the field is a builtin integer or bytestring or a wrapper around those, it is probably cheap | ||
- If the field is a datatype which is itself defined with ``asData`` then it is free (since it's already ``Data``!) | ||
- If the field is a complex or large datatype then it is potentially expensive | ||
|
||
Therefore ``asData`` tends to work best when you use it for a type and also for all the types of its fields. | ||
|
||
Choosing an approach | ||
-------------------- | ||
|
||
There are a number of tradeoffs to consider: | ||
|
||
1. Plutus Tx's datatypes are faster to work with and easier to optimize than ``Data``, so if the resulting object is going to be processed in its entirety (or have parts of it repeatedly processed) then it can be better to parse it up-front. | ||
2. If it is important to check that the entire structure is well-formed, then it is better to parse it up-front, since the conversion will check the entire structure for well-formedness immediately, rather than checking only the parts that are used when they are used. | ||
3. If you do not want to use ``asData`` for the types of the fields, then it may be better to not use it at all in order to avoid conversion penalties at the use sites. | ||
|
||
Which approach is better is an empirical question and may vary in different cases. | ||
A single script may wish to use different approaches in different places. | ||
For example, your datum might contain a large state object which is usually only inspected in part (a good candidate for ``asData``), whereas your redeemer might be a small object which is inspected frequently to determine what to do (a good candidate for a native Plutus Tx datatype). |
This file contains 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 |
---|---|---|
|
@@ -7,5 +7,6 @@ How-to guides | |
:maxdepth: 3 | ||
:titlesonly: | ||
|
||
asdata | ||
exporting-a-script | ||
profiling-scripts |
This file contains 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 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 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 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
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0000020002010200020101020201000100010001020101020201010000020102.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 401412988 | ||
| mem: 1451263}) | ||
({cpu: 400791988 | ||
| mem: 1448563}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0001000101000000010101000001000001010101010100000001000001010000.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 519999134 | ||
| mem: 1821564}) | ||
({cpu: 519677134 | ||
| mem: 1820164}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0003040402030103010203030303000200000104030002040304020400000102.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 1368245687 | ||
| mem: 5135402}) | ||
({cpu: 1367463687 | ||
| mem: 5132002}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/004025fd712d6c325ffa12c16d157064192992faf62e0b991d7310a2f91666b8.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 971935150 | ||
| mem: 3459445}) | ||
({cpu: 970348150 | ||
| mem: 3452545}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0101010001010101010101000100010100000001010000010001000001000101.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 842825112 | ||
| mem: 2477998}) | ||
({cpu: 842043112 | ||
| mem: 2474598}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0101020201010201010200010102000201000201010102000102010201010000.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 376467639 | ||
| mem: 1373721}) | ||
({cpu: 376191639 | ||
| mem: 1372521}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0101080808040600020306010000000302050807010208060100070207080202.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 997974739 | ||
| mem: 3653124}) | ||
({cpu: 997652739 | ||
| mem: 3651724}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0104010200020000040103020102020004040300030304040400010301040303.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 985076123 | ||
| mem: 3606481}) | ||
({cpu: 984570123 | ||
| mem: 3604281}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/04000f0b04051006000e060f09080d0b090d0104050a0b0f0506070f0a070008.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 913587161 | ||
| mem: 3290999}) | ||
({cpu: 912690161 | ||
| mem: 3287099}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0543a00ba1f63076c1db6bf94c6ff13ae7d266dd7544678743890b0e8e1add63.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 1335886976 | ||
| mem: 4623885}) | ||
({cpu: 1333655976 | ||
| mem: 4614185}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0705030002040601010206030604080208020207000101060706050502040301.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 1410834502 | ||
| mem: 4711774}) | ||
({cpu: 1407637502 | ||
| mem: 4697874}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/07070c070510030509010e050d00040907050e0a0d06030f1006030701020607.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 1328135567 | ||
| mem: 4842543}) | ||
({cpu: 1326939567 | ||
| mem: 4837343}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0bcfd9487614104ec48de2ea0b2c0979866a95115748c026f9ec129384c262c4.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 1461455571 | ||
| mem: 5309691}) | ||
({cpu: 1460719571 | ||
| mem: 5306491}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0be82588e4e4bf2ef428d2f44b7687bbb703031d8de696d90ec789e70d6bc1d8.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 1773166290 | ||
| mem: 6446866}) | ||
({cpu: 1771924290 | ||
| mem: 6441466}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/0f1d0110001b121d051e15140c0c05141d151c1f1d201c040f10091b020a0e1a.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 610770250 | ||
| mem: 2243067}) | ||
({cpu: 610494250 | ||
| mem: 2241867}) |
4 changes: 2 additions & 2 deletions
4
...antics/9.6/119fbea4164e2bf21d2b53aa6c2c4e79414fe55e4096f5ce2e804735a7fbaf91.budget.golden
This file contains 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
({cpu: 937067950 | ||
| mem: 3383163}) | ||
({cpu: 935480950 | ||
| mem: 3376263}) |
Oops, something went wrong.