@@ -7,6 +7,7 @@ module internal FSharp.Compiler.TypeRelations
77open FSharp.Compiler .Features
88open Internal.Utilities .Collections
99open Internal.Utilities .Library
10+ open Internal.Utilities .TypeHashing .StructuralUtilities
1011
1112open FSharp.Compiler .DiagnosticsLogger
1213open FSharp.Compiler .TcGlobals
@@ -19,6 +20,26 @@ open Import
1920
2021#nowarn " 3391"
2122
23+ [<Struct; NoComparison>]
24+ type CanCoerce =
25+ | CanCoerce
26+ | NoCoerce
27+
28+ [<Struct; NoComparison>]
29+ type TTypeCacheKey =
30+ | TTypeCacheKey of TypeStructure * TypeStructure * CanCoerce
31+ static member FromStrippedTypes ( ty1 , ty2 , canCoerce ) =
32+ TTypeCacheKey( getTypeStructure ty1, getTypeStructure ty2, canCoerce)
33+
34+ let getTypeSubsumptionCache =
35+ let factory ( g : TcGlobals ) =
36+ let options =
37+ match g.compilationMode with
38+ | CompilationMode.OneOff -> Caches.CacheOptions.getDefault() |> Caches.CacheOptions.withNoEviction
39+ | _ -> { Caches.CacheOptions.getDefault() with TotalCapacity = 65536 ; HeadroomPercentage = 75 }
40+ new Caches.Cache< TTypeCacheKey, bool>( options, " typeSubsumptionCache" )
41+ Extras.WeakMap.getOrCreate factory
42+
2243/// Implements a :> b without coercion based on finalized (no type variable) types
2344// Note: This relation is approximate and not part of the language specification.
2445//
@@ -136,14 +157,8 @@ let rec TypeFeasiblySubsumesType ndeep (g: TcGlobals) (amap: ImportMap) m (ty1:
136157 List.exists ( TypeFeasiblySubsumesType ( ndeep + 1 ) g amap m ty1 NoCoerce) interfaces
137158
138159 if g.langVersion.SupportsFeature LanguageFeature.UseTypeSubsumptionCache then
139- let key = TTypeCacheKey.FromStrippedTypes ( ty1, ty2, canCoerce)
140-
141- match amap.TypeSubsumptionCache.TryGetValue( key) with
142- | true , subsumes -> subsumes
143- | false , _ ->
144- let subsumes = checkSubsumes ty1 ty2
145- amap.TypeSubsumptionCache.TryAdd( key, subsumes) |> ignore
146- subsumes
160+ let key = TTypeCacheKey.FromStrippedTypes( ty1, ty2, canCoerce)
161+ ( getTypeSubsumptionCache g) .GetOrAdd( key, fun _ -> checkSubsumes ty1 ty2)
147162 else
148163 checkSubsumes ty1 ty2
149164
0 commit comments