Skip to content

Fast path for Reductionops kernel-defined functions.#22143

Draft
ppedrot wants to merge 1 commit into
rocq-prover:masterfrom
ppedrot:vm-fast-nf-type
Draft

Fast path for Reductionops kernel-defined functions.#22143
ppedrot wants to merge 1 commit into
rocq-prover:masterfrom
ppedrot:vm-fast-nf-type

Conversation

@ppedrot

@ppedrot ppedrot commented Jun 19, 2026

Copy link
Copy Markdown
Member

We do not call normalization when the argument is already in head normal form. Note that for some reason the code still relies on the term being evar-expanded, so we do implement this carefully.

This fast path seems to be quite important for VM normalization, as most constructors of inductive types already have a type in head normal form. On a micro-benchmark designed to measure VM reification of big first-order terms, this gives a 20% speedup.

Here is the code of the micro-benchmark:

Inductive tree := leaf | node : tree -> tree -> tree.

Fixpoint big (n : nat) :=
match n with
| 0 => leaf
| S n =>
  let t := big n in
  node t t
end.

Inductive hide A := Hide : A -> hide A.

Arguments Hide {A _}.

Definition test := Eval lazy in @Hide tree (big 22).

Time Eval vm_compute in test.

We do not call normalization when the argument is already in head normal
form. Note that for some reason the code still relies on the term being
evar-expanded, so we do implement this carefully.

This fast path seems to be quite important for VM normalization, as most
constructors of inductive types already have a type in head normal form.
On a micro-benchmark designed to measure VM reification of big first-order
terms, this gives a 20% speedup.
@ppedrot ppedrot added request: full CI Use this label when you want your next push to trigger a full CI. kind: experiment labels Jun 19, 2026
@coqbot-app coqbot-app Bot removed the request: full CI Use this label when you want your next push to trigger a full CI. label Jun 19, 2026
@ppedrot

ppedrot commented Jun 19, 2026

Copy link
Copy Markdown
Member Author

@coqbot bench

Comment thread pretyping/vnorm.ml
| Const _ | Case _ | Fix _ | CoFix _ | Proj _ ->
Reductionops.clos_whd_flags RedFlags.all env sigma t
end
| Rel _ | Evar _ | Cast _ | LetIn _ | Case _ | Proj _ | Const _ | Var _ ->

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

isn't Evar in whnf? since it's from EConstr.kind it's undefined

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Technically in vnorm we only ever reduce types whose normal forms are either Π or (applied) inductives, so in practice it doesn't matter.

Comment thread pretyping/vnorm.ml
| Const _ | Case _ | Fix _ | CoFix _ | Proj _ ->
Reductionops.clos_whd_flags RedFlags.all env sigma t
end
| Rel _ | Evar _ | Cast _ | LetIn _ | Case _ | Proj _ | Const _ | Var _ ->

@SkySkimmer SkySkimmer Jun 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

would it also be worth checking definedness of Rel/Var/Const?

@coqbot-app

coqbot-app Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

🏁 Bench results:

┌─────────────────────────────────────┬─────────────────────────┬───────────────────────────────────────┬─────────────────────────┐
│                                     │      user time [s]      │           CPU instructions            │  max resident mem [KB]  │
│                                     │                         │                                       │                         │
│            package_name             │   NEW      OLD    PDIFF │      NEW             OLD        PDIFF │   NEW      OLD    PDIFF │
├─────────────────────────────────────┼─────────────────────────┼───────────────────────────────────────┼─────────────────────────┤
│             rocq-mathcomp-ssreflect │    1.11     1.18  -5.93 │     7693074286      7688441251   0.06 │  592444   592364   0.01 │
│                           rocq-core │    6.71     6.90  -2.75 │    41370868596     41377663878  -0.02 │  444548   445096  -0.12 │
│                      rocq-equations │    8.53     8.75  -2.51 │    59084297577     59073592324   0.02 │  400596   399920   0.17 │
│         coq-rewriter-perf-SuperFast │  453.64   464.66  -2.37 │  3553069671628   3657914649410  -2.87 │ 1255532  1244844   0.86 │
│                 rocq-mathcomp-order │   80.99    82.83  -2.22 │   600911562914    600920492364  -0.00 │ 1618784  1619316  -0.03 │
│               coq-mathcomp-analysis │ 1224.93  1235.32  -0.84 │  9032054658071   9032148517274  -0.00 │ 2112040  2112172  -0.01 │
│               coq-engine-bench-lite │  126.78   127.67  -0.70 │   945277011040    951854854558  -0.69 │ 1108836  1004816  10.35 │
│  rocq-mathcomp-group-representation │  103.55   104.11  -0.54 │   729973994836    729934897502   0.01 │ 1711572  1707904   0.21 │
│                       coq-fourcolor │ 1349.58  1354.67  -0.38 │ 12426116001308  12426182645110  -0.00 │ 1020248  1020188   0.01 │
│                           coq-color │  229.82   230.60  -0.34 │  1458129672727   1458149172958  -0.00 │ 1162284  1162404  -0.01 │
│          rocq-metarocq-translations │   15.12    15.17  -0.33 │   109024541656    109051796329  -0.02 │  780156   781360  -0.15 │
│                        rocq-bignums │   25.23    25.31  -0.32 │   160547599346    160501740894   0.03 │  463156   459384   0.82 │
│                      coq-coquelicot │   38.86    38.97  -0.28 │   235238661876    235482605664  -0.10 │  828072   827264   0.10 │
│              rocq-mathcomp-solvable │   98.74    98.95  -0.21 │   666262218093    666211842117   0.01 │ 1097056  1096824   0.02 │
│                 rocq-mathcomp-field │  193.33   193.69  -0.19 │  1453352148570   1453171807065   0.01 │ 2287776  2291824  -0.18 │
│                  rocq-mathcomp-boot │   39.59    39.66  -0.18 │   233289855604    233321895693  -0.01 │  660372   662304  -0.29 │
│               rocq-mathcomp-algebra │  391.02   391.71  -0.18 │  2894607333943   2893035491243   0.05 │ 1594804  1594640   0.01 │
│              rocq-metarocq-template │   81.66    81.79  -0.16 │   560994447234    560984200853   0.00 │ 1099028  1098864   0.01 │
│                 rocq-metarocq-pcuic │  601.95   602.50  -0.09 │  3883467128673   3883462430381   0.00 │ 1788820  1788444   0.02 │
│              coq-mathcomp-odd-order │  604.50   604.88  -0.06 │  4284789322297   4284645812969   0.00 │ 2655052  2656656  -0.06 │
│                        coq-compcert │  305.38   305.54  -0.05 │  1998073755771   1998728787703  -0.03 │ 1196976  1197276  -0.03 │
│          rocq-mathcomp-finite-group │   26.48    26.49  -0.04 │   172595341793    172566774427   0.02 │  568740   568604   0.02 │
│                 rocq-metarocq-utils │   24.66    24.65   0.04 │   160089805291    160112345354  -0.01 │  586060   582968   0.53 │
│                    coq-fiat-parsers │  272.98   272.84   0.05 │  2093411761927   2092676073910   0.04 │ 2272348  2036920  11.56 │
│                             coq-vst │  835.92   835.38   0.06 │  6328490948295   6328570623616  -0.00 │ 2160060  2160492  -0.02 │
│          coq-performance-tests-lite │  888.27   886.91   0.15 │  7117998848542   7132507570572  -0.20 │ 1494184  1568932  -4.76 │
│                         rocq-stdlib │  418.72   418.05   0.16 │  1508839385462   1509459775498  -0.04 │  760752   760868  -0.02 │
│                rocq-metarocq-common │   41.75    41.68   0.17 │   268792321558    268819539282  -0.01 │  906764   905468   0.14 │
│                    coq-math-classes │   82.27    82.10   0.21 │   498372733557    498374625681  -0.00 │  513492   513984  -0.10 │
│                   coq-iris-examples │  369.02   368.25   0.21 │  2409141193128   2410934938474  -0.07 │ 1065752  1071500  -0.54 │
│               rocq-metarocq-erasure │  437.74   436.66   0.25 │  2984856214702   2984901637460  -0.00 │ 1822852  1824752  -0.10 │
│        coq-fiat-crypto-with-bedrock │ 7258.00  7233.80   0.33 │ 59627105315215  59670631050700  -0.07 │ 2827136  2827092   0.00 │
│                 coq-category-theory │  664.05   661.79   0.34 │  4952916500689   4952499817292   0.01 │ 6748544  6749200  -0.01 │
│                            coq-corn │  642.39   640.18   0.35 │  4328987475501   4329388821682  -0.01 │  619376   619388  -0.00 │
│                         coq-unimath │ 1898.03  1891.17   0.36 │ 15699535314936  15699737641539  -0.00 │ 2036496  2036832  -0.02 │
│                        rocq-runtime │   76.12    75.76   0.48 │   551253092362    551166946207   0.02 │  496124   494568   0.31 │
│                       coq-fiat-core │   55.58    55.31   0.49 │   336658840169    336669740591  -0.00 │  481008   483132  -0.44 │
│                        coq-rewriter │  331.93   330.08   0.56 │  2464042100073   2463450528488   0.02 │ 1440544  1471820  -2.12 │
│                        coq-coqprime │   56.93    56.61   0.57 │   392011745906    392418000209  -0.10 │  828380   830688  -0.28 │
│                         coq-coqutil │   47.38    47.11   0.57 │   293286814636    293385784827  -0.03 │  568216   569984  -0.31 │
│                            coq-hott │  156.87   155.89   0.63 │  1051747888632   1051802354642  -0.01 │  460556   460652  -0.02 │
│ coq-neural-net-interp-computed-lite │  239.39   237.08   0.97 │  2265181590030   2265162377788   0.00 │  883168   883164   0.00 │
│                           rocq-elpi │   16.50    16.34   0.98 │   116305902464    116269858897   0.03 │  451072   451328  -0.06 │
│           rocq-metarocq-safechecker │  311.74   308.54   1.04 │  2316120699900   2316498353286  -0.02 │ 1703648  1703488   0.01 │
│                            coq-core │    2.76     2.69   2.60 │    18615028284     18612969274   0.01 │   90972    91076  -0.11 │
└─────────────────────────────────────┴─────────────────────────┴───────────────────────────────────────┴─────────────────────────┘

INFO: failed to install
coq-bedrock2 (in NEW)

🐢 Top 25 slow downs
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                            TOP 25 SLOW DOWNS                                                             │
│                                                                                                                                          │
│   OLD      NEW    DIFF     %DIFF      Ln                    FILE                                                                         │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│      236    242  5.4710       2.32%   141  coq-fiat-crypto-with-bedrock/src/UnsaturatedSolinasHeuristics/Tests.v.html                    │
│      133    136  2.7157       2.04%   155  coq-fiat-crypto-with-bedrock/src/UnsaturatedSolinasHeuristics/Tests.v.html                    │
│      202    204  1.8866       0.94%     8  coq-neural-net-interp-computed-lite/theories/MaxOfTwoNumbersSimpler/Computed/AllLogits.v.html │
│     80.5   81.8  1.2832       1.59%    20  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/NBE.v.html                                   │
│      106    107  1.0511       0.99%    22  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/ArithWithCasts.v.html                        │
│     83.0   83.8  0.8795       1.06%    48  coq-fiat-crypto-with-bedrock/src/Curves/Weierstrass/AffineProofs.v.html                       │
│     7.83   8.68  0.8435      10.77%  1331  coq-mathcomp-odd-order/theories/PFsection9.v.html                                             │
│     20.4   21.0  0.6397       3.14%    24  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/MultiRetSplit.v.html                         │
│     16.3   17.0  0.6325       3.87%   762  coq-fiat-crypto-with-bedrock/src/Bedrock/Secp256k1/JoyeLadder.v.html                          │
│     3.62   4.24  0.6219      17.18%   196  rocq-stdlib/theories/ZArith/ZModOffset.v.html                                                 │
│     55.0   55.6  0.6119       1.11%    27  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/ToFancyWithCasts.v.html                      │
│     89.9   90.4  0.5509       0.61%   999  coq-performance-tests-lite/src/fiat_crypto_via_setoid_rewrite_standalone.v.html               │
│     22.3   22.8  0.5459       2.45%   776  coq-fiat-crypto-with-bedrock/src/Bedrock/Secp256k1/JoyeLadder.v.html                          │
│     21.4   21.9  0.5359       2.50%   651  rocq-stdlib/theories/Zmod/ZmodBase.v.html                                                     │
│     23.5   24.0  0.5184       2.20%   782  coq-fiat-crypto-with-bedrock/src/Bedrock/Secp256k1/JoyeLadder.v.html                          │
│     43.1   43.6  0.4981       1.16%     2  coq-fiat-crypto-with-bedrock/src/ExtractionJsOfOCaml/fiat_crypto.v.html                       │
│     53.8   54.3  0.4799       0.89%   296  coq-fiat-crypto-with-bedrock/src/Bedrock/Secp256k1/Addchain.v.html                            │
│     21.1   21.6  0.4764       2.26%   516  coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/EdwardsXYZT.v.html                    │
│     9.25   9.71  0.4589       4.96%   435  coq-mathcomp-odd-order/theories/PFsection12.v.html                                            │
│     54.7   55.2  0.4500       0.82%   512  coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/EdwardsXYZT.v.html                    │
│    0.761   1.20  0.4438      58.34%   215  rocq-stdlib/theories/setoid_ring/Ncring_tac.v.html                                            │
│     21.4   21.8  0.4006       1.87%    49  coq-fiat-crypto-with-bedrock/src/Curves/Weierstrass/AffineProofs.v.html                       │
│     44.7   45.1  0.3825       0.86%     3  coq-fiat-crypto-with-bedrock/src/ExtractionJsOfOCaml/bedrock2_fiat_crypto.v.html              │
│ 0.000083  0.380  0.3804  458325.30%    81  coq-mathcomp-analysis/theories/hoelder.v.html                                                 │
│     22.8   23.1  0.3433       1.51%    85  coq-fiat-crypto-with-bedrock/src/Curves/Montgomery/AffineProofs.v.html                        │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
🐇 Top 25 speed ups
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                           TOP 25 SPEED UPS                                                           │
│                                                                                                                                      │
│  OLD     NEW     DIFF     %DIFF   Ln                    FILE                                                                         │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│  27.9     24.7  -3.2411  -11.61%  129  coq-fiat-crypto-with-bedrock/src/Curves/Weierstrass/Projective.v.html                         │
│  2.45     1.36  -1.0972  -44.73%  574  coq-fiat-crypto-with-bedrock/src/Bedrock/P256/Jacobian.v.html                                 │
│  43.9     43.1  -0.8218   -1.87%  578  coq-fiat-crypto-with-bedrock/rupicola/bedrock2/compiler/src/compiler/MMIO.v.html              │
│  9.78     9.03  -0.7501   -7.67%   87  coq-engine-bench-lite/coq/PerformanceDemos/quadratic_reduction.v.html                         │
│  64.6     63.9  -0.6720   -1.04%  608  coq-fiat-crypto-with-bedrock/rupicola/bedrock2/bedrock2/src/bedrock2Examples/lightbulb.v.html │
│  2.48     1.89  -0.5936  -23.91%   32  coq-fiat-crypto-with-bedrock/src/Rewriter/Passes/NBE.v.html                                   │
│  48.1     47.6  -0.5320   -1.11%  277  coq-fiat-crypto-with-bedrock/src/Bedrock/P256/Jacobian.v.html                                 │
│  27.7     27.2  -0.4868   -1.76%   34  coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/MontgomeryLadderRISCV.v.html          │
│  18.7     18.2  -0.4802   -2.57%   32  coq-performance-tests-lite/src/pattern.v.html                                                 │
│  29.6     29.2  -0.4507   -1.52%   31  coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/MontgomeryLadderRISCV.v.html          │
│  26.1     25.6  -0.4505   -1.73%   13  coq-fourcolor/theories/proof/job466to485.v.html                                               │
│  90.6     90.2  -0.4072   -0.45%  968  coq-performance-tests-lite/src/fiat_crypto_via_setoid_rewrite_standalone.v.html               │
│  37.0     36.6  -0.3899   -1.05%  139  coq-fiat-parsers/src/Parsers/Refinement/SharpenedJSON.v.html                                  │
│  36.7     36.4  -0.3796   -1.03%  222  coq-fiat-crypto-with-bedrock/src/Bedrock/P256/Coord32.v.html                                  │
│ 0.995    0.615  -0.3792  -38.12%   41  rocq-stdlib/theories/ZArith/Zdiv_facts.v.html                                                 │
│  26.8     26.4  -0.3588   -1.34%  374  coq-unimath/UniMath/ModelCategories/Generated/LNWFSMonoidalStructure.v.html                   │
│ 0.561    0.221  -0.3403  -60.61%    4  rocq-stdlib/theories/extraction/ExtrHaskellZInteger.v.html                                    │
│ 0.344  0.00481  -0.3392  -98.60%  112  coq-mathcomp-analysis/theories/esum.v.html                                                    │
│  29.3     29.0  -0.3269   -1.12%  145  coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/GarageDoorTop.v.html                  │
│ 0.587    0.262  -0.3256  -55.43%   13  rocq-stdlib/theories/ZArith/Zmin.v.html                                                       │
│  30.2     29.9  -0.3250   -1.08%   13  coq-fourcolor/theories/proof/job165to189.v.html                                               │
│ 0.679    0.355  -0.3234  -47.63%  719  rocq-stdlib/theories/MSets/MSetRBT.v.html                                                     │
│  25.6     25.2  -0.3124   -1.22%  224  coq-fiat-crypto-with-bedrock/src/Bedrock/P256/Coord32.v.html                                  │
│  31.0     30.7  -0.3096   -1.00%  223  coq-fiat-crypto-with-bedrock/src/Bedrock/P256/Coord32.v.html                                  │
│  4.27     3.97  -0.3065   -7.17%  576  coq-fiat-crypto-with-bedrock/src/Bedrock/End2End/X25519/EdwardsXYZT.v.html                    │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

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