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

[Builtin] Introduce 'AssociateType' in 'KnownTypeIn' #4172

Conversation

effectfully
Copy link
Contributor

This drops the term argument from KnownTypeIn allowing us to
express more and require less. See the comments.

KnownType term a is recovered and user code is not affected
apart from KnownTypeIn DefaultUni instances being simpler and
some old testing code (that was already behaving weirdly) requiring
a few explicit type applications.

@@ -93,7 +93,7 @@ nopCostParameters = toMachineParameters $ CostModel defaultCekMachineCosts nopCo
arguments. We have checked this and there there was no dependence: let's
leave open the possibility of doing it again in case anything changes.
-}
instance uni ~ DefaultUni => ToBuiltinMeaning uni NopFuns where
instance KnownBuiltinTypeIn uni Integer => ToBuiltinMeaning uni NopFuns where
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This returns the original version just to show that it's possible. My latest PR made that impossible (which I only figured out after the PR was merged), so I thought it would be nice to fix that. Although it's not really important, we don't even have more than one universe, so who cares if we can or cannot define universe-polymorphic built-in functions.

@@ -100,7 +101,7 @@ insertBuiltin
insertBuiltin fun =
case toBuiltinMeaning fun of
BuiltinMeaning sch meta _ ->
case typeSchemeResult sch of
case typeSchemeResult @(Term TyName Name DefaultUni DefaultFun ()) sch of
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the type annotation. And it's the place that was triggering the OVERLAPPABLE instance that is now removed. So this piece of code was already quite weird (we're trying to infer term under the presence of a KnownType term a constraint and propagate that upwards where the constraint does not exist and that doesn't quite work).

Comment on lines -335 to -340
-- This constraint is just to get through 'KnownPolytype' stuck on an unknown type straight
-- to the custom type error that we have in the @Typed@ module. Though, somehow, the error
-- gets triggered even without the constraint when this function in used, but I don't
-- understand how that is possible and it does not work when the function from the @Debug@
-- module is used. So we're just doing the right thing here and adding the constraint.
, KnownMonotype term args res a
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 dropped that OVERLAPPING instance, so this is now obsolete.

@effectfully effectfully force-pushed the effectfully/builtins/introduce-AssociateType-2 branch from 7426ced to a279e9e Compare October 31, 2021 00:57
Copy link
Contributor

@michaelpj michaelpj left a comment

Choose a reason for hiding this comment

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

Seems fine, I mostly just find the AssociateType naming confusing.

This drops the `term` argument from `KnownTypeIn` allowing us to
express more and require less. See the comments.

`KnownType term a` is recovered and user code is not affected
apart from `KnownTypeIn DefaultUni` instances being simpler and
some old testing code (that was already behaving weirdly) requiring
a few explicit type applications.
@effectfully effectfully force-pushed the effectfully/builtins/introduce-AssociateType-2 branch from a279e9e to d073e26 Compare November 1, 2021 23:15
@effectfully effectfully merged commit 3959845 into IntersectMBO:master Nov 2, 2021
@effectfully effectfully deleted the effectfully/builtins/introduce-AssociateType-2 branch November 2, 2021 00:08
@effectfully
Copy link
Contributor Author

@kwxm could you run the benchmarks on this PR (even though it's already merged) to check that it's not what giving us the weird slowdown?

@kwxm
Copy link
Contributor

kwxm commented Nov 5, 2021

@kwxm could you run the benchmarks on this PR (even though it's already merged) to check that it's not what giving us the weird slowdown?

Sorry, I forgot to do this. I'm doing it now.

@kwxm
Copy link
Contributor

kwxm commented Nov 5, 2021

I compared this branch with its parent (90d7665) and there is a slowdown:

Script                              90d7665        PR 1472     Change
----------------------------------------------------------------------------------
auction_1-1                       489.7 μs	 534.0 μs      +9.0%
auction_1-2                       1.297 ms	 1.367 ms      +5.4%
auction_1-3                       1.311 ms	 1.381 ms      +5.3%
auction_1-4                       655.9 μs	 714.8 μs      +9.0%
auction_2-1                       492.6 μs	 537.8 μs      +9.2%
auction_2-2                       1.294 ms	 1.367 ms      +5.6%
auction_2-3                       1.631 ms	 1.715 ms      +5.2%
auction_2-4                       1.314 ms	 1.386 ms      +5.5%
auction_2-5                       659.3 μs	 719.4 μs      +9.1%
crowdfunding-success-1            591.0 μs	 644.9 μs      +9.1%
crowdfunding-success-2            591.4 μs	 644.0 μs      +8.9%
crowdfunding-success-3            590.3 μs	 643.5 μs      +9.0%
currency-1                        564.1 μs	 604.5 μs      +7.2%
escrow-redeem_1-1                 851.4 μs	 911.7 μs      +7.1%
escrow-redeem_1-2                 851.7 μs	 913.0 μs      +7.2%
escrow-redeem_2-1                 970.3 μs	 1.040 ms      +7.2%
escrow-redeem_2-2                 970.5 μs	 1.039 ms      +7.1%
escrow-redeem_2-3                 970.0 μs	 1.039 ms      +7.1%
escrow-refund-1                   436.7 μs	 474.7 μs      +8.7%
future-increase-margin-1          561.3 μs	 601.8 μs      +7.2%
future-increase-margin-2          1.202 ms	 1.281 ms      +6.6%
future-increase-margin-3          1.208 ms	 1.281 ms      +6.0%
future-increase-margin-4          1.083 ms	 1.141 ms      +5.4%
future-increase-margin-5          1.548 ms	 1.629 ms      +5.2%
future-pay-out-1                  564.1 μs	 606.0 μs      +7.4%
future-pay-out-2                  1.208 ms	 1.290 ms      +6.8%
future-pay-out-3                  1.207 ms	 1.287 ms      +6.6%
future-pay-out-4                  1.540 ms	 1.628 ms      +5.7%
future-settle-early-1             562.2 μs	 603.7 μs      +7.4%
future-settle-early-2             1.205 ms	 1.287 ms      +6.8%
future-settle-early-3             1.203 ms	 1.285 ms      +6.8%
future-settle-early-4             1.227 ms	 1.302 ms      +6.1%
game-sm-success_1-1               922.3 μs	 988.8 μs      +7.2%
game-sm-success_1-2               556.2 μs	 609.6 μs      +9.6%
game-sm-success_1-3               1.332 ms	 1.410 ms      +5.9%
game-sm-success_1-4               651.1 μs	 713.7 μs      +9.6%
game-sm-success_2-1               928.8 μs	 986.1 μs      +6.2%
game-sm-success_2-2               559.5 μs	 610.7 μs      +9.2%
game-sm-success_2-3               1.333 ms	 1.406 ms      +5.5%
game-sm-success_2-4               652.1 μs	 711.8 μs      +9.2%
game-sm-success_2-5               1.331 ms	 1.405 ms      +5.6%
game-sm-success_2-6               651.7 μs	 711.3 μs      +9.1%
multisig-sm-1                     920.4 μs	 970.9 μs      +5.5%
multisig-sm-2                     909.8 μs	 963.6 μs      +5.9%
multisig-sm-3                     916.3 μs	 970.9 μs      +6.0%
multisig-sm-4                     922.3 μs	 981.0 μs      +6.4%
multisig-sm-5                     1.167 ms	 1.236 ms      +5.9%
multisig-sm-6                     918.1 μs	 976.4 μs      +6.4%
multisig-sm-7                     907.2 μs	 963.5 μs      +6.2%
multisig-sm-8                     915.2 μs	 972.8 μs      +6.3%
multisig-sm-9                     921.5 μs	 982.0 μs      +6.6%
multisig-sm-10                    1.167 ms	 1.236 ms      +5.9%
ping-pong-1                       777.5 μs	 830.1 μs      +6.8%
ping-pong-2                       777.1 μs	 829.9 μs      +6.8%
ping-pong_2-1                     523.1 μs	 565.2 μs      +8.0%
prism-1                           464.3 μs	 509.3 μs      +9.7%
prism-2                           1.005 ms	 1.068 ms      +6.3%
prism-3                           882.3 μs	 941.7 μs      +6.7%
pubkey-1                          398.8 μs	 434.8 μs      +9.0%
stablecoin_1-1                    1.828 ms	 1.921 ms      +5.1%
stablecoin_1-2                    545.3 μs	 597.1 μs      +9.5%
stablecoin_1-3                    2.081 ms	 2.195 ms      +5.5%
stablecoin_1-4                    580.6 μs	 638.0 μs      +9.9%
stablecoin_1-5                    2.652 ms	 2.810 ms      +6.0%
stablecoin_1-6                    713.9 μs	 781.0 μs      +9.4%
stablecoin_2-1                    1.822 ms	 1.936 ms      +6.3%
stablecoin_2-2                    544.0 μs	 598.9 μs     +10.1%
stablecoin_2-3                    2.074 ms	 2.192 ms      +5.7%
stablecoin_2-4                    579.1 μs	 637.2 μs     +10.0%
token-account-1                   455.4 μs	 492.1 μs      +8.1%
token-account-2                   777.6 μs	 835.0 μs      +7.4%
uniswap-1                         836.4 μs	 887.2 μs      +6.1%
uniswap-2                         560.2 μs	 608.3 μs      +8.6%
uniswap-3                         3.317 ms	 3.495 ms      +5.4%
uniswap-4                         924.3 μs	 1.008 ms      +9.1%
uniswap-5                         2.454 ms	 2.583 ms      +5.3%
uniswap-6                         879.2 μs	 954.5 μs      +8.6%
vesting-1                         788.2 μs	 832.8 μs      +5.7%

The figures for the parent are comparable with the figures from master on the 1st of November (the first table in the other PR) and the ones for the PR are comparable with master on the 2nd of November (the second in the other PR).

What I don't understand is that I benchmarked both master and your branch on the 1st of November and then again on the second, and both branches slowed down, but I thought I hadn't updated your branch between the two lots of benchmarks. Perhaps I was wrong about that. Anyway, what I think happened was that I compared your branch against master twice and both times your branch was faster than master, but I hadn't noticed that that second time both branches had slowed down. I don't know where this leaves us overall.

@michaelpj
Copy link
Contributor

I can't see how this could have slowed us down, doesn't this only affect type-level stuff?

@effectfully
Copy link
Contributor Author

I can't see how this could have slowed us down, doesn't this only affect type-level stuff?

Not exactly. It moves constraints like HasConstantIn from the outside of KnownTypeIn to its methods, so this is probably what gives us such a dramatic slowdown.

MaximilianAlgehed pushed a commit to Quviq/plutus that referenced this pull request Mar 3, 2022
This drops the `term` argument from `KnownTypeIn` allowing us to
express more and require less. See the comments.

`KnownType term a` is recovered and user code is not affected
apart from `KnownTypeIn DefaultUni` instances being simpler and
some old testing code (that was already behaving weirdly) requiring
a few explicit type applications.
MaximilianAlgehed pushed a commit to Quviq/plutus that referenced this pull request Mar 3, 2022
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