Memoize srcTypeToVar in solver for monomorphic types#95
Draft
CharlonTank wants to merge 1 commit intolamdera:lamdera-nextfrom
Draft
Memoize srcTypeToVar in solver for monomorphic types#95CharlonTank wants to merge 1 commit intolamdera:lamdera-nextfrom
CharlonTank wants to merge 1 commit intolamdera:lamdera-nextfrom
Conversation
When a function signature like FA -> Action references large monomorphic type aliases (e.g. FrontendModel with 100+ transitive fields), srcTypeToVar walks the entire expanded type and creates fresh UnionFind variables on every single call site. On a real project, this resulted in 45M+ srcTypeToVar calls for only ~4000 unique Can.Type subtrees (11000x redundancy). This commit adds a per-run cache keyed on (rank, Can.Type) that returns the previously-built Variable when the same monomorphic type is encountered again. The cache is gated on flexVars being empty to ensure correctness in the presence of polymorphism: when type variables are in scope, sharing would incorrectly conflate distinct instantiations. On a real Lamdera project (391 modules, including dense Effect.Test code): - Cold build: 120s -> 98s (-18%, median over 3 runs) - typecheck UsersFlows: 125s -> 67s (-46%) on the bottleneck module
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.
Summary
Type checking modules that import functions with large monomorphic type aliases is dominated by redundant calls to
srcTypeToVarinType.Solve. This change adds a per-runcache keyed on(rank, Can.Type)that returns the previously-builtVariablewhen the same monomorphic type is encountered again.Problem
When
srcTypeToVarprocessesCan.TAlias home name args (Filled tipe), it walks the entire expandedtipeand allocates a fresh UnionFindVariablefor every node. For a function whose signature references a record alias with N transitively-expanded fields, each call site of that function makessrcTypeToVarre-walk those N nodes and create N fresh variables.Instrumentation on a project with 391 modules and a few large model record aliases:
srcTypeToVarcallsCan.TypesubtreesThe 11,000x redundancy comes entirely from re-walking the same
Filledexpansions across different constraints.Fix
A per-
runIORef (Map (Int, Can.Type) Variable)cache is threaded throughsolveandsrcTypeToVariable/srcTypeToVar. Cache lookups happen only whenflexVarsis empty (truly monomorphic context) — this guard ensures correctness in the presence of polymorphism:forall a. a -> a), each instantiation needs fresh variables foraVariabledepends only on(rank, Can.Type)and can safely be shared between call sitesThe cache is local to each
runinvocation, so there is no cross-module sharing or thread safety concern.Results
On a project with 391 modules (cold build of test suite,
+RTS -N12, median of 3 runs):typechecktime of bottleneck moduleThe relative improvement scales with how heavily a project relies on large monomorphic type aliases. Apps that follow patterns like a single big
Modelrecord exposed across many helpers benefit most.Safety considerations
flexVarsis non-empty, so polymorphic instantiation is unaffectedrank, so variables created at one rank are never reused at anotherVariables are correct for monomorphic types — unifying a flex variable with a concrete sharedVariableconstrains the flex side; the shared side's descriptor matches everywhere it appearsType.Solve.run; no cross-module stateTest plan
elm-test-rssuite passes (70 tests)scenario-alltypes,scenario-empty-lamdera-init,direct-fn-calls,direct-fn-calls-mutual-recursion) compile correctly