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] Fuse 'AsConstant' and 'FromConstant' into 'HasConstantIn' #4417

Conversation

effectfully
Copy link
Contributor

We had to have two classes because of the way costing was aligned,
but that has changed, so now we can unite these classes.

I also dropped HasConstant, because we don't need it.

We had to have two classes because of the way costing was aligned,
but that has changed, so now we can unite these classes.

I also dropped 'HasConstant', because we don't need it.
@effectfully
Copy link
Contributor Author

/benchmark plutus-benchmark:validation

@iohk-devops
Copy link

Comparing benchmark results of 'plutus-benchmark:validation' on '0397f83b8' (base) and 'b1d046796' (PR)

Script 0397f83 b1d0467 Change
auction_1-1 280.1 μs 286.8 μs +2.4%
auction_1-2 937.8 μs 954.4 μs +1.8%
auction_1-3 934.3 μs 950.3 μs +1.7%
auction_1-4 365.1 μs 376.5 μs +3.1%
auction_2-1 279.6 μs 288.0 μs +3.0%
auction_2-2 936.9 μs 951.4 μs +1.5%
auction_2-3 1.189 ms 1.208 ms +1.6%
auction_2-4 933.5 μs 948.1 μs +1.6%
auction_2-5 366.1 μs 377.2 μs +3.0%
crowdfunding-success-1 331.8 μs 340.0 μs +2.5%
crowdfunding-success-2 332.0 μs 339.1 μs +2.1%
crowdfunding-success-3 331.6 μs 340.0 μs +2.5%
currency-1 368.2 μs 373.2 μs +1.4%
escrow-redeem_1-1 535.7 μs 546.6 μs +2.0%
escrow-redeem_1-2 534.6 μs 546.0 μs +2.1%
escrow-redeem_2-1 624.4 μs 636.6 μs +2.0%
escrow-redeem_2-2 624.4 μs 636.6 μs +2.0%
escrow-redeem_2-3 625.9 μs 635.5 μs +1.5%
escrow-refund-1 246.2 μs 250.5 μs +1.7%
future-increase-margin-1 368.5 μs 372.8 μs +1.2%
future-increase-margin-2 812.0 μs 828.2 μs +2.0%
future-increase-margin-3 813.7 μs 829.9 μs +2.0%
future-increase-margin-4 755.8 μs 760.7 μs +0.6%
future-increase-margin-5 1.155 ms 1.159 ms +0.3%
future-pay-out-1 368.9 μs 372.5 μs +1.0%
future-pay-out-2 814.0 μs 827.0 μs +1.6%
future-pay-out-3 813.5 μs 824.7 μs +1.4%
future-pay-out-4 1.148 ms 1.157 ms +0.8%
future-settle-early-1 366.4 μs 370.7 μs +1.2%
future-settle-early-2 811.6 μs 825.6 μs +1.7%
future-settle-early-3 814.2 μs 828.7 μs +1.8%
future-settle-early-4 891.1 μs 900.5 μs +1.1%
game-sm-success_1-1 599.0 μs 610.6 μs +1.9%
game-sm-success_1-2 310.4 μs 320.0 μs +3.1%
game-sm-success_1-3 936.6 μs 954.4 μs +1.9%
game-sm-success_1-4 362.8 μs 374.2 μs +3.1%
game-sm-success_2-1 601.4 μs 611.7 μs +1.7%
game-sm-success_2-2 312.3 μs 319.5 μs +2.3%
game-sm-success_2-3 941.9 μs 952.5 μs +1.1%
game-sm-success_2-4 363.9 μs 373.6 μs +2.7%
game-sm-success_2-5 939.1 μs 950.8 μs +1.2%
game-sm-success_2-6 363.3 μs 372.8 μs +2.6%
multisig-sm-1 606.3 μs 612.2 μs +1.0%
multisig-sm-2 594.1 μs 601.4 μs +1.2%
multisig-sm-3 599.5 μs 607.4 μs +1.3%
multisig-sm-4 606.1 μs 613.0 μs +1.1%
multisig-sm-5 834.3 μs 839.2 μs +0.6%
multisig-sm-6 607.4 μs 612.7 μs +0.9%
multisig-sm-7 595.1 μs 602.6 μs +1.3%
multisig-sm-8 597.2 μs 608.1 μs +1.8%
multisig-sm-9 607.8 μs 614.8 μs +1.2%
multisig-sm-10 830.5 μs 839.9 μs +1.1%
ping-pong-1 499.1 μs 509.6 μs +2.1%
ping-pong-2 499.1 μs 508.3 μs +1.8%
ping-pong_2-1 308.1 μs 315.4 μs +2.4%
prism-1 261.1 μs 268.5 μs +2.8%
prism-2 650.7 μs 661.1 μs +1.6%
prism-3 556.3 μs 569.6 μs +2.4%
pubkey-1 221.4 μs 226.4 μs +2.3%
stablecoin_1-1 1.313 ms 1.353 ms +3.0%
stablecoin_1-2 304.9 μs 313.1 μs +2.7%
stablecoin_1-3 1.500 ms 1.559 ms +3.9%
stablecoin_1-4 324.1 μs 331.8 μs +2.4%
stablecoin_1-5 1.906 ms 1.983 ms +4.0%
stablecoin_1-6 401.0 μs 411.4 μs +2.6%
stablecoin_2-1 1.307 ms 1.344 ms +2.8%
stablecoin_2-2 304.4 μs 313.0 μs +2.8%
stablecoin_2-3 1.493 ms 1.553 ms +4.0%
stablecoin_2-4 323.4 μs 332.7 μs +2.9%
token-account-1 283.9 μs 289.5 μs +2.0%
token-account-2 498.2 μs 505.0 μs +1.4%
uniswap-1 593.5 μs 599.1 μs +0.9%
uniswap-2 340.0 μs 348.6 μs +2.5%
uniswap-3 2.437 ms 2.528 ms +3.7%
uniswap-4 530.3 μs 547.5 μs +3.2%
uniswap-5 1.712 ms 1.777 ms +3.8%
uniswap-6 506.6 μs 519.8 μs +2.6%
vesting-1 522.0 μs 524.6 μs +0.5%

@effectfully
Copy link
Contributor Author

Damn it.

@effectfully
Copy link
Contributor Author

/benchmark plutus-benchmark:validation

@iohk-devops
Copy link

Comparing benchmark results of 'plutus-benchmark:validation' on '0397f83b8' (base) and '41a1f8d29' (PR)

Script 0397f83 41a1f8d Change
auction_1-1 281.0 μs 281.5 μs +0.2%
auction_1-2 935.4 μs 938.0 μs +0.3%
auction_1-3 932.4 μs 937.3 μs +0.5%
auction_1-4 364.8 μs 371.0 μs +1.7%
auction_2-1 279.5 μs 284.7 μs +1.9%
auction_2-2 944.0 μs 937.2 μs -0.7%
auction_2-3 1.196 ms 1.186 ms -0.8%
auction_2-4 939.9 μs 931.7 μs -0.9%
auction_2-5 365.9 μs 369.6 μs +1.0%
crowdfunding-success-1 332.1 μs 334.6 μs +0.8%
crowdfunding-success-2 331.1 μs 333.8 μs +0.8%
crowdfunding-success-3 331.2 μs 334.1 μs +0.9%
currency-1 367.8 μs 366.7 μs -0.3%
escrow-redeem_1-1 536.1 μs 539.6 μs +0.7%
escrow-redeem_1-2 537.3 μs 539.8 μs +0.5%
escrow-redeem_2-1 627.4 μs 629.7 μs +0.4%
escrow-redeem_2-2 626.4 μs 630.7 μs +0.7%
escrow-redeem_2-3 626.4 μs 629.2 μs +0.4%
escrow-refund-1 244.3 μs 247.8 μs +1.4%
future-increase-margin-1 370.0 μs 366.5 μs -0.9%
future-increase-margin-2 817.9 μs 813.1 μs -0.6%
future-increase-margin-3 815.5 μs 814.0 μs -0.2%
future-increase-margin-4 755.6 μs 751.5 μs -0.5%
future-increase-margin-5 1.151 ms 1.146 ms -0.4%
future-pay-out-1 368.3 μs 367.8 μs -0.1%
future-pay-out-2 814.7 μs 819.4 μs +0.6%
future-pay-out-3 814.8 μs 818.4 μs +0.4%
future-pay-out-4 1.146 ms 1.145 ms -0.1%
future-settle-early-1 367.1 μs 367.0 μs -0.0%
future-settle-early-2 812.7 μs 814.6 μs +0.2%
future-settle-early-3 813.5 μs 815.4 μs +0.2%
future-settle-early-4 888.7 μs 890.1 μs +0.2%
game-sm-success_1-1 598.1 μs 603.3 μs +0.9%
game-sm-success_1-2 312.4 μs 313.8 μs +0.4%
game-sm-success_1-3 943.7 μs 943.0 μs -0.1%
game-sm-success_1-4 365.4 μs 366.8 μs +0.4%
game-sm-success_2-1 600.5 μs 600.9 μs +0.1%
game-sm-success_2-2 312.4 μs 313.1 μs +0.2%
game-sm-success_2-3 941.2 μs 939.2 μs -0.2%
game-sm-success_2-4 363.9 μs 365.8 μs +0.5%
game-sm-success_2-5 938.2 μs 940.7 μs +0.3%
game-sm-success_2-6 362.8 μs 366.3 μs +1.0%
multisig-sm-1 603.6 μs 603.1 μs -0.1%
multisig-sm-2 596.5 μs 592.8 μs -0.6%
multisig-sm-3 602.0 μs 601.4 μs -0.1%
multisig-sm-4 609.8 μs 607.5 μs -0.4%
multisig-sm-5 835.4 μs 831.1 μs -0.5%
multisig-sm-6 607.3 μs 605.5 μs -0.3%
multisig-sm-7 595.8 μs 595.2 μs -0.1%
multisig-sm-8 601.2 μs 600.0 μs -0.2%
multisig-sm-9 609.7 μs 605.3 μs -0.7%
multisig-sm-10 837.8 μs 828.2 μs -1.1%
ping-pong-1 503.5 μs 500.1 μs -0.7%
ping-pong-2 503.1 μs 500.2 μs -0.6%
ping-pong_2-1 310.8 μs 309.5 μs -0.4%
prism-1 261.4 μs 262.4 μs +0.4%
prism-2 652.7 μs 649.6 μs -0.5%
prism-3 556.8 μs 559.6 μs +0.5%
pubkey-1 222.9 μs 221.3 μs -0.7%
stablecoin_1-1 1.308 ms 1.312 ms +0.3%
stablecoin_1-2 305.9 μs 306.5 μs +0.2%
stablecoin_1-3 1.499 ms 1.504 ms +0.3%
stablecoin_1-4 324.8 μs 324.5 μs -0.1%
stablecoin_1-5 1.904 ms 1.914 ms +0.5%
stablecoin_1-6 402.3 μs 405.1 μs +0.7%
stablecoin_2-1 1.312 ms 1.320 ms +0.6%
stablecoin_2-2 305.9 μs 307.5 μs +0.5%
stablecoin_2-3 1.501 ms 1.513 ms +0.8%
stablecoin_2-4 325.1 μs 326.3 μs +0.4%
token-account-1 285.4 μs 285.1 μs -0.1%
token-account-2 499.3 μs 500.2 μs +0.2%
uniswap-1 595.4 μs 593.4 μs -0.3%
uniswap-2 341.7 μs 341.7 μs 0.0%
uniswap-3 2.450 ms 2.443 ms -0.3%
uniswap-4 533.2 μs 535.4 μs +0.4%
uniswap-5 1.720 ms 1.715 ms -0.3%
uniswap-6 509.9 μs 511.3 μs +0.3%
vesting-1 524.0 μs 518.5 μs -1.0%

@effectfully
Copy link
Contributor Author

^ no change in performance.

Good to know that a "harmless" equality constraint can cost us that much.

Ready for review.

-- Switching from 'MonadError' to 'Either' here gave us a speedup of 2-4%.
-- | Unlift from the 'Constant' constructor throwing an 'UnliftingError' if the provided @term@
-- is not a 'Constant'.
asConstant
:: AsUnliftingError err
=> Maybe cause -> term -> Either (ErrorWithCause err cause) (Some (ValueOf (UniOf term)))

class FromConstant term where
-- | Wrap a Haskell value as a @term@.
fromConstant :: Some (ValueOf (UniOf term)) -> term
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe asConstant should be toConstant for symmetry.

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 use the convention of calling things that can fail as* and things that can't to*/from*. We have that with errors too, AsParseError, AsTypeError etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see

@michaelpj michaelpj merged commit d008ca5 into master Feb 21, 2022
@effectfully effectfully deleted the effectfully/builtins/fuse-AsConstant-and-FromConstant-into-HasConstantIn branch February 21, 2022 11:03
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.

3 participants