Skip to content
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

SCP-2462: Use BuiltinData in the ledger etc. #3539

Merged
merged 2 commits into from
Jul 15, 2021
Merged

Conversation

michaelpj
Copy link
Contributor

This propagates up the use of BuiltinData to script arguments. The
payoff is in Plutus.V1.Ledger.Api, where we can now just wrap the
arguments in constants instead of having to use Lift.

I've opted to be painfully explicit about the difference between Data
and BuiltinData. There are a few places where we need conversions, but
overall not too many.

Will need a corresponding ledger PR, there are a couple of small changes.

Pre-submit checklist:

  • Branch
    • Commit sequence broadly makes sense
    • Key commits have useful messages
    • Relevant tickets are mentioned in commit messages
    • Formatting, materialized Nix files, PNG optimization, etc. are updated
  • PR
    • Self-reviewed the diff
    • Useful pull request description
    • Reviewer requested

Pre-merge checklist:

  • Someone approved it
  • Commits have useful messages
  • Review clarifications made it into the code
  • History is moderately tidy; or going to squash-merge

deriving stock (Generic, Haskell.Show)
deriving anyclass (ToJSON, FromJSON)
newtype Context = Context BuiltinData
deriving (ToJSON, FromJSON) via PLC.Data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you derive Pretty this way too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, do you think it's useful? Doesn't hurt, I guess.

@kwxm
Copy link
Contributor

kwxm commented Jul 13, 2021

I regenerated most of the plutus-use-cases scripts with this and they mostly seem to have got bigger. Times to follow.

Script sizes
Old = master at bfc2e4a
New = mpj/data-ledger at e29816a8

Script                            Old        New
------------------------------------------------------------
crowdfunding-success-1            7836       7975      +1.77%  
crowdfunding-success-2            7836	     7975      +1.77%  
crowdfunding-success-3            7836	     7975      +1.77%  
currency-1                        6589	     7563     +14.78% 
currency-2                        7480	     7480       0.00%  
escrow-redeem_1-1                 9364	     9389      +0.27%  
escrow-redeem_1-2                 9364	     9389      +0.27%  
escrow-redeem_2-1                 9628	     9528      -1.04% 
escrow-redeem_2-2                 9628	     9528      -1.04% 
escrow-redeem_2-3                 9628	     9528      -1.04% 
escrow-refund-1                   8808	     9130      +3.66%  
future-increase-margin-1          6567	     7528     +14.63% 
future-increase-margin-2          7447	     9986     +34.09% 
future-increase-margin-3         10355	     9986      -3.56% 
future-increase-margin-4         10355	    17333     +67.39% 
future-increase-margin-5         16893	    17773      +5.21%  
future-increase-margin-6         17058	    17058      +0.00%  
future-pay-out-1                  6567	     7528     +14.63% 
future-pay-out-2                  7447	     9986     +34.09% 
future-pay-out-3                 10355	     9986      -3.56% 
future-pay-out-4                 10355	    17585     +69.82% 
future-pay-out-5                 17231	    17231      +0.00%  
future-settle-early-1             6567	     7528     +14.63% 
future-settle-early-2             7447	     9986     +34.09% 
future-settle-early-3            10355	     9986      -3.56% 
future-settle-early-4            10355	    17479     +68.80% 
future-settle-early-5            17033	    17033      +0.00%  
game-sm-success-1                13961	    14186      +1.61%  
game-sm-success_2-1              13961	    14186      +1.61%  
game-sm-success_2-2               6332	     6606      +4.33%  
game-sm-success_2-3              14366	    14390      +0.17%  
game-sm-success_2-4               6625	     6758      +2.01%  
game-sm-success_2-5              14371	    14395      +0.17%  
game-sm-success_2-6               6625	     6758      +2.01%  
game-sm-success-2                 6332	     6606      +4.33%  
game-sm-success-3                14366	    14390      +0.17%  
game-sm-success-4                 6625	     6758      +2.01%  
multisig-sm-01                   15352	    15708      +2.32%  
multisig-sm-02                   15440	    15777      +2.18%  
multisig-sm-03                   15522	    15845      +2.08%  
multisig-sm-04                   15604	    15914      +1.99%  
multisig-sm-05                   15513	    15822      +1.99%  
multisig-sm-06                   15352	    15813      +3.00%  
multisig-sm-07                   15440	    15777      +2.18%  
multisig-sm-08                   15522	    15845      +2.08%  
multisig-sm-09                   15604	    15914      +1.99%  
multisig-sm-10                   15513	    15822      +1.99%  
ping-pong-1                      12466	    12958      +3.95%  
ping-pong_2-1                    12219	    12823      +4.94%  
ping-pong-2                      12466	    12958      +3.95%  
prism-1                           5795	     6185      +6.73%  
prism-2                          13643	    13769      +0.92%  
prism-3                           7893	     8088      +2.47%  
pubkey-1                          6150	     6491      +5.54%  
stablecoin_1-1                   19589	    20426      +4.27%  
stablecoin_1-2                    6403	     6648      +3.83%  
stablecoin_1-3                   19695	    20476      +3.97%  
stablecoin_1-4                    6508	     6696      +2.89%  
stablecoin_1-5                   20140	    20535      +1.96%  
stablecoin_1-6                    6953	     6755      -2.85% 
stablecoin_2-1                   19589	    20426      +4.27%  
stablecoin_2-2                    6403	     6648      +3.83%  
stablecoin_2-3                   19695	    20476      +3.97%  
stablecoin_2-4                    6508	     6696      +2.89%  
token-account-1                   6517	     7508     +15.21% 
token-account-2                   7385	     7624      +3.24%  
token-account-3                   7493	     7493      +0.00%  
vesting-1                         8829	     9118      +3.27%  

@kwxm
Copy link
Contributor

kwxm commented Jul 13, 2021

Oh no!!!!

Script                                      master       data-ledger    Change
----------------------------------------------------------------------------------
crowdfunding/crowdfunding-success-1         644.7 μs	   1.276 ms     +97.9%
crowdfunding/crowdfunding-success-2         645.8 μs	   1.277 ms     +97.7%
crowdfunding/crowdfunding-success-3         645.7 μs	   1.277 ms     +97.8%
currency/currency-1                         581.8 μs	   1.007 ms     +73.1%
currency/currency-2                         798.5 μs	   800.6 μs      +0.3%
escrow/escrow-redeem_1-1                    942.8 μs	   1.539 ms     +63.2%
escrow/escrow-redeem_1-2                    942.9 μs	   1.539 ms     +63.2%
escrow/escrow-redeem_2-1                    1.081 ms	   1.756 ms     +62.4%
escrow/escrow-redeem_2-2                    1.085 ms	   1.757 ms     +61.9%
escrow/escrow-redeem_2-3                    1.084 ms	   1.754 ms     +61.8%
escrow/escrow-refund-1                      446.0 μs	   939.4 μs    +110.6%
future/future-increase-margin-1             585.1 μs	   1.002 ms     +71.3%
future/future-increase-margin-2             803.2 μs	   2.118 ms    +163.7%
future/future-increase-margin-3             1.367 ms	   2.122 ms     +55.2%
future/future-increase-margin-4             1.366 ms	   1.799 ms     +31.7%
future/future-increase-margin-5             1.409 ms	   2.549 ms     +80.9%
future/future-increase-margin-6             1.842 ms	   1.844 ms      +0.1%
future/future-pay-out-1                     584.4 μs	   1.007 ms     +72.3%
future/future-pay-out-2                     803.0 μs	   2.119 ms    +163.9%
future/future-pay-out-3                     1.366 ms	   2.118 ms     +55.1%
future/future-pay-out-4                     1.363 ms	   2.330 ms     +70.9%
future/future-pay-out-5                     1.897 ms	   1.897 ms       0.0%
future/future-settle-early-1                583.2 μs	   1.010 ms     +73.2%
future/future-settle-early-2                802.4 μs	   2.125 ms    +164.8%
future/future-settle-early-3                1.367 ms	   2.122 ms     +55.2%
future/future-settle-early-4                1.363 ms	   1.923 ms     +41.1%
future/future-settle-early-5                1.518 ms	   1.519 ms      +0.1%
game-sm/game-sm-success-1                   1.005 ms	   1.589 ms     +58.1%
game-sm/game-sm-success-2                   606.8 μs	   1.208 ms     +99.1%
game-sm/game-sm-success-3                   1.459 ms	   2.136 ms     +46.4%
game-sm/game-sm-success-4                   726.3 μs	   1.395 ms     +92.1%
game-sm/game-sm-success_2-1                 1.011 ms	   1.589 ms     +57.2%
game-sm/game-sm-success_2-2                 608.7 μs	   1.212 ms     +99.1%
game-sm/game-sm-success_2-3                 1.462 ms	   2.152 ms     +47.2%
game-sm/game-sm-success_2-4                 723.4 μs	   1.401 ms     +93.7%
game-sm/game-sm-success_2-5                 1.453 ms	   2.179 ms     +50.0%
game-sm/game-sm-success_2-6                 724.4 μs	   1.400 ms     +93.3%
multisig-sm/multisig-sm-01                  1.054 ms	   1.535 ms     +45.6%
multisig-sm/multisig-sm-02                  1.058 ms	   1.543 ms     +45.8%
multisig-sm/multisig-sm-03                  1.074 ms	   1.554 ms     +44.7%
multisig-sm/multisig-sm-04                  1.086 ms	   1.572 ms     +44.8%
multisig-sm/multisig-sm-05                  1.275 ms	   1.874 ms     +47.0%
multisig-sm/multisig-sm-06                  1.051 ms	   1.713 ms     +63.0%
multisig-sm/multisig-sm-07                  1.055 ms	   1.539 ms     +45.9%
multisig-sm/multisig-sm-08                  1.069 ms	   1.549 ms     +44.9%
multisig-sm/multisig-sm-09                  1.085 ms	   1.566 ms     +44.3%
multisig-sm/multisig-sm-10                  1.278 ms	   1.869 ms     +46.2%
ping-pong/ping-pong-1                       800.3 μs	   1.295 ms     +61.8%
ping-pong/ping-pong-2                       798.6 μs	   1.294 ms     +62.0%
ping-pong/ping-pong_2-1                     492.0 μs	   986.4 μs    +100.5%
prism/prism-1                               473.8 μs	   996.5 μs    +110.3%
prism/prism-2                               1.083 ms	   1.760 ms     +62.5%
prism/prism-3                               981.2 μs	   1.643 ms     +67.4%
pubkey/pubkey-1                             414.5 μs	   905.8 μs    +118.5%
stablecoin/stablecoin_1-1                   2.088 ms	   2.597 ms     +24.4%
stablecoin/stablecoin_1-2                   597.0 μs	   1.187 ms     +98.8%
stablecoin/stablecoin_1-3                   2.364 ms	   2.880 ms     +21.8%
stablecoin/stablecoin_1-4                   649.4 μs	   1.266 ms     +94.9%
stablecoin/stablecoin_1-5                   2.964 ms	   3.371 ms     +13.7%
stablecoin/stablecoin_1-6                   831.6 μs	   1.331 ms     +60.1%
stablecoin/stablecoin_2-1                   2.078 ms	   2.588 ms     +24.5%
stablecoin/stablecoin_2-2                   597.3 μs	   1.182 ms     +97.9%
stablecoin/stablecoin_2-3                   2.365 ms	   2.864 ms     +21.1%
stablecoin/stablecoin_2-4                   650.4 μs	   1.260 ms     +93.7%
token-account/token-account-1               555.9 μs	   874.3 μs     +57.3%
token-account/token-account-2               663.6 μs	   1.451 ms    +118.7%
token-account/token-account-3               903.1 μs	   897.3 μs      -0.6%
vesting/vesting-1                           885.9 μs	   1.366 ms     +54.2%

@kwxm
Copy link
Contributor

kwxm commented Jul 13, 2021

The figures from the master column above were those in the repository (which have been there for several weeks), but the ones in data-ledger were generated anew today. Just to make sure that the changes weren't due to something else that had happened in the interim I regenerated the scripts in the master branch and compared them with the ones in the repository (ie. the ones in the left-hand column above). There are a few large changes which look as if they've contributed to the biggest changes in the data-ledger branch, but otherwise they're pretty much in the same ballpark. So it seems that the slowdown with built-in data is real, with slowdowns of 50-100% compared to newly-generated scripts in master.

validation-old-new-13th-July.txt

@kwxm
Copy link
Contributor

kwxm commented Jul 13, 2021

I ran the nofib examples in both branches and I'm glad to say that the results were pretty much identical.

I also used uplc with the -t and -1 options to run a couple of the scripts and tell me how often the machine saw each constructor. It seems to be doing a lot more work at the basic PLC level when it's using built-in Data, but not twice as much.

crowdfunding-1

                     master        data-ledger
----------------------------------------------
Const                  177             278
Var                   6077            8181
LamAbs                6170            6918
Apply                 5996            9010
Delay                 1061            1153
Force                 2071            3320
Builtin                145            1236

startup                  0               0
compute              21697           30096
AST nodes             9181            8717
future-increase-margin-2

                     master         data-ledger
-----------------------------------------------
Const                  269               394
Var                  13916             16687
LamAbs               12662             13583
Apply                12819             16857
Delay                 2397              2452
Force                 4655              6307
Builtin                264              1772

startup                  0                 0
compute              46982             58052
AST nodes            11418             10407

@effectfully
Copy link
Contributor

That's sad, but it kinda makes sense. matchData is essentially an inefficient way of constructing Scott-encoded Data. If we had builtinToScottData : BuiltinData -> ScottData, we could avoid the super inefficient conversion where we pay a lot for both recursion and compilation of Data. I think that would amount to having a right fold over BuiltinData as a builtin. Not sure if we can do that without allowing to return nearly arbitrary stuff from the builtin application machinery. On the other hand, fixing this ridiculous performance is probably worth complicating the builtin application machinery further.

@michaelpj
Copy link
Contributor Author

I think there may be a more obvious problem, looking into it now...

@michaelpj
Copy link
Contributor Author

So one issue was the ExMemory instance for Data which was using the generic one, which will, I think, traverse the whole structure. Which would have meant a lot of work: if you traverse a Data object converting it to something else, you'll traverse the whole substructure for the ExMemory calculation at every stage, which I think is going to be quadratic. However, it seems that that's not enough to restore the previous performance.

@michaelpj
Copy link
Contributor Author

hacked in some tallying of builtin counts:

HeadList                           0                0  Sum {getSum = 120}
TailList                           0                0  Sum {getSum = 120}
AddInteger                    237457                2  Sum {getSum = 1}
IfThenElse                         0                0  Sum {getSum = 274}
NullList                           0                0  Sum {getSum = 202}
UnIData                            0                0  Sum {getSum = 13}
EqualsByteString              189546                1  Sum {getSum = 1}
UnListData                         0                0  Sum {getSum = 19}
SndPair                            0                0  Sum {getSum = 63}
UnConstrData                       0                0  Sum {getSum = 132}
EqualsInteger               15093393               71  Sum {getSum = 71}
FstPair                            0                0  Sum {getSum = 69}
ChooseData                         0                0  Sum {getSum = 123}
UnBData                            0                0  Sum {getSum = 28}

Going to try a few things. First one: replace null+ifThenElse with chooseList.

@michaelpj
Copy link
Contributor Author

Profiling suggested that we weren't obviously spending more time in builtin operations. I concluded that we were perhaps simply doing more work than we had been.

Things I tried:

  • Implementing fromBuiltinData directly in terms of operations on builtin types, rather than converting to the Haskell versions first. I thought this would save us some time: otherwise for each Constr i l we traverse the builtin list l to turn it into a Haskell list and then match on that (traversing it again). Made a small difference.
  • Using chooseList rather than the combination of null and ifThenElse. Saves us some builtin calls, but didn't make much difference overall. Left it in because it seems sensible to have.
  • Implementing unsafeFromBuiltinData that gives an error instead of returning Nothing in case of failure. This is useful because the primary use of fromBuiltinData is when parsing the arguments to scripts, and we would anyway fail in that situation if they don't parse. This lets us cut out a lot of the parsing code, and makes a huge difference. I think it pulls us back to something comparable to where we were before, but I need to check.

The current state has some code-bloat due to the fact that the non-unsafe fromBuiltinData is quite bulky, and we're not clever enough to get rid of one unused method of a typeclass. I might split the typeclass in future to try and ameliorate this: most scripts should only need unsafeFromBuiltinData.

I need to benchmark this properly but that requires me to regenerate the scripts again...

@michaelpj
Copy link
Contributor Author

Oh, and if this works, I should take more time to work out why the old way was so fast, I really don't know how it can have been!

@kwxm
Copy link
Contributor

kwxm commented Jul 15, 2021

Profiling suggested that we weren't obviously spending more time in builtin operations. I concluded that we were perhaps simply doing more work than we had been.

In the two examples I looked at up above it was calling compute 25% and 50% more often, which would account for quite a lot of the slowdown itself and is the opposite of what I'd have expected. I tried looking at the PLC but of course it wasn't very illuminating.

@michaelpj
Copy link
Contributor Author

In the two examples I looked at up above it was calling compute 25% and 50% more often, which would account for quite a lot of the slowdown itself and is the opposite of what I'd have expected.

So I think this mainly means we were generating fromData code that just required much more computation to get the answer, somehow. I'm still not really sure how that came about, but my latest patch gets rid of a lot of the computation, which should make things better...

@michaelpj
Copy link
Contributor Author

Script                                      Old            New          Change                                                                                                                                                                                                                                             
----------------------------------------------------------------------------------                                                                                                                                                                                                                                         
crowdfunding/crowdfunding-success-1         623.6 μs       731.2 μs     +17.3%                                                                                                                                                                                                                                             
crowdfunding/crowdfunding-success-2         625.4 μs       731.5 μs     +17.0%                                                                                                                                                                                                                                             
crowdfunding/crowdfunding-success-3         625.6 μs       733.9 μs     +17.3%                                                                                                                                                                                                                                             
currency/currency-1                         580.9 μs       654.7 μs     +12.7%                                                                                                                                                                                                                                             
currency/currency-2                         801.7 μs       790.6 μs      -1.4%                                                                                                                                                                                                                                             
escrow/escrow-redeem_1-1                    936.5 μs       996.5 μs      +6.4%                                                                                                                                                                                                                                             
escrow/escrow-redeem_1-2                    935.8 μs       996.7 μs      +6.5%                                                                                                                                                                                                                                             
escrow/escrow-redeem_2-1                    1.072 ms       1.137 ms      +6.1%                                                                                                                                                                                                                                             
escrow/escrow-redeem_2-2                    1.072 ms       1.135 ms      +5.9%                                                                                                                                                                                                                                             
escrow/escrow-redeem_2-3                    1.070 ms       1.137 ms      +6.3%                                                                                                                                                                                                                                             
escrow/escrow-refund-1                      435.0 μs       534.1 μs     +22.8%                                                                                                                                                                                                                                             
future/future-increase-margin-1             579.1 μs       658.9 μs     +13.8%                                                                                                                                                                                                                                             
future/future-increase-margin-2             1.355 ms       1.387 ms      +2.4%                                                                                                                                                                                                                                             
future/future-increase-margin-3             1.350 ms       1.388 ms      +2.8%                                                                                                                                                                                                                                             
future/future-increase-margin-4             1.411 ms       1.380 ms      -2.2%                                                                                                                                                                                                                                             
future/future-increase-margin-5             1.806 ms       1.803 ms      -0.2%                                                                                                                                                                                                                                             
future/future-increase-margin-6             1.833 ms       1.830 ms      -0.2%
future/future-pay-out-1                     580.5 μs       657.5 μs     +13.3%
future/future-pay-out-2                     1.351 ms       1.384 ms      +2.4%
future/future-pay-out-3                     1.349 ms       1.383 ms      +2.5%
future/future-pay-out-4                     1.877 ms       1.864 ms      -0.7%
future/future-pay-out-5                     1.903 ms       1.883 ms      -1.1%
future/future-settle-early-1                583.4 μs       659.0 μs     +13.0%
future/future-settle-early-2                1.362 ms       1.389 ms      +2.0%
future/future-settle-early-3                1.360 ms       1.392 ms      +2.4%
future/future-settle-early-4                1.510 ms       1.534 ms      +1.6%
future/future-settle-early-5                1.527 ms       1.515 ms      -0.8%
game-sm/game-sm-success-1                   1.004 ms       1.033 ms      +2.9%
game-sm/game-sm-success-2                   597.5 μs       685.8 μs     +14.8%
game-sm/game-sm-success-3                   1.453 ms       1.457 ms      +0.3%
game-sm/game-sm-success-4                   714.9 μs       787.5 μs     +10.2%
game-sm/game-sm-success_2-1                 1.003 ms       1.029 ms      +2.6%
game-sm/game-sm-success_2-2                 596.8 μs       684.2 μs     +14.6%
game-sm/game-sm-success_2-3                 1.450 ms       1.454 ms      +0.3%
game-sm/game-sm-success_2-4                 713.2 μs       785.9 μs     +10.2%
game-sm/game-sm-success_2-5                 1.449 ms       1.476 ms      +1.9%
game-sm/game-sm-success_2-6                 712.3 μs       787.1 μs     +10.5%
multisig-sm/multisig-sm-01                  1.073 ms       1.037 ms      -3.4%
multisig-sm/multisig-sm-02                  1.075 ms       1.036 ms      -3.6%
multisig-sm/multisig-sm-03                  1.098 ms       1.044 ms      -4.9%
multisig-sm/multisig-sm-04                  1.110 ms       1.057 ms      -4.8%
multisig-sm/multisig-sm-05                  1.311 ms       1.314 ms      +0.2%
multisig-sm/multisig-sm-06                  1.080 ms       1.038 ms      -3.9%
multisig-sm/multisig-sm-07                  1.080 ms       1.034 ms      -4.3%
multisig-sm/multisig-sm-08                  1.097 ms       1.145 ms      +4.4%
multisig-sm/multisig-sm-09                  1.109 ms       1.053 ms      -5.0%
multisig-sm/multisig-sm-10                  1.304 ms       1.309 ms      +0.4%
ping-pong/ping-pong-1                       809.8 μs       882.8 μs      +9.0%
ping-pong/ping-pong-2                       809.0 μs       882.1 μs      +9.0%
ping-pong/ping-pong_2-1                     478.0 μs       581.7 μs     +21.7%
prism/prism-1                               458.4 μs       572.4 μs     +24.9%
prism/prism-2                               1.084 ms       1.140 ms      +5.2%
prism/prism-3                               963.1 μs       1.026 ms      +6.5%
pubkey/pubkey-1                             402.5 μs       499.6 μs     +24.1%
stablecoin/stablecoin_1-1                   2.044 ms       2.023 ms      -1.0%
stablecoin/stablecoin_1-2                   590.3 μs       675.4 μs     +14.4%
stablecoin/stablecoin_1-3                   2.303 ms       2.264 ms      -1.7%
stablecoin/stablecoin_1-4                   640.5 μs       714.2 μs     +11.5%
stablecoin/stablecoin_1-5                   2.800 ms       2.846 ms      +1.6%
stablecoin/stablecoin_1-6                   682.5 μs       860.0 μs     +26.0%
stablecoin/stablecoin_2-1                   2.045 ms       2.017 ms      -1.4%
stablecoin/stablecoin_2-2                   587.7 μs       672.9 μs     +14.5%
stablecoin/stablecoin_2-3                   2.297 ms       2.259 ms      -1.7%
stablecoin/stablecoin_2-4                   638.8 μs       712.1 μs     +11.5%
token-account/token-account-1               449.4 μs       535.5 μs     +19.2%
token-account/token-account-2               869.7 μs       904.9 μs      +4.0%
token-account/token-account-3               899.7 μs       890.2 μs      -1.1%
uniswap/uniswap-1                           646.3 μs       946.8 μs     +46.5%
uniswap/uniswap-2                           1.100 ms       661.7 μs     -39.8%
uniswap/uniswap-3                           717.1 μs       3.579 ms    +399.1%
uniswap/uniswap-4                           825.2 μs       1.099 ms     +33.2%
uniswap/uniswap-5                           3.824 ms       2.960 ms     -22.6%
uniswap/uniswap-6                           1.108 ms       1.041 ms      -6.0%
uniswap/uniswap-7                           2.770 ms       2.762 ms      -0.3%
uniswap/uniswap-8                           1.050 ms       1.044 ms      -0.6%
vesting/vesting-1                           868.7 μs       921.0 μs      +6.0%
marlowe/trustfund/trustfund-1               2.065 ms       2.049 ms      -0.8%
marlowe/trustfund/trustfund-2               1.517 ms       1.508 ms      -0.6%
marlowe/zerocoupon/zerocoupon-1             2.170 ms       2.152 ms      -0.8%
marlowe/zerocoupon/zerocoupon-2             1.319 ms       1.309 ms      -0.8%

Better, but still not great.

@kwxm
Copy link
Contributor

kwxm commented Jul 15, 2021

uniswap/uniswap-3 717.1 μs 3.579 ms +399.1%

Nice!

@michaelpj
Copy link
Contributor Author

uniswap/uniswap-3 717.1 μs 3.579 ms +399.1%

I think I know what this is: it's got a manual use of the slow fromBuiltinData. I'll change that.

@michaelpj
Copy link
Contributor Author

Didn't work 🤔

This propagates up the use of `BuiltinData` to script arguments. The
payoff is in `Plutus.V1.Ledger.Api`, where we can now just wrap the
arguments in constants instead of having to use `Lift`.

I've opted to be painfully explicit about the difference between `Data`
and `BuiltinData`. There are a few places where we need conversions, but
overall not *too* many.
@michaelpj
Copy link
Contributor Author

Okay, I'm going to break with best practices and merge this. I'm planning to keep investigating the regressions, or generally, to optimize fromBuiltinData, since this has made it obvious that it's a major part of script execution. And I'll definitely need to track down the regression in uniswap-3, but that seems doable. But I want to have this done before I go on holiday so it unblocks the ledger - it does at least achieve that!

Also note that I'm not updating the benchmark programs in this PR.

@michaelpj michaelpj merged commit 160d158 into master Jul 15, 2021
@sjoerdvisscher sjoerdvisscher deleted the mpj/data-ledger branch July 16, 2021 08:14
luigy added a commit that referenced this pull request Jul 17, 2021
this matches with the encoding in haskell

Fixes #3563
Fixup from aeson instance changes in #3539
luigy added a commit that referenced this pull request Jul 17, 2021
this matches with the encoding in haskell

Fixes #3563
Fixup from aeson instance changes in #3539
@luigy luigy mentioned this pull request Jul 18, 2021
11 tasks
@j-mueller j-mueller mentioned this pull request Jul 23, 2021
11 tasks
Comment on lines 162 to +165
-- Note that we are using builtin ifThenElse here so this is *strict* application! So we need to do
-- the manual laziness ourselves. We could instead convert the boolean to a Haskell boolean and use
-- normal if, but we might as well use the builtins directly here.
go l = ifThenElse (null l) (\_ -> []) (\_ -> fromBuiltin (head l):go (tail l)) unitval
go l = chooseList (\_ -> []) (\_ -> fromBuiltin (head l):go (tail l)) l unitval
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Outdated comment.

Comment on lines +274 to +277
choosePlc :: Opaque term b -> Opaque term b -> SomeConstantOf uni [] '[a] -> EvaluationResult (Opaque term b)
choosePlc a b (SomeConstantOfArg _ (SomeConstantOfRes _ xs)) = case xs of
[] -> EvaluationSuccess a
_ : _ -> EvaluationSuccess b
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need EvaluationResult if you always return EvaluationSuccess. I.e. you can drop all of that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to do that 🤔 I think I tried to write it like the other functions and it didn't work for some reason?

Comment on lines 0 to 1
ExMemory 2854 No newline at end of file
ExMemory 2170
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are really impressive numbers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants