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-2436: reorder constructors according to frequency #3435

Merged
merged 1 commit into from
Jun 26, 2021

Conversation

michaelpj
Copy link
Contributor

Seems to be noise but I guess it's worth having made an explicit decision 🤷

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

@michaelpj
Copy link
Contributor Author

crowdfunding/crowdfunding-success-1         677.6 μs       679.8 μs       +0.3%                                                                                                                                                                                                                                            
crowdfunding/crowdfunding-success-2         675.3 μs       678.0 μs       +0.4%                                                                                                                                                                                                                                            
crowdfunding/crowdfunding-success-3         678.7 μs       679.2 μs       +0.1%                                                                                                                                                                                                                                            
currency/currency-1                         609.7 μs       610.5 μs       +0.1%                                                                                                                                                                                                                                            
currency/currency-2                         810.2 μs       816.0 μs       +0.7%                                                                                                                                                                                                                                            
escrow/escrow-redeem_1-1                    970.9 μs       971.3 μs       +0.0%                                                                                                                                                                                                                                            
escrow/escrow-redeem_1-2                    972.6 μs       972.9 μs       +0.0%                                                                                                                                                                                                                                            
escrow/escrow-redeem_2-1                    1.119 ms       1.117 ms       -0.2%                                                                                                                                                                                                                                            
escrow/escrow-redeem_2-2                    1.123 ms       1.117 ms       -0.5%                                                                                                                                                                                                                                            
escrow/escrow-redeem_2-3                    1.121 ms       1.117 ms       -0.4%                                                                                                                                                                                                                                            
escrow/escrow-refund-1                      472.3 μs       469.4 μs       -0.6%                                                                                                                                                                                                                                            
future/future-increase-margin-1             613.6 μs       609.2 μs       -0.7%                                                                                                                                                                                                                                            
future/future-increase-margin-2             818.5 μs       816.4 μs       -0.3%                                                                                                                                                                                                                                            
future/future-increase-margin-3             1.419 ms       1.409 ms       -0.7%                                                                                                                                                                                                                                            
future/future-increase-margin-4             1.423 ms       1.411 ms       -0.8%                                                                                                                                                                                                                                            
future/future-increase-margin-5             1.307 ms       1.306 ms       -0.1%                                                                                                                                                                                                                                            
future/future-increase-margin-6             1.823 ms       1.818 ms       -0.3%
future/future-pay-out-1                     612.0 μs       610.7 μs       -0.2%
future/future-pay-out-2                     813.2 μs       814.2 μs       +0.1%
future/future-pay-out-3                     1.416 ms       1.408 ms       -0.6%
future/future-pay-out-4                     1.418 ms       1.408 ms       -0.7%
future/future-pay-out-5                     1.819 ms       1.818 ms       -0.1%
future/future-settle-early-1                612.1 μs       609.6 μs       -0.4%
future/future-settle-early-2                814.3 μs       814.1 μs       -0.0%
future/future-settle-early-3                1.414 ms       1.409 ms       -0.4%
future/future-settle-early-4                1.413 ms       1.408 ms       -0.4%
future/future-settle-early-5                1.437 ms       1.438 ms       +0.1%
game-sm/game-sm-success-1                   1.006 ms       1.007 ms       +0.1%
game-sm/game-sm-success-2                   625.1 μs       628.4 μs       +0.5%
game-sm/game-sm-success-3                   1.477 ms       1.478 ms       +0.1%
game-sm/game-sm-success-4                   748.7 μs       752.1 μs       +0.5%
game-sm/game-sm-success_2-1                 1.009 ms       1.010 ms       +0.1%
game-sm/game-sm-success_2-2                 624.4 μs       627.4 μs       +0.5%
game-sm/game-sm-success_2-3                 1.478 ms       1.481 ms       +0.2%
game-sm/game-sm-success_2-4                 750.0 μs       751.8 μs       +0.2%
game-sm/game-sm-success_2-5                 1.458 ms       1.452 ms       -0.4%
game-sm/game-sm-success_2-6                 752.2 μs       751.4 μs       -0.1%
multisig-sm/multisig-sm-01                  1.070 ms       1.068 ms       -0.2%
multisig-sm/multisig-sm-02                  1.077 ms       1.072 ms       -0.5%
multisig-sm/multisig-sm-03                  1.090 ms       1.088 ms       -0.2%
multisig-sm/multisig-sm-04                  1.113 ms       1.107 ms       -0.5%
multisig-sm/multisig-sm-05                  1.311 ms       1.307 ms       -0.3%
multisig-sm/multisig-sm-06                  1.068 ms       1.063 ms       -0.5%
multisig-sm/multisig-sm-07                  1.073 ms       1.068 ms       -0.5%
multisig-sm/multisig-sm-08                  1.186 ms       1.181 ms       -0.4%
multisig-sm/multisig-sm-09                  1.106 ms       1.101 ms       -0.5%
multisig-sm/multisig-sm-10                  1.307 ms       1.301 ms       -0.5%
ping-pong/ping-pong-1                       832.1 μs       828.9 μs       -0.4%
ping-pong/ping-pong-2                       832.5 μs       829.8 μs       -0.3%
ping-pong/ping-pong_2-1                     511.2 μs       511.9 μs       +0.1%
prism/prism-1                               484.5 μs       485.8 μs       +0.3%
prism/prism-2                               1.094 ms       1.093 ms       -0.1%
prism/prism-3                               998.0 μs       999.4 μs       +0.1%
pubkey/pubkey-1                             429.9 μs       429.5 μs       -0.1%
stablecoin/stablecoin_1-1                   2.118 ms       2.123 ms       +0.2%
stablecoin/stablecoin_1-2                   615.6 μs       613.9 μs       -0.3%
stablecoin/stablecoin_1-3                   2.397 ms       2.386 ms       -0.5%
stablecoin/stablecoin_1-4                   673.0 μs       671.2 μs       -0.3%
stablecoin/stablecoin_1-5                   3.039 ms       3.033 ms       -0.2%
stablecoin/stablecoin_1-6                   841.6 μs       838.2 μs       -0.4%
stablecoin/stablecoin_2-1                   2.128 ms       2.115 ms       -0.6%
stablecoin/stablecoin_2-2                   615.0 μs       612.2 μs       -0.5%
stablecoin/stablecoin_2-3                   2.393 ms       2.378 ms       -0.6%
stablecoin/stablecoin_2-4                   674.4 μs       669.2 μs       -0.8%
token-account/token-account-1               578.3 μs       573.3 μs       -0.9%
token-account/token-account-2               681.9 μs       680.3 μs       -0.2%
token-account/token-account-3               888.5 μs       882.4 μs       -0.7%
uniswap/uniswap-1                           681.8 μs       677.3 μs       -0.7%
uniswap/uniswap-2                           1.114 ms       1.113 ms       -0.1%
uniswap/uniswap-3                           752.4 μs       749.9 μs       -0.3%
uniswap/uniswap-4                           832.7 μs       837.2 μs       +0.5%
uniswap/uniswap-5                           3.895 ms       3.894 ms       -0.0%
uniswap/uniswap-6                           1.136 ms       1.133 ms       -0.3%
uniswap/uniswap-7                           2.799 ms       2.810 ms       +0.4%
uniswap/uniswap-8                           1.071 ms       1.073 ms       +0.2%
vesting/vesting-1                           904.9 μs       904.7 μs       -0.0%
marlowe/trustfund/trustfund-1               2.104 ms       2.106 ms       +0.1%
marlowe/trustfund/trustfund-2               1.522 ms       1.517 ms       -0.3%
marlowe/zerocoupon/zerocoupon-1             2.210 ms       2.199 ms       -0.5%
marlowe/zerocoupon/zerocoupon-2             1.325 ms       1.317 ms       -0.6%

@thealmarty
Copy link
Contributor

thealmarty commented Jun 25, 2021

So it seems it's slightly better in some cases and slightly worse in others? At this point it's hard to provide empirical evidence of what ordering is faster because we don't have a lot of contracts yet. It's definitely worth re-visiting when we have more contracts/know more about the actual use cases. So it's good to put the note in there.
[just saw that you already said it's mostly noise]

Copy link
Contributor

@thealmarty thealmarty left a comment

Choose a reason for hiding this comment

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

The note is awesome, very clear. Thanks!

@michaelpj
Copy link
Contributor Author

Yeah, it's unclear that it's noticeable at all, looks like random noise. But I think it's good to have the note there so we think about it in future too.

@michaelpj michaelpj merged commit 0a2333b into master Jun 26, 2021
Copy link
Contributor

@kwxm kwxm left a comment

Choose a reason for hiding this comment

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

This looks entirely harmless. For completeness, here's the number of times the compute step of the CEK machine encounters each constructor during execution of all the benchmark examples. I wanted this data for documentation purposes anyway, so I thought I might as well do it now.

Validation:

Script                     Total      Var     LamAbs   Apply    Force   Delay   Const  Builtin
----------------------------------------------------------------------------------------------
crowdfunding-success-1     22475     28.49%   28.25%   27.79%   9.28%   4.74%   0.79%   0.65%
crowdfunding-success-2     22475     28.49%   28.25%   27.79%   9.28%   4.74%   0.79%   0.65%
crowdfunding-success-3     22475     28.49%   28.25%   27.79%   9.28%   4.74%   0.79%   0.65%
currency-1                 21389     28.63%   27.98%   27.53%   9.60%   4.91%   0.75%   0.60%
currency-2                 28344     29.76%   26.93%   27.33%   9.83%   5.04%   0.57%   0.55%
escrow-redeem_1-1          31410     29.26%   27.39%   27.57%   9.51%   5.07%   0.61%   0.58%
escrow-redeem_1-2          31410     29.26%   27.39%   27.57%   9.51%   5.07%   0.61%   0.58%
escrow-redeem_2-1          36665     29.42%   27.25%   27.54%   9.56%   5.05%   0.60%   0.58%
escrow-redeem_2-2          36665     29.42%   27.25%   27.54%   9.56%   5.05%   0.60%   0.58%
escrow-redeem_2-3          36665     29.42%   27.25%   27.54%   9.56%   5.05%   0.60%   0.58%
escrow-refund-1            15999     27.75%   28.54%   27.88%   9.16%   5.22%   0.80%   0.66%
future-increase-margin-1   21389     28.63%   27.98%   27.53%   9.60%   4.91%   0.75%   0.60%
future-increase-margin-2   28344     29.76%   26.93%   27.33%   9.83%   5.04%   0.57%   0.55%
future-increase-margin-3   47930     29.90%   26.88%   27.40%   9.71%   5.00%   0.56%   0.54%
future-increase-margin-4   47930     29.90%   26.88%   27.40%   9.71%   5.00%   0.56%   0.54%
future-increase-margin-5   41551     29.45%   27.36%   27.58%   9.39%   5.23%   0.49%   0.51%
future-increase-margin-6   63549     30.03%   27.05%   27.47%   9.54%   5.02%   0.41%   0.50%
future-pay-out-1           21389     28.63%   27.98%   27.53%   9.60%   4.91%   0.75%   0.60%
future-pay-out-2           28344     29.76%   26.93%   27.33%   9.83%   5.04%   0.57%   0.55%
future-pay-out-3           47930     29.90%   26.88%   27.40%   9.71%   5.00%   0.56%   0.54%
future-pay-out-4           47930     29.90%   26.88%   27.40%   9.71%   5.00%   0.56%   0.54%
future-pay-out-5           59210     30.03%   27.05%   27.47%   9.52%   5.06%   0.38%   0.49%
future-settle-early-1      21389     28.63%   27.98%   27.53%   9.60%   4.91%   0.75%   0.60%
future-settle-early-2      28344     29.76%   26.93%   27.33%   9.83%   5.04%   0.57%   0.55%
future-settle-early-3      47930     29.90%   26.88%   27.40%   9.71%   5.00%   0.56%   0.54%
future-settle-early-4      47930     29.90%   26.88%   27.40%   9.71%   5.00%   0.56%   0.54%
future-settle-early-5      44385     29.56%   27.34%   27.55%   9.44%   5.13%   0.46%   0.52%
game-sm-success-1          31668     29.30%   27.65%   27.67%   9.37%   4.87%   0.58%   0.58%
game-sm-success-2          22275     29.06%   27.56%   27.42%   9.74%   4.88%   0.75%   0.58%
game-sm-success-3          48817     29.96%   27.00%   27.41%   9.65%   4.99%   0.47%   0.51%
game-sm-success-4          25938     29.18%   27.51%   27.43%   9.73%   4.80%   0.76%   0.59%
game-sm-success_2-1        31668     29.30%   27.65%   27.67%   9.37%   4.87%   0.58%   0.58%
game-sm-success_2-2        22275     29.06%   27.56%   27.42%   9.74%   4.88%   0.75%   0.58%
game-sm-success_2-3        48817     29.96%   27.00%   27.41%   9.65%   4.99%   0.47%   0.51%
game-sm-success_2-4        25938     29.18%   27.51%   27.43%   9.73%   4.80%   0.76%   0.59%
game-sm-success_2-5        49838     30.01%   26.96%   27.40%   9.67%   4.99%   0.46%   0.51%
game-sm-success_2-6        25938     29.18%   27.51%   27.43%   9.73%   4.80%   0.76%   0.59%
multisig-sm-01             32782     29.23%   27.68%   27.73%   9.21%   5.09%   0.52%   0.54%
multisig-sm-02             32811     29.28%   27.72%   27.81%   9.12%   4.99%   0.53%   0.56%
multisig-sm-03             33426     29.32%   27.69%   27.81%   9.12%   4.97%   0.53%   0.56%
multisig-sm-04             34041     29.36%   27.67%   27.82%   9.11%   4.96%   0.52%   0.56%
multisig-sm-05             42402     30.01%   27.05%   27.55%   9.47%   4.94%   0.45%   0.54%
multisig-sm-06             36227     29.34%   27.58%   27.75%   9.23%   5.00%   0.54%   0.57%
multisig-sm-07             32811     29.28%   27.72%   27.81%   9.12%   4.99%   0.53%   0.56%
multisig-sm-08             33426     29.32%   27.69%   27.81%   9.12%   4.97%   0.53%   0.56%
multisig-sm-09             34041     29.36%   27.67%   27.82%   9.11%   4.96%   0.52%   0.56%
multisig-sm-10             42402     30.01%   27.05%   27.55%   9.47%   4.94%   0.45%   0.54%
ping-pong-1                25396     29.11%   27.67%   27.71%   9.32%   5.02%   0.57%   0.59%
ping-pong-2                25396     29.11%   27.67%   27.71%   9.32%   5.02%   0.57%   0.59%
ping-pong_2-1              16887     27.81%   28.56%   27.88%   9.14%   5.27%   0.70%   0.64%
prism-1                    18169     28.87%   27.68%   27.38%   9.79%   5.00%   0.71%   0.57%
prism-2                    35083     29.32%   27.54%   27.49%   9.56%   4.95%   0.59%   0.54%
prism-3                    34673     29.72%   27.02%   27.29%   9.86%   4.99%   0.59%   0.53%
pubkey-1                   15576     28.17%   28.28%   27.78%   9.31%   5.03%   0.78%   0.65%
stablecoin_1-1             56718     30.32%   26.02%   28.97%   8.46%   4.03%   0.81%   1.38%
stablecoin_1-2             21847     28.99%   27.53%   27.44%   9.72%   4.95%   0.79%   0.59%
stablecoin_1-3             64244     30.58%   25.58%   29.26%   8.30%   3.85%   0.86%   1.58%
stablecoin_1-4             23609     29.07%   27.50%   27.37%   9.79%   4.93%   0.77%   0.57%
stablecoin_1-5             76654     30.95%   24.72%   29.90%   7.90%   3.52%   1.00%   2.00%
stablecoin_1-6             24885     29.15%   27.50%   27.41%   9.74%   4.85%   0.78%   0.57%
stablecoin_2-1             56718     30.32%   26.02%   28.97%   8.46%   4.03%   0.81%   1.38%
stablecoin_2-2             21847     28.99%   27.53%   27.44%   9.72%   4.95%   0.79%   0.59%
stablecoin_2-3             64244     30.58%   25.58%   29.26%   8.30%   3.85%   0.86%   1.58%
stablecoin_2-4             23609     29.07%   27.50%   27.37%   9.79%   4.93%   0.77%   0.57%
token-account-1            20345     28.55%   28.00%   27.56%   9.57%   4.96%   0.75%   0.61%
token-account-2            23570     29.34%   27.29%   27.39%   9.75%   5.04%   0.63%   0.56%
token-account-3            30057     29.38%   27.23%   27.41%   9.71%   5.13%   0.58%   0.55%
uniswap-1                  23477     28.76%   27.94%   27.47%   9.67%   4.83%   0.75%   0.58%
uniswap-2                  41111     30.58%   26.23%   27.31%   9.90%   4.95%   0.46%   0.57%
uniswap-3                  25761     28.88%   27.88%   27.39%   9.75%   4.80%   0.74%   0.56%
uniswap-4                  28986     29.48%   27.32%   27.26%   9.88%   4.89%   0.65%   0.52%
uniswap-5                 122404     31.71%   24.53%   29.03%   8.57%   3.90%   0.63%   1.63%
uniswap-6                  40111     29.45%   27.29%   27.28%   9.92%   4.70%   0.80%   0.56%
uniswap-7                  87114     31.46%   25.20%   28.89%   8.62%   3.70%   0.66%   1.48%
uniswap-8                  37729     29.52%   27.23%   27.33%   9.88%   4.65%   0.81%   0.58%
vesting-1                  29819     29.50%   27.25%   27.51%   9.56%   5.09%   0.53%   0.56%
marlowe/trustfund-1        66186     28.50%   27.64%   27.59%   9.43%   5.81%   0.49%   0.54%
marlowe/trustfund-2        45251     28.60%   27.20%   27.54%   9.70%   5.85%   0.41%   0.69%
marlowe/zerocoupon-1       70640     28.91%   26.98%   27.40%   9.77%   5.97%   0.40%   0.58%
marlowe/zerocoupon-2       39275     27.89%   27.73%   27.09%  10.05%   6.35%   0.44%   0.46%

There's quite a bit of repetition for scripts that only differ in wallet addresses (and maybe other hashes), but I just left them in.

These confirm the ordering in this PR: it seems pretty clear that Var, LamAbs and Apply are the most important, but the ordering probably doesn't matter a lot apart from that.

Nofib:

Script                   Total       Var     LamAbs   Apply    Force   Delay   Const  Builtin
----------------------------------------------------------------------------------------------
clausify-formula1       2280764     28.27%   32.52%   29.73%   5.56%   3.81%   0.00%   0.11%
clausify-formula2       2807130     28.36%   32.39%   29.72%   5.61%   3.79%   0.00%   0.13%
clausify-formula3       7796706     28.37%   32.36%   29.72%   5.62%   3.79%   0.00%   0.13%
clausify-formula4      10174203     29.73%   30.73%   29.56%   6.18%   3.46%   0.00%   0.35%
clausify-formula5      51115918     28.19%   32.57%   29.73%   5.56%   3.85%   0.00%   0.10%
knights-4x4             4145714     32.84%   24.35%   29.75%   7.79%   2.87%   0.67%   1.74%
knights-6x6            12875754     34.24%   23.84%   29.43%   8.03%   2.75%   0.22%   1.49%
knights-8x8            22038341     34.47%   23.76%   29.31%   8.13%   2.77%   0.14%   1.42%
primetest-05digits      1533903     27.07%   15.48%   35.48%   5.17%   4.55%   4.73%   7.52%
primetest-08digits      2691481     26.62%   14.41%   36.27%   4.70%   4.63%   5.14%   8.22%
primetest-10digits      3689287     26.20%   13.82%   36.65%   4.52%   4.80%   5.40%   8.61%
primetest-20digits      7485593     27.37%   14.27%   36.54%   4.45%   4.04%   5.04%   8.30%
primetest-30digits     10929997     27.64%   14.34%   36.57%   4.39%   3.81%   4.98%   8.26%
primetest-40digits     14666073     27.82%   14.43%   36.52%   4.40%   3.73%   4.91%   8.19%
primetest-50digits     13999383     26.61%   13.55%   36.97%   4.26%   4.40%   5.43%   8.79%
queens4x4-bjbt1          916873     33.63%   23.36%   28.89%   8.30%   4.30%   0.36%   1.17%
queens4x4-bjbt2          984597     33.71%   23.42%   28.91%   8.27%   4.17%   0.34%   1.18%
queens4x4-bm             942203     32.80%   23.65%   28.81%   8.46%   4.38%   0.66%   1.25%
queens4x4-bt             722667     33.78%   23.11%   29.24%   8.04%   4.01%   0.44%   1.38%
queens4x4-fc            2528130     33.41%   24.70%   28.37%   8.53%   3.98%   0.24%   0.77%
queens5x5-bjbt1        11502713     33.88%   23.04%   29.31%   8.00%   3.95%   0.41%   1.42%
queens5x5-bjbt2        12241271     33.91%   23.12%   29.29%   8.00%   3.89%   0.39%   1.39%
queens5x5-bm           10710115     32.73%   23.63%   28.88%   8.42%   4.35%   0.71%   1.29%
queens5x5-bt            9601780     34.07%   22.82%   29.66%   7.73%   3.62%   0.48%   1.61%
queens5x5-fc           32271360     33.47%   24.70%   28.51%   8.39%   3.88%   0.24%   0.82%

The frequencies are a bit more varied for nofib,and sometimes in a different order, but we don't care.

@@ -31,6 +31,22 @@ import PlutusCore.MkPlc
import qualified PlutusCore.Name as TPLC
import Universe

{- Note [Term constructor ordering and numbers]
Ordering of constructors has a small but real effect on efficiency.
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're doing this for the Term type, I suggest we also do it for StepKind and also CekValue in Cek/Internal.hs, and maybe also Frame (although that looks like a pretty sensible order already). I'm not sure how often the various CekValue types are seen, but we can find out.

Copy link
Contributor

@kwxm kwxm Jun 28, 2021

Choose a reason for hiding this comment

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

Actually the CekValue types are used in different ways, so it's tricky to tell how important the frequencies are. The only place we dispatch on the full range is in dischargeCekValue, which is irrelevant since it shoud only ever be called once per program. Apart from that VLamAbs and VBuiltin are used in the two cases of forceEvaluate (when you're applying a lambda or a builtin to an argument) and VDelay and VBuiltin are used in forceEvaluate (when you're applying force to something you've just reduced to a value). I think VCon is only ever used is in asConstant when function arguments are passed to the builtin evaluation machinery, and I have no idea if the tag of the constructor matters there. Builtins don't really get called a lot though, so maybe that doesn't matter.

Copy link
Contributor

Choose a reason for hiding this comment

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

Based on the figures for the Term type it looks as if a sensible order would be VLamAbs, VDelay, VCon, VBuiltin. Not sure about the order of the last two, if it even matters.

Also, does it matter what order we do matches in, or are they translated to something based on the constructor order?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, good idea about CekValue. I'll give that a try, and I agree with your analysis.

I believe the order of matches doesn't matter, it's just the order of the constructors.

@kwxm kwxm deleted the mpj/constructor-ordering branch October 12, 2021 04:28
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.

4 participants