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

[Builtins] Shove everything into 'BuiltinRuntime' #4514

Conversation

effectfully
Copy link
Contributor

Don't look here yet.

(Haven't looked at the Core yet, might contain infelicities).

@effectfully
Copy link
Contributor Author

/benchmark plutus-benchmark:validation

BuiltinRuntimeArrow $ \mem arg ->
knownMonoruntime @val @args @res $ do
(exF, f) <- getFs
x <- readKnown (Just ()) arg
Copy link
Contributor Author

@effectfully effectfully Mar 30, 2022

Choose a reason for hiding this comment

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

Note to self: don't forget to drop that Just () argument once unlifting is changed (I don't feel like it affects performance, it's just silly to have it).

@iohk-devops
Copy link

Comparing benchmark results of 'plutus-benchmark:validation' on 'e59ade143' (base) and 'bd7d0763e' (PR)

Script e59ade1 bd7d076 Change
auction_1-1 226.5 μs 220.8 μs -2.5%
auction_1-2 858.4 μs 854.3 μs -0.5%
auction_1-3 848.4 μs 846.1 μs -0.3%
auction_1-4 293.3 μs 283.2 μs -3.4%
auction_2-1 227.6 μs 220.4 μs -3.2%
auction_2-2 858.7 μs 850.8 μs -0.9%
auction_2-3 1.089 ms 1.080 ms -0.8%
auction_2-4 854.1 μs 843.5 μs -1.2%
auction_2-5 295.7 μs 283.6 μs -4.1%
crowdfunding-success-1 268.5 μs 257.2 μs -4.2%
crowdfunding-success-2 267.9 μs 257.1 μs -4.0%
crowdfunding-success-3 268.4 μs 256.7 μs -4.4%
currency-1 322.2 μs 316.9 μs -1.6%
escrow-redeem_1-1 466.1 μs 453.3 μs -2.7%
escrow-redeem_1-2 466.3 μs 454.2 μs -2.6%
escrow-redeem_2-1 544.7 μs 534.1 μs -1.9%
escrow-redeem_2-2 544.9 μs 535.1 μs -1.8%
escrow-redeem_2-3 542.9 μs 535.3 μs -1.4%
escrow-refund-1 200.3 μs 195.9 μs -2.2%
future-increase-margin-1 321.8 μs 318.8 μs -0.9%
future-increase-margin-2 723.2 μs 712.3 μs -1.5%
future-increase-margin-3 721.7 μs 710.8 μs -1.5%
future-increase-margin-4 676.7 μs 667.2 μs -1.4%
future-increase-margin-5 1.053 ms 1.046 ms -0.7%
future-pay-out-1 321.9 μs 316.7 μs -1.6%
future-pay-out-2 722.2 μs 708.5 μs -1.9%
future-pay-out-3 722.2 μs 709.2 μs -1.8%
future-pay-out-4 1.053 ms 1.041 ms -1.1%
future-settle-early-1 322.2 μs 317.9 μs -1.3%
future-settle-early-2 724.0 μs 711.8 μs -1.7%
future-settle-early-3 725.9 μs 708.7 μs -2.4%
future-settle-early-4 808.0 μs 801.3 μs -0.8%
game-sm-success_1-1 524.9 μs 514.5 μs -2.0%
game-sm-success_1-2 250.2 μs 242.2 μs -3.2%
game-sm-success_1-3 849.4 μs 841.4 μs -0.9%
game-sm-success_1-4 288.9 μs 282.2 μs -2.3%
game-sm-success_2-1 524.4 μs 517.3 μs -1.4%
game-sm-success_2-2 249.1 μs 242.1 μs -2.8%
game-sm-success_2-3 850.4 μs 842.6 μs -0.9%
game-sm-success_2-4 288.0 μs 281.4 μs -2.3%
game-sm-success_2-5 848.6 μs 843.8 μs -0.6%
game-sm-success_2-6 289.2 μs 281.7 μs -2.6%
multisig-sm-1 538.6 μs 529.9 μs -1.6%
multisig-sm-2 527.1 μs 515.7 μs -2.2%
multisig-sm-3 534.3 μs 521.8 μs -2.3%
multisig-sm-4 539.6 μs 524.7 μs -2.8%
multisig-sm-5 753.6 μs 742.9 μs -1.4%
multisig-sm-6 538.2 μs 527.0 μs -2.1%
multisig-sm-7 528.1 μs 515.8 μs -2.3%
multisig-sm-8 531.9 μs 520.6 μs -2.1%
multisig-sm-9 537.3 μs 526.8 μs -2.0%
multisig-sm-10 752.2 μs 743.8 μs -1.1%
ping-pong-1 440.8 μs 432.6 μs -1.9%
ping-pong-2 439.9 μs 432.9 μs -1.6%
ping-pong_2-1 261.0 μs 254.8 μs -2.4%
prism-1 208.6 μs 200.8 μs -3.7%
prism-2 566.2 μs 556.0 μs -1.8%
prism-3 480.0 μs 469.4 μs -2.2%
pubkey-1 175.4 μs 170.7 μs -2.7%
stablecoin_1-1 1.173 ms 1.177 ms +0.3%
stablecoin_1-2 243.1 μs 235.4 μs -3.2%
stablecoin_1-3 1.338 ms 1.344 ms +0.4%
stablecoin_1-4 257.6 μs 250.6 μs -2.7%
stablecoin_1-5 1.695 ms 1.686 ms -0.5%
stablecoin_1-6 321.4 μs 309.5 μs -3.7%
stablecoin_2-1 1.178 ms 1.172 ms -0.5%
stablecoin_2-2 244.2 μs 234.1 μs -4.1%
stablecoin_2-3 1.348 ms 1.340 ms -0.6%
stablecoin_2-4 259.4 μs 249.5 μs -3.8%
token-account-1 243.1 μs 238.7 μs -1.8%
token-account-2 428.5 μs 422.0 μs -1.5%
uniswap-1 535.9 μs 534.3 μs -0.3%
uniswap-2 287.2 μs 282.9 μs -1.5%
uniswap-3 2.174 ms 2.133 ms -1.9%
uniswap-4 432.2 μs 413.2 μs -4.4%
uniswap-5 1.503 ms 1.476 ms -1.8%
uniswap-6 414.7 μs 395.6 μs -4.6%
vesting-1 461.4 μs 456.3 μs -1.1%

@effectfully
Copy link
Contributor Author

effectfully commented Mar 30, 2022

-1.98%. Well, that's not good at all. I'll look at it closely some time later.

@effectfully
Copy link
Contributor Author

/benchmark plutus-benchmark:validation

@iohk-devops
Copy link

Comparing benchmark results of 'plutus-benchmark:validation' on 'e59ade143' (base) and '1715c99a2' (PR)

Script e59ade1 1715c99 Change
auction_1-1 226.8 μs 212.6 μs -6.3%
auction_1-2 861.7 μs 836.4 μs -2.9%
auction_1-3 852.0 μs 828.3 μs -2.8%
auction_1-4 294.2 μs 270.8 μs -8.0%
auction_2-1 227.1 μs 212.4 μs -6.5%
auction_2-2 859.3 μs 836.8 μs -2.6%
auction_2-3 1.089 ms 1.066 ms -2.1%
auction_2-4 847.8 μs 827.7 μs -2.4%
auction_2-5 293.3 μs 271.0 μs -7.6%
crowdfunding-success-1 266.3 μs 249.6 μs -6.3%
crowdfunding-success-2 266.7 μs 249.5 μs -6.4%
crowdfunding-success-3 266.6 μs 249.9 μs -6.3%
currency-1 319.8 μs 311.7 μs -2.5%
escrow-redeem_1-1 464.2 μs 443.0 μs -4.6%
escrow-redeem_1-2 465.2 μs 442.6 μs -4.9%
escrow-redeem_2-1 544.3 μs 519.4 μs -4.6%
escrow-redeem_2-2 544.0 μs 518.9 μs -4.6%
escrow-redeem_2-3 543.9 μs 518.2 μs -4.7%
escrow-refund-1 199.1 μs 188.2 μs -5.5%
future-increase-margin-1 321.9 μs 310.1 μs -3.7%
future-increase-margin-2 721.7 μs 697.7 μs -3.3%
future-increase-margin-3 724.9 μs 696.4 μs -3.9%
future-increase-margin-4 678.7 μs 655.4 μs -3.4%
future-increase-margin-5 1.057 ms 1.031 ms -2.5%
future-pay-out-1 322.3 μs 310.0 μs -3.8%
future-pay-out-2 723.7 μs 697.6 μs -3.6%
future-pay-out-3 721.5 μs 701.1 μs -2.8%
future-pay-out-4 1.050 ms 1.035 ms -1.4%
future-settle-early-1 321.5 μs 312.1 μs -2.9%
future-settle-early-2 722.8 μs 697.2 μs -3.5%
future-settle-early-3 720.8 μs 697.0 μs -3.3%
future-settle-early-4 805.6 μs 791.9 μs -1.7%
game-sm-success_1-1 522.6 μs 501.4 μs -4.1%
game-sm-success_1-2 247.8 μs 231.8 μs -6.5%
game-sm-success_1-3 848.1 μs 824.3 μs -2.8%
game-sm-success_1-4 288.1 μs 269.6 μs -6.4%
game-sm-success_2-1 523.1 μs 501.1 μs -4.2%
game-sm-success_2-2 248.3 μs 232.1 μs -6.5%
game-sm-success_2-3 844.9 μs 825.7 μs -2.3%
game-sm-success_2-4 288.3 μs 269.3 μs -6.6%
game-sm-success_2-5 851.9 μs 827.0 μs -2.9%
game-sm-success_2-6 289.1 μs 269.9 μs -6.6%
multisig-sm-1 538.0 μs 518.0 μs -3.7%
multisig-sm-2 526.5 μs 504.3 μs -4.2%
multisig-sm-3 533.4 μs 510.9 μs -4.2%
multisig-sm-4 538.2 μs 517.6 μs -3.8%
multisig-sm-5 753.3 μs 736.9 μs -2.2%
multisig-sm-6 535.8 μs 518.3 μs -3.3%
multisig-sm-7 525.2 μs 505.5 μs -3.8%
multisig-sm-8 529.7 μs 508.7 μs -4.0%
multisig-sm-9 535.6 μs 518.2 μs -3.2%
multisig-sm-10 749.5 μs 735.9 μs -1.8%
ping-pong-1 439.2 μs 424.3 μs -3.4%
ping-pong-2 440.7 μs 425.8 μs -3.4%
ping-pong_2-1 262.1 μs 250.6 μs -4.4%
prism-1 208.8 μs 194.0 μs -7.1%
prism-2 565.2 μs 541.3 μs -4.2%
prism-3 479.2 μs 459.1 μs -4.2%
pubkey-1 175.1 μs 163.8 μs -6.5%
stablecoin_1-1 1.176 ms 1.147 ms -2.5%
stablecoin_1-2 243.5 μs 228.4 μs -6.2%
stablecoin_1-3 1.351 ms 1.302 ms -3.6%
stablecoin_1-4 259.0 μs 241.4 μs -6.8%
stablecoin_1-5 1.697 ms 1.630 ms -3.9%
stablecoin_1-6 320.4 μs 298.3 μs -6.9%
stablecoin_2-1 1.176 ms 1.143 ms -2.8%
stablecoin_2-2 242.2 μs 226.3 μs -6.6%
stablecoin_2-3 1.343 ms 1.298 ms -3.4%
stablecoin_2-4 258.3 μs 240.3 μs -7.0%
token-account-1 241.4 μs 233.2 μs -3.4%
token-account-2 427.2 μs 413.3 μs -3.3%
uniswap-1 534.6 μs 514.9 μs -3.7%
uniswap-2 287.8 μs 273.1 μs -5.1%
uniswap-3 2.175 ms 2.081 ms -4.3%
uniswap-4 432.4 μs 399.8 μs -7.5%
uniswap-5 1.502 ms 1.442 ms -4.0%
uniswap-6 413.6 μs 385.2 μs -6.9%
vesting-1 459.3 μs 447.4 μs -2.6%

@effectfully
Copy link
Contributor Author

-4.34%. Better, but not nearly enough. Maybe the other PR does not have as reliable semantics as they are here, will need to check.

@effectfully
Copy link
Contributor Author

/benchmark plutus-benchmark:validation

@iohk-devops
Copy link

Comparing benchmark results of 'plutus-benchmark:validation' on 'e59ade143' (base) and '85d43fc21' (PR)

Script e59ade1 85d43fc Change
auction_1-1 228.0 μs 207.1 μs -9.2%
auction_1-2 860.1 μs 826.3 μs -3.9%
auction_1-3 852.6 μs 817.0 μs -4.2%
auction_1-4 295.0 μs 263.1 μs -10.8%
auction_2-1 228.1 μs 207.8 μs -8.9%
auction_2-2 858.8 μs 827.0 μs -3.7%
auction_2-3 1.089 ms 1.055 ms -3.1%
auction_2-4 849.6 μs 812.9 μs -4.3%
auction_2-5 294.1 μs 262.5 μs -10.7%
crowdfunding-success-1 266.9 μs 241.9 μs -9.4%
crowdfunding-success-2 267.1 μs 242.3 μs -9.3%
crowdfunding-success-3 266.8 μs 242.5 μs -9.1%
currency-1 320.9 μs 303.1 μs -5.5%
escrow-redeem_1-1 463.9 μs 432.8 μs -6.7%
escrow-redeem_1-2 464.5 μs 433.1 μs -6.8%
escrow-redeem_2-1 545.3 μs 509.1 μs -6.6%
escrow-redeem_2-2 544.1 μs 507.9 μs -6.7%
escrow-redeem_2-3 544.4 μs 508.9 μs -6.5%
escrow-refund-1 200.0 μs 183.8 μs -8.1%
future-increase-margin-1 321.5 μs 302.8 μs -5.8%
future-increase-margin-2 723.1 μs 685.7 μs -5.2%
future-increase-margin-3 724.3 μs 686.0 μs -5.3%
future-increase-margin-4 677.6 μs 643.0 μs -5.1%
future-increase-margin-5 1.058 ms 1.022 ms -3.4%
future-pay-out-1 323.0 μs 303.8 μs -5.9%
future-pay-out-2 722.6 μs 685.9 μs -5.1%
future-pay-out-3 725.0 μs 686.5 μs -5.3%
future-pay-out-4 1.056 ms 1.014 ms -4.0%
future-settle-early-1 322.1 μs 301.8 μs -6.3%
future-settle-early-2 723.2 μs 684.9 μs -5.3%
future-settle-early-3 721.6 μs 680.6 μs -5.7%
future-settle-early-4 806.1 μs 772.7 μs -4.1%
game-sm-success_1-1 524.3 μs 491.1 μs -6.3%
game-sm-success_1-2 248.3 μs 225.0 μs -9.4%
game-sm-success_1-3 846.4 μs 809.8 μs -4.3%
game-sm-success_1-4 289.3 μs 262.2 μs -9.4%
game-sm-success_2-1 524.0 μs 490.4 μs -6.4%
game-sm-success_2-2 249.4 μs 225.3 μs -9.7%
game-sm-success_2-3 844.7 μs 811.8 μs -3.9%
game-sm-success_2-4 289.7 μs 263.4 μs -9.1%
game-sm-success_2-5 850.3 μs 811.1 μs -4.6%
game-sm-success_2-6 290.0 μs 263.2 μs -9.2%
multisig-sm-1 537.6 μs 506.9 μs -5.7%
multisig-sm-2 526.6 μs 492.8 μs -6.4%
multisig-sm-3 531.8 μs 501.4 μs -5.7%
multisig-sm-4 538.6 μs 506.6 μs -5.9%
multisig-sm-5 751.1 μs 723.9 μs -3.6%
multisig-sm-6 535.7 μs 507.8 μs -5.2%
multisig-sm-7 524.3 μs 494.6 μs -5.7%
multisig-sm-8 529.8 μs 498.8 μs -5.9%
multisig-sm-9 535.9 μs 505.3 μs -5.7%
multisig-sm-10 750.1 μs 722.2 μs -3.7%
ping-pong-1 438.7 μs 415.9 μs -5.2%
ping-pong-2 439.2 μs 415.2 μs -5.5%
ping-pong_2-1 263.0 μs 246.6 μs -6.2%
prism-1 208.6 μs 188.4 μs -9.7%
prism-2 565.1 μs 533.1 μs -5.7%
prism-3 479.5 μs 448.6 μs -6.4%
pubkey-1 174.8 μs 159.5 μs -8.8%
stablecoin_1-1 1.174 ms 1.126 ms -4.1%
stablecoin_1-2 243.4 μs 219.9 μs -9.7%
stablecoin_1-3 1.349 ms 1.279 ms -5.2%
stablecoin_1-4 259.2 μs 233.8 μs -9.8%
stablecoin_1-5 1.696 ms 1.600 ms -5.7%
stablecoin_1-6 321.8 μs 290.4 μs -9.8%
stablecoin_2-1 1.178 ms 1.124 ms -4.6%
stablecoin_2-2 242.9 μs 219.7 μs -9.6%
stablecoin_2-3 1.344 ms 1.282 ms -4.6%
stablecoin_2-4 258.9 μs 234.2 μs -9.5%
token-account-1 242.1 μs 228.8 μs -5.5%
token-account-2 429.2 μs 402.7 μs -6.2%
uniswap-1 534.6 μs 509.0 μs -4.8%
uniswap-2 286.8 μs 267.6 μs -6.7%
uniswap-3 2.178 ms 2.034 ms -6.6%
uniswap-4 431.8 μs 388.2 μs -10.1%
uniswap-5 1.508 ms 1.414 ms -6.2%
uniswap-6 415.5 μs 373.1 μs -10.2%
vesting-1 459.9 μs 439.2 μs -4.5%

@effectfully
Copy link
Contributor Author

-6.5%. So some extra safety for costing does cost us some. But it's still worse than -7.18% from the other PR! Even though this one is supposed to be doing less. Not by much, but still should be a bit faster. Not sure if 0.68% of difference is a reliable measurement, though. Core doesn't tell me anything, but it does now contain some join points that I didn't see before and I really should study how those are compiled some time.

In any case good to know that the minimal changes are not really worse than the more radical ones. I'll return to both of the PRs once I'm done with doing unlifting both the ways.

@effectfully
Copy link
Contributor Author

effectfully commented Mar 31, 2022

I failed to stop thinking about it and I think I know what's going on. Here's how AddInteger is compiled:

let {
  lvl204_sssh
    = \ mem_igXF arg1_igXG ->
        join {
          $j_sl4A x_igXN
            = case (exF43_sl4C mem_igXF) `cast` <Co:7> of nt_igXR
              { __DEFAULT ->
              let { lvl205_sm1f = Left x_igXN } in
              let {
                lvl206_sm1h = (lvl205_sm1f, lvl13_ruuY `cast` <Co:3>) } in
              BuiltinRuntimeArrow
                (\ mem1_Xhhc _ ->
                   case (nt_igXR mem1_Xhhc) `cast` <Co:4> of
                   { ExBudget ipv2_igXZ ipv3_igY0 ->
                   BuiltinRuntimeResult
                     ipv2_igXZ ipv3_igY0 (lvl206_sm1h `cast` <Co:35>)
                   })
              } } in
        case arg1_igXG of {
          __DEFAULT -> jump $j_sl4A lvl14_ruv0;
          VCon val_ijiL ->
            case val_ijiL `cast` <Co:8> of { ValueOf uniAct_srPw x_srPx ->
            case $fGCompareTYPEDefaultUni_$cgeq
                   $WDefaultUniInteger (uniAct_srPw `cast` <Co:8>)
            of {
              Nothing ->
                jump $j_sl4A
                  (ErrorWithCause
                     (ReadKnownUnliftingError
                        (typeMismatchError
                           ($fGShowTYPEDefaultUni_$cshowsPrec `cast` <Co:6>)
                           ($WDefaultUniInteger `cast` <Co:9>)
                           uniAct_srPw))
                     mayCause_ruuZ);
              Just ds2_ihc4 ->
                case ds2_ihc4 of { Refl co8_ihc7 ->
                case (exF43_sl4C mem_igXF) `cast` <Co:7> of nt_ihc9
                { __DEFAULT ->
                BuiltinRuntimeArrow
                  (\ mem1_ihca arg2_ihcb ->
                     case arg2_ihcb of {
                       __DEFAULT ->
                         case (nt_ihc9 mem1_ihca) `cast` <Co:4> of
                         { ExBudget ipv2_ihck ipv3_ihcl ->
                         BuiltinRuntimeResult
                           ipv2_ihck ipv3_ihcl (lvl72_ruwg `cast` <Co:35>)
                         };
                       VCon val1_XjCF ->
                         case val1_XjCF `cast` <Co:8> of
                         { ValueOf uniAct1_srPD x1_srPE ->
                         case $fGCompareTYPEDefaultUni_$cgeq
                                $WDefaultUniInteger
                                (uniAct1_srPD `cast` <Co:8>)
                         of {
                           Nothing ->
                             case (nt_ihc9 mem1_ihca) `cast` <Co:4> of
                             { ExBudget ipv2_ihck ipv3_ihcl ->
                             BuiltinRuntimeResult
                               ipv2_ihck
                               ipv3_ihcl
                               ((Left
                                   (ErrorWithCause
                                      (ReadKnownUnliftingError
                                         (typeMismatchError
                                            ($fGShowTYPEDefaultUni_$cshowsPrec
                                             `cast` <Co:6>)
                                            ($WDefaultUniInteger
                                             `cast` <Co:9>)
                                            uniAct1_srPD))
                                      mayCause_ruuZ),
                                 lvl13_ruuY `cast` <Co:3>)
                                `cast` <Co:35>)
                             };
                           Just ds3_ihcO ->
                             case ds3_ihcO of { Refl co12_ihcR ->
                             case (nt_ihc9 mem1_ihca) `cast` <Co:4> of
                             { ExBudget ipv2_ihcU ipv3_ihcV ->
                             BuiltinRuntimeResult
                               ipv2_ihcU
                               ipv3_ihcV
                               (case plusInteger
                                       (x_srPx `cast` <Co:3>)
                                       (x1_srPE `cast` <Co:3>)
                                of vx_ihdb
                                { __DEFAULT ->
                                (Right
                                   (VCon
                                      ((ValueOf
                                          $s$fMakeKnownInDefaultUniterm(,)4
                                          vx_ihdb)
                                       `cast` <Co:13>)),
                                 lvl13_ruuY `cast` <Co:3>)
                                `cast` <Co:35>
                                })
                             }
                             }
                         }
                         }
                     })
                }
                }
            }
            }
        } } in
let { lvl205_srPI = BuiltinRuntimeArrow lvl204_sssh } in

Now the difference with what we have in master right now (and with the other branch) is that the non-initial BuiltinRuntimeArrow and the BuiltinRuntimeResult appear under a lambda (several of them), which means we have to reallocate them every time the builtin is applied, while previously that stuff would move to the top level and get shared across multiple applications. I thought our current approach was somewhat naive, but it seems to be a clever one in fact.

I'm closing this for now, but I'll probably look at it again in future.

@effectfully effectfully deleted the effectfully/builtins/one-BuiltinRuntime-to-rule-them-all branch March 31, 2022 15:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants