# Folding small modules into their only importer Issue S-5 (#14). Measured on main at `ba4a037`: 582 modules fold into 343 files of 68 families, and the tree goes from 54.73 T instructions over 2,587 files to 50.94 T over 2,005 (−6.9%; T is 10¹²). ## The rule A module is folded when exactly one file of the repository imports it, that file is in the same folder and the same family (first word of the file name), the module has at most 200 lines, the file that takes it stays within 600 lines, and neither states results (no file with `Comparator`, `Solution`, `Theorem`, `PaperResult`, `Results` or `Statement` in its name). The module's text goes into the file unchanged, under a heading that names it, in a `section` of its own, before the file's own text; the scopes it left open at its end are closed there; the file takes over its imports. Chains fold from the leaf up while the limits hold. ## Why it is cheaper Every module loads what it imports before its first line is read. The cheapest modules of this tree cost about 4 G instructions with next to nothing in them, and the tree had 2,587 modules: that fixed cost is about a fifth of the whole. Lean Pool's copy of this proof went the same way (fewer modules, trimmed imports) and its benchmark found the gain there; Treant's `merge` stage folded 13 thin wrappers. The folds here were computed on this tree from its own import graph. ## Doing it again ```sh python3 -I docs/s5-folding/fold.py self-test python3 -I docs/s5-folding/fold.py plan . /tmp/plan.json --small 200 --cap 600 # what would fold on the tree as it stands python3 -I docs/s5-folding/fold.py apply . /tmp/plan.json # do it python3 -I docs/s5-folding/fold.py check . # is the working tree main's text, folded? ``` Each pull request of the series keeps its plan in its route notes (`routes//plan.json`). A fold whose file did not build was taken back before the count (0 files). ## What is checked `fold.py check` makes every changed file again from main's text and compares byte for byte: a fold moves text and edits none. The `build` check compiles the result, and the four theorems Comparator judges are judged again on main after a merge, as for every change. ## By family | family | files that take modules in | modules folded | saved | |---|---|---|---| | `Euler/Packet*` | 98 | 173 | 1,162.62 G | | `Euler/Mean*` | 32 | 50 | 329.88 G | | `Euler/Transverse*` | 26 | 41 | 265.61 G | | `NavierStokes/R3` | 19 | 42 | 225.88 G | | `Euler/Ordinary*` | 14 | 27 | 186.14 G | | `Euler/Parent*` | 13 | 22 | 155.52 G | | `Euler/Cylinder*` | 15 | 18 | 114.99 G | | `Euler/Base*` | 8 | 14 | 100.25 G | | `NavierStokes/Sharp*` | 3 | 15 | 87.29 G | | `Euler/Smooth*` | 8 | 12 | 75.62 G | | `Euler/Gevrey*` | 9 | 11 | 68.18 G | | `NavierStokes/Generic*` | 3 | 12 | 67.72 G | | `Euler/Source*` | 5 | 9 | 66.09 G | | `Euler/Drift*` | 2 | 9 | 57.48 G | | `Euler/Sobolev*` | 7 | 9 | 52.95 G | | `Euler/Whole*` | 3 | 8 | 52.72 G | | `Euler/All*` | 2 | 6 | 38.35 G | | `Euler/Correction*` | 4 | 6 | 37.95 G | | `Euler/Compact*` | 3 | 5 | 34.91 G | | `NavierStokes/Whole*` | 3 | 6 | 34.28 G | | `Euler/Lp*` | 5 | 5 | 32.77 G | | `Euler/Parameter*` | 3 | 5 | 31.17 G | | `Euler/Physical*` | 3 | 4 | 27.62 G | | `NavierStokes/Local*` | 2 | 4 | 27.05 G | | `Euler/Time*` | 2 | 4 | 25.92 G | | `Euler/Static*` | 3 | 3 | 20.97 G | | `Euler/Regularized*` | 2 | 3 | 19.87 G | | `Euler/Field*` | 1 | 3 | 19.18 G | | `Euler/Continuous*` | 3 | 3 | 18.81 G | | `Euler/Stage*` | 1 | 3 | 18.09 G | | `Euler/Heat*` | 1 | 2 | 17.33 G | | `NavierStokes/Axis*` | 2 | 3 | 15.54 G | | `Euler/Euler*` | 1 | 2 | 15.02 G | | `Euler/Coefficient*` | 1 | 2 | 14.09 G | | `Euler/Gaussian*` | 2 | 2 | 13.43 G | | `Euler/Bounded*` | 1 | 2 | 13.15 G | | `Euler/Linear*` | 1 | 2 | 12.58 G | | `Euler/H6*` | 1 | 2 | 12.45 G | | `Euler/Inviscid*` | 2 | 2 | 11.50 G | | `NavierStokes/Particular*` | 1 | 2 | 11.14 G | | `NavierStokes/Reference*` | 1 | 2 | 10.57 G | | `NavierStokes/Periodic*` | 1 | 1 | 10.14 G | | `Euler/Canonical*` | 1 | 1 | 7.72 G | | `Euler/Lifted*` | 1 | 1 | 6.91 G | | `Euler/Top*` | 1 | 1 | 6.70 G | | `Euler/Mixed*` | 1 | 1 | 6.69 G | | `Euler/H5*` | 1 | 1 | 6.56 G | | `Euler/Fixed*` | 1 | 1 | 6.53 G | | `Euler/Gained*` | 1 | 1 | 6.40 G | | `Euler/Small*` | 1 | 1 | 6.39 G | | `Euler/Projected*` | 1 | 1 | 6.35 G | | `Euler/External*` | 1 | 1 | 6.34 G | | `Euler/Terminal*` | 1 | 1 | 6.33 G | | `Euler/Quadratic*` | 1 | 1 | 6.28 G | | `Euler/Graph*` | 1 | 1 | 6.28 G | | `Euler/Elapsed*` | 1 | 1 | 6.26 G | | `Euler/Duhamel*` | 1 | 1 | 6.19 G | | `Euler/Weighted*` | 1 | 1 | 6.17 G | | `NavierStokes/Positive*` | 1 | 1 | 5.94 G | | `NavierStokes/Actual*` | 1 | 1 | 5.85 G | | `NavierStokes/Torus*` | 1 | 1 | 5.51 G | | `NavierStokes/Within*` | 1 | 1 | 5.42 G | | `NavierStokes/Smooth*` | 1 | 1 | 5.23 G | | `NavierStokes/Moment*` | 1 | 1 | 5.03 G | | `NavierStokes/Periodize*` | 1 | 1 | 4.94 G | | `NavierStokes/Mean*` | 1 | 1 | 4.91 G | | `NavierStokes/Valid*` | 1 | 1 | 4.89 G | | `NavierStokes/Label*` | 1 | 1 | 4.33 G | ## What this does not show The wall-clock time of a build on your machine; that every file is now in its best shape for reading (P-8 splits the large ones; nothing here grows past 600 lines). ## Statements, and the series as it was opened The first pull request of the series was cut from a plan of 582 folds. Before the others were opened, main and the folded tree were both built and every named constant of the two libraries was listed with a hash of its type and, for a definition, of its value (`ContentTail.lean`, after one `import` line a module). In that listing an auxiliary constant inside a term (a private declaration, a nested-proof lemma, an automatically named local instance) counts by what it is, not by its name, and a theorem by its statement: a fold changes the names of such constants and nothing else about them. The first plan did not pass. Of 53,802 named constants, 12 differed. In one, `Euler.ComparatorBridge.compactSolenoidalTests_representation`, the word `divergence` resolved to `Euler.divergence` in the folded file and to `EulerSmoothLimit.divergence` on main: once folded, the text saw more names. The build passed all the same. In the others an instance was found by another path (`NormedCommRing ℂ` through the C⋆-algebra instances, `DistribSMul ℝ` through the semiring), or a proof inside the statement differed. Two changes followed. `fold.py` now puts a folded module's imports where its import line stood, so that every other module is loaded in the order it was loaded before (a plan file says which way it was cut, and `check` follows it, so the first pull request still checks). And five files keep their modules: `Euler/CompactProjectedEulerLaw.lean`, `Euler/CylinderJetLpAlgebra.lean`, `Euler/MeanCutoffDifferenceBound.lean`, `NavierStokes/R3/CompactEnergy.lean`, `NavierStokes/R3/PressureRecovery.lean`. The series as opened is 575 folds into 338 files. On it the two listings are equal: 53,802 named constants, none missing, none with another type or value; the first pull request's twelve folds were checked alone and are equal too. The whole tree goes from 54.73 T instructions over 2,587 files to 50.97 T over 2,012 (−6.9%), in place of the figures at the top of this page. What the listing does not show: that two terms with different hashes mean different things (the twelve were read one by one), or anything about proofs, which are not listed.