I found an old topic in 2018 about compilation times. According to it, changing large records to opaque types can dramatically improve the situation, and when we tried it in the SPA application we’re building at our company, it actually decreases compilation time and memory usage by 20~30% (from about 80sec to 60 sec).
Some advice on Elm compile performance
To investigate the reason, I explored the compiler’s source code and discovered that there is an issue with how type aliases are handled internally.
For example,
type alias Three a = {p: a, q: a, r: a}
var: Three Int
When dumping the type of the above var from the compiler, it looks like this.
(TAlias
(Canonical {_package = Name {_author = author, _project = project}, _module = Main})
Three
[(a,TType (Canonical {_package = Name {_author = elm, _project = core}, _module = Basics}) Int [])]
(Filled (TRecord (fromList [(p,FieldType 0 (TType (Canonical {_package = Name {_author = elm, _project = core}, _module = Basics}) Int [])),(q,FieldType 0 (TType (Canonical {_package = Name {_author = elm, _project = core}, _module = Basics}) Int [])),(r,FieldType 0 (TType (Canonical {_package = Name {_author = elm, _project = core}, _module = Basics}) Int []))]) Nothing))
)
Simplified by removing the syntactic noise, it looks like this.
TAlias (author.project.Main.Three)
[(a, elm.core.Basics.Int[])]
(Record
[(p, elm.core.Basics.Int[])
,(q, elm.core.Basics.Int[])
,(r, elm.core.Basics.Int[])
])
Each time you pass a type argument to an alias, its contents are expanded (usage count + 1) times. In this case, it’s just an Int, so it’s fine, but if you pass a large record, it becomes quite problematic.
This type information is expanded each time the type is used and is binary-encoded in .elmi files within elm-stuff. When I passed the -p option to elm-compiler to take a profile, 90% of our project’s compile time was spent parsing elmi files.
My next goal is to work on improving the encoding of .elmi files, but for now, using opaque types for large records is a practical and effective solution.