Share the variable-free parts of let-bound value types
Why
Every use of a let-bound value copied its whole type, even the parts without type variables. For a value with a 400-field object type, each use added about 22 KB to the .cmt, and saving the .cmi copied the type again for every signature item (#9265).
The parts of a let-bound value's type that contain no type variable now stay non-generic at the binding's level, like the parts of a weak type. Instantiation keeps non-generic nodes, so all uses share them. Saving a signature copies each such part once for the whole .cmi, reusing a copy only under the path substitution it was made with. Generic nodes never count as variable-free, so a shared part never contains one.
Typing: Ctype.generalize_value (let-bound patterns)
let o = {"f0": 0, /* … 400 fields */}let g0 = olet g1 = o
text
Before
After
generalize o's type: every node generic, each use copies all 400 fieldsgeneralize o's type: variable-free nodes stay at the let's level, each use shares them
Review
- Can a shared part change after it is shared? Unification may link it to an equal type or lower its level, as for weak types. The mutability cells of its object fields can't be promoted: its rows are closed, and cells are merged only together with their rows' tails.