Skip to content

Commit c193b34

Browse files
authored
collect metrics from MemoizationTables (#18936)
1 parent 2d87dc2 commit c193b34

File tree

6 files changed

+26
-21
lines changed

6 files changed

+26
-21
lines changed

src/Compiler/Checking/InfoReader.fs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,9 @@ type InfoReader(g: TcGlobals, amap: Import.ImportMap) as this =
727727
/// Make a cache for function 'f' keyed by type (plus some additional 'flags') that only
728728
/// caches computations for monomorphic types.
729729
730-
let MakeInfoCache f (flagsEq : IEqualityComparer<_>) =
730+
let MakeInfoCache name f (flagsEq : IEqualityComparer<_>) =
731731
MemoizationTable<_, _>
732-
(compute=f,
732+
(name, compute=f,
733733
// Only cache closed, monomorphic types (closed = all members for the type
734734
// have been processed). Generic type instantiations could be processed if we had
735735
// a decent hash function for these.
@@ -803,18 +803,18 @@ type InfoReader(g: TcGlobals, amap: Import.ImportMap) as this =
803803
member _.GetHashCode((ad, nm)) = AccessorDomain.CustomGetHashCode ad + hash nm
804804
member _.Equals((ad1, nm1), (ad2, nm2)) = AccessorDomain.CustomEquals(g, ad1, ad2) && (nm1 = nm2) }
805805

806-
let methodInfoCache = MakeInfoCache GetIntrinsicMethodSetsUncached hashFlags0
807-
let propertyInfoCache = MakeInfoCache GetIntrinsicPropertySetsUncached hashFlags0
808-
let recdOrClassFieldInfoCache = MakeInfoCache GetIntrinsicRecdOrClassFieldInfosUncached hashFlags1
809-
let ilFieldInfoCache = MakeInfoCache GetIntrinsicILFieldInfosUncached hashFlags1
810-
let eventInfoCache = MakeInfoCache GetIntrinsicEventInfosUncached hashFlags1
811-
let namedItemsCache = MakeInfoCache GetIntrinsicNamedItemsUncached hashFlags2
812-
let mostSpecificOverrideMethodInfoCache = MakeInfoCache GetIntrinsicMostSpecificOverrideMethodSetsUncached hashFlags0
813-
814-
let entireTypeHierarchyCache = MakeInfoCache GetEntireTypeHierarchyUncached HashIdentity.Structural
815-
let primaryTypeHierarchyCache = MakeInfoCache GetPrimaryTypeHierarchyUncached HashIdentity.Structural
816-
let implicitConversionCache = MakeInfoCache FindImplicitConversionsUncached hashFlags3
817-
let isInterfaceWithStaticAbstractMethodCache = MakeInfoCache IsInterfaceTypeWithMatchingStaticAbstractMemberUncached hashFlags4
806+
let methodInfoCache = MakeInfoCache "methodInfoCache" GetIntrinsicMethodSetsUncached hashFlags0
807+
let propertyInfoCache = MakeInfoCache "propertyInfoCache" GetIntrinsicPropertySetsUncached hashFlags0
808+
let recdOrClassFieldInfoCache = MakeInfoCache "recdOrClassFieldInfoCache" GetIntrinsicRecdOrClassFieldInfosUncached hashFlags1
809+
let ilFieldInfoCache = MakeInfoCache "ilFieldInfoCache" GetIntrinsicILFieldInfosUncached hashFlags1
810+
let eventInfoCache = MakeInfoCache "eventInfoCache" GetIntrinsicEventInfosUncached hashFlags1
811+
let namedItemsCache = MakeInfoCache "namedItemsCache" GetIntrinsicNamedItemsUncached hashFlags2
812+
let mostSpecificOverrideMethodInfoCache = MakeInfoCache "mostSpecificOverrideMethodInfoCache" GetIntrinsicMostSpecificOverrideMethodSetsUncached hashFlags0
813+
814+
let entireTypeHierarchyCache = MakeInfoCache "entireTypeHierarchyCache" GetEntireTypeHierarchyUncached HashIdentity.Structural
815+
let primaryTypeHierarchyCache = MakeInfoCache "primaryTypeHierarchyCache" GetPrimaryTypeHierarchyUncached HashIdentity.Structural
816+
let implicitConversionCache = MakeInfoCache "implicitConversionCache" FindImplicitConversionsUncached hashFlags3
817+
let isInterfaceWithStaticAbstractMethodCache = MakeInfoCache "isInterfaceWithStaticAbstractMethodCache" IsInterfaceTypeWithMatchingStaticAbstractMemberUncached hashFlags4
818818

819819
// Runtime feature support
820820

src/Compiler/CodeGen/IlxGen.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,7 @@ and AssemblyBuilder(cenv: cenv, anonTypeTable: AnonTypeGenerationTable) as mgbuf
23342334
// A memoization table for generating value types for big constant arrays
23352335
let rawDataValueTypeGenerator =
23362336
MemoizationTable<CompileLocation * int, ILTypeSpec>(
2337+
"rawDataValueTypeGenerator",
23372338
(fun (cloc, size) ->
23382339
let name =
23392340
CompilerGeneratedName("T" + string (newUnique ()) + "_" + string size + "Bytes") // Type names ending ...$T<unique>_37Bytes

src/Compiler/FSharp.Compiler.Service.fsproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
</EmbeddedResource>
115115
<Compile Include="Utilities\Activity.fsi" />
116116
<Compile Include="Utilities\Activity.fs" />
117+
<Compile Include="Utilities\Caches.fsi" />
118+
<Compile Include="Utilities\Caches.fs" />
117119
<Compile Include="Utilities\illib.fsi" />
118120
<Compile Include="Utilities\illib.fs" />
119121
<Compile Include="Utilities\sformat.fsi" />
@@ -147,8 +149,6 @@
147149
<Compile Include="Utilities\lib.fsi" />
148150
<Compile Include="Utilities\lib.fs" />
149151
<Compile Include="Utilities\DependencyGraph.fs" />
150-
<Compile Include="Utilities\Caches.fsi" />
151-
<Compile Include="Utilities\Caches.fs" />
152152
<Compile Include="Utilities\LruCache.fsi" />
153153
<Compile Include="Utilities\LruCache.fs" />
154154
<Compile Include="Utilities\ImmutableArray.fsi" />

src/Compiler/TypedTree/TcGlobals.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ type TcGlobals(
687687

688688
// Build the memoization table for files
689689
let v_memoize_file =
690-
MemoizationTable<int, ILSourceDocument>(compute, keyComparer = HashIdentity.Structural)
690+
MemoizationTable<int, ILSourceDocument>("v_memoize_file", compute, keyComparer = HashIdentity.Structural)
691691

692692
let v_and_info = makeIntrinsicValRef(fslib_MFIntrinsicOperators_nleref, CompileOpName "&" , None , None , [], mk_rel_sig v_bool_ty)
693693
let v_addrof_info = makeIntrinsicValRef(fslib_MFIntrinsicOperators_nleref, CompileOpName "~&" , None , None , [vara], ([[varaTy]], mkByrefTy varaTy))

src/Compiler/Utilities/illib.fs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ open System.Threading
1111
open System.Threading.Tasks
1212
open System.Runtime.CompilerServices
1313

14+
open FSharp.Compiler.Caches
15+
1416
[<Class>]
1517
type InterruptibleLazy<'T> private (value, valueFactory: unit -> 'T) =
1618
let syncObj = obj ()
@@ -950,10 +952,11 @@ type UniqueStampGenerator<'T when 'T: equality and 'T: not null>() =
950952
member _.Table = encodeTable.Keys
951953

952954
/// memoize tables (all entries cached, never collected)
953-
type MemoizationTable<'T, 'U when 'T: not null>(compute: 'T -> 'U, keyComparer: IEqualityComparer<'T>, ?canMemoize) =
955+
type MemoizationTable<'T, 'U when 'T: not null>(name, compute: 'T -> 'U, keyComparer: IEqualityComparer<'T>, ?canMemoize) =
954956

955-
let table = new ConcurrentDictionary<'T, Lazy<'U>>(keyComparer)
956-
let computeFunc = Func<_, _>(fun key -> lazy (compute key))
957+
let options = CacheOptions.getDefault keyComparer |> CacheOptions.withNoEviction
958+
let table = new Cache<'T, Lazy<'U>>(options, name)
959+
let computeFunc key = lazy compute key
957960

958961
member t.Apply x =
959962
if

src/Compiler/Utilities/illib.fsi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ type internal UniqueStampGenerator<'T when 'T: equality and 'T: not null> =
386386
type internal MemoizationTable<'T, 'U when 'T: not null> =
387387

388388
new:
389-
compute: ('T -> 'U) * keyComparer: IEqualityComparer<'T> * ?canMemoize: ('T -> bool) -> MemoizationTable<'T, 'U>
389+
name: string * compute: ('T -> 'U) * keyComparer: IEqualityComparer<'T> * ?canMemoize: ('T -> bool) ->
390+
MemoizationTable<'T, 'U>
390391

391392
member Apply: x: 'T -> 'U
392393

0 commit comments

Comments
 (0)