Repository navigation
Resolve Has with a closed type family - #366
Merged
Merged
Conversation
`Has` found a field through a chain of overlapping instances, one per list element. Looking up the k-th table or column built k dictionaries, each of whose types carried the whole list, and every column reference in every statement repeated that work. In a large schema this makes the type checker one of the most expensive parts of compiling a module of statements. Look the field up with a closed type family, `Lookup`, instead, and turn its result into the field, or into the existing error, with a small class, `HasLookup`. Evidence for `Has` is now constant-size. The `Has` instances still match on the list’s shape, so a `Has` constraint over a list variable in a signature isn’t simplified through an instance. `HasErr` and the error families are unchanged, and so are the messages for a missing or mismatched field. On a 233-module application with a 23-table schema (GHC 9.6.7, -O0), a clean build took 14 % less wall time (129 s to 111 s) and 5 % less peak memory; in its largest storage module the type checker went from 38 s and 11.1 GB allocated to 23 s and 7.5 GB.
Contributor
|
Thanks! Looks good to me. |
echatav
self-requested a review
November 5, 2026 16:39
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Hasresolves every table and column reference through a chain of overlapping instances: finding the k-th field builds k dictionaries whose types each carry the whole list, and every reference in every statement repeats that work. In an application with a large schema, type checking these lookups is one of the most expensive parts of compiling its modules of statements.What
Hasnow looks the field up with a closed type family,Lookup :: Symbol -> [(Symbol, k)] -> Maybe k, and a two-instance class,HasLookup, turns the result into the field or into the existing error. Evidence forHasis constant-size (aKnownSymboldictionary) instead of a chain of k dictionaries.Hasinstances still match on the list’s shape (cons or nil), as the overlapping ones did. A single catch-all instance would make GHC simplifyHasconstraints over a list variable in user signatures through the instance (-Wsimplifiable-class-constraints), which changes inference for existing code.HasErr,MismatchError,LookupFailedErrorand the pretty-printing families are unchanged and still exported; a missing or mismatched field reports the same messages.Effect
Measured on a production application (713 modules, a 23-table schema, GHC 9.6.7,
-O0,-j4, M1 Max, clean library build, one run each):On a 233-module extract of it, the type checker of the largest storage module went from 11.1 GB to 7.5 GB allocated. No change at run time.
How tested