From f03c64dbbcfbb270a3a1ac2a4f3c45dc0a8eeb9b Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Wed, 15 May 2024 19:56:41 +0200 Subject: [PATCH 1/4] Various --- src/Compiler/AbstractIL/il.fs | 10 ++--- src/Compiler/AbstractIL/ilread.fs | 70 ++++++++++--------------------- 2 files changed, 27 insertions(+), 53 deletions(-) diff --git a/src/Compiler/AbstractIL/il.fs b/src/Compiler/AbstractIL/il.fs index 37c160ef673..91c05d8a0be 100644 --- a/src/Compiler/AbstractIL/il.fs +++ b/src/Compiler/AbstractIL/il.fs @@ -39,7 +39,7 @@ let lazyMap f (x: InterruptibleLazy<_>) = else InterruptibleLazy(fun _ -> f (x.Force())) -[] +[] type PrimaryAssembly = | Mscorlib | System_Runtime @@ -54,10 +54,10 @@ type PrimaryAssembly = static member IsPossiblePrimaryAssembly(fileName: string) = let name = System.IO.Path.GetFileNameWithoutExtension(fileName) - String.Compare(name, "mscorlib", true) <> 0 - || String.Compare(name, "System.Runtime", true) <> 0 - || String.Compare(name, "netstandard", true) <> 0 - || String.Compare(name, "System.Private.CoreLib", true) <> 0 + String.Equals(name, "System.Runtime", StringComparison.OrdinalIgnoreCase) + || String.Equals(name, "mscorlib", StringComparison.OrdinalIgnoreCase) + || String.Equals(name, "netstandard", StringComparison.OrdinalIgnoreCase) + || String.Equals(name, "System.Private.CoreLib", StringComparison.OrdinalIgnoreCase) // -------------------------------------------------------------------- // Utilities: type names diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs index 78082e34ca7..e4fa64254cc 100644 --- a/src/Compiler/AbstractIL/ilread.fs +++ b/src/Compiler/AbstractIL/ilread.fs @@ -926,58 +926,32 @@ type GenericParamsIdx = GenericParamsIdx of numTypars: int * TypeOrMethodDefTag // Polymorphic caches for row and heap readers //--------------------------------------------------------------------- -let mkCacheInt32 lowMem _inbase _nm _sz = +let mkCacheGeneric lowMem _inbase _nm (sz: int) = if lowMem then - (fun f x -> f x) + id else - let mutable cache = null - let mutable count = 0 -#if STATISTICS - addReport (fun oc -> - if count <> 0 then - oc.WriteLine((_inbase + string count + " " + _nm + " cache hits"): string)) -#endif - fun f (idx: int32) -> - let cache = - match cache with - | Null -> - let v = ConcurrentDictionary(Environment.ProcessorCount, 11) - cache <- v - v - | NonNull v -> v - - match cache.TryGetValue idx with - | true, res -> - count <- count + 1 - res - | _ -> - let res = f idx - cache[idx] <- res - res + let mutable cache = Unchecked.defaultof<_> -let mkCacheGeneric lowMem _inbase _nm _sz = - if lowMem then - (fun f x -> f x) - else - let mutable cache = null - let mutable count = 0 #if STATISTICS + let mutable _count = 0 addReport (fun oc -> - if !count <> 0 then - oc.WriteLine((_inbase + string !count + " " + _nm + " cache hits"): string)) + if !_count <> 0 then + oc.WriteLine((_inbase + string !_count + " " + _nm + " cache hits"): string)) #endif fun f (idx: 'T) -> let cache = match cache with | Null -> - let v = ConcurrentDictionary<_, _>(Environment.ProcessorCount, 11 (* sz: int *) ) + let v = ConcurrentDictionary<_, _>(Environment.ProcessorCount, sz) cache <- v v | NonNull v -> v match cache.TryGetValue idx with | true, v -> - count <- count + 1 +#if STATISTICS + _count <- _count + 1 +#endif v | _ -> let res = f idx @@ -1700,7 +1674,7 @@ let readStringHeapUncached ctxtH idx = let readStringHeap (ctxt: ILMetadataReader) idx = ctxt.readStringHeap idx -let readStringHeapOption (ctxt: ILMetadataReader) idx = +let inline readStringHeapOption (ctxt: ILMetadataReader) idx = if idx = 0 then None else Some(readStringHeap ctxt idx) let readBlobHeapUncached ctxtH idx = @@ -4375,7 +4349,7 @@ let openMetadataReader // All the caches. The sizes are guesstimates for the rough sharing-density of the assembly let cacheAssemblyRef = - mkCacheInt32 false inbase "ILAssemblyRef" (getNumRows TableNames.AssemblyRef) + mkCacheGeneric false inbase "ILAssemblyRef" (getNumRows TableNames.AssemblyRef) let cacheMethodSpecAsMethodData = mkCacheGeneric reduceMemoryUsage inbase "MethodSpecAsMethodData" (getNumRows TableNames.MethodSpec / 20 + 1) @@ -4387,7 +4361,7 @@ let openMetadataReader mkCacheGeneric reduceMemoryUsage inbase "CustomAttr" (getNumRows TableNames.CustomAttribute / 50 + 1) let cacheTypeRef = - mkCacheInt32 false inbase "ILTypeRef" (getNumRows TableNames.TypeRef / 20 + 1) + mkCacheGeneric false inbase "ILTypeRef" (getNumRows TableNames.TypeRef / 20 + 1) let cacheTypeRefAsType = mkCacheGeneric reduceMemoryUsage inbase "TypeRefAsType" (getNumRows TableNames.TypeRef / 20 + 1) @@ -4405,38 +4379,38 @@ let openMetadataReader mkCacheGeneric reduceMemoryUsage inbase "TypeDefAsType" (getNumRows TableNames.TypeDef / 20 + 1) let cacheMethodDefAsMethodData = - mkCacheInt32 reduceMemoryUsage inbase "MethodDefAsMethodData" (getNumRows TableNames.Method / 20 + 1) + mkCacheGeneric reduceMemoryUsage inbase "MethodDefAsMethodData" (getNumRows TableNames.Method / 20 + 1) let cacheGenericParams = mkCacheGeneric reduceMemoryUsage inbase "GenericParams" (getNumRows TableNames.GenericParam / 20 + 1) let cacheFieldDefAsFieldSpec = - mkCacheInt32 reduceMemoryUsage inbase "FieldDefAsFieldSpec" (getNumRows TableNames.Field / 20 + 1) + mkCacheGeneric reduceMemoryUsage inbase "FieldDefAsFieldSpec" (getNumRows TableNames.Field / 20 + 1) let cacheUserStringHeap = - mkCacheInt32 reduceMemoryUsage inbase "UserStringHeap" (userStringsStreamSize / 20 + 1) + mkCacheGeneric reduceMemoryUsage inbase "UserStringHeap" (userStringsStreamSize / 20 + 1) // nb. Lots and lots of cache hits on this cache, hence never optimize cache away let cacheStringHeap = - mkCacheInt32 false inbase "string heap" (stringsStreamSize / 50 + 1) + mkCacheGeneric false inbase "string heap" (stringsStreamSize / 50 + 1) let cacheBlobHeap = - mkCacheInt32 reduceMemoryUsage inbase "blob heap" (blobsStreamSize / 50 + 1) + mkCacheGeneric reduceMemoryUsage inbase "blob heap" (blobsStreamSize / 50 + 1) // These tables are not required to enforce sharing fo the final data // structure, but are very useful as searching these tables gives rise to many reads // in standard applications. let cacheNestedRow = - mkCacheInt32 reduceMemoryUsage inbase "Nested Table Rows" (getNumRows TableNames.Nested / 20 + 1) + mkCacheGeneric reduceMemoryUsage inbase "Nested Table Rows" (getNumRows TableNames.Nested / 20 + 1) let cacheConstantRow = - mkCacheInt32 reduceMemoryUsage inbase "Constant Rows" (getNumRows TableNames.Constant / 20 + 1) + mkCacheGeneric reduceMemoryUsage inbase "Constant Rows" (getNumRows TableNames.Constant / 20 + 1) let cacheMethodSemanticsRow = - mkCacheInt32 reduceMemoryUsage inbase "MethodSemantics Rows" (getNumRows TableNames.MethodSemantics / 20 + 1) + mkCacheGeneric reduceMemoryUsage inbase "MethodSemantics Rows" (getNumRows TableNames.MethodSemantics / 20 + 1) let cacheTypeDefRow = - mkCacheInt32 reduceMemoryUsage inbase "ILTypeDef Rows" (getNumRows TableNames.TypeDef / 20 + 1) + mkCacheGeneric reduceMemoryUsage inbase "ILTypeDef Rows" (getNumRows TableNames.TypeDef / 20 + 1) let rowAddr (tab: TableName) idx = tablePhysLocations[tab.Index] + (idx - 1) * tableRowSizes[tab.Index] From 01813273e12000e8583c20631f678af0ff4b1f0e Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Wed, 15 May 2024 20:09:51 +0200 Subject: [PATCH 2/4] Testing --- src/Compiler/AbstractIL/il.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compiler/AbstractIL/il.fs b/src/Compiler/AbstractIL/il.fs index 91c05d8a0be..48cee265fdc 100644 --- a/src/Compiler/AbstractIL/il.fs +++ b/src/Compiler/AbstractIL/il.fs @@ -39,7 +39,7 @@ let lazyMap f (x: InterruptibleLazy<_>) = else InterruptibleLazy(fun _ -> f (x.Force())) -[] +[] type PrimaryAssembly = | Mscorlib | System_Runtime From cf9b67faadf04ba9b79d379e9f6c4deff78dea0d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 15 May 2024 19:28:50 +0000 Subject: [PATCH 3/4] Automated command ran: fantomas Co-authored-by: vzarytovskii <1260985+vzarytovskii@users.noreply.github.com> --- src/Compiler/AbstractIL/ilread.fs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs index e4fa64254cc..5537fce124a 100644 --- a/src/Compiler/AbstractIL/ilread.fs +++ b/src/Compiler/AbstractIL/ilread.fs @@ -934,6 +934,7 @@ let mkCacheGeneric lowMem _inbase _nm (sz: int) = #if STATISTICS let mutable _count = 0 + addReport (fun oc -> if !_count <> 0 then oc.WriteLine((_inbase + string !_count + " " + _nm + " cache hits"): string)) From 2b4a6e2212d2621856396e231fa304befcda880b Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Thu, 16 May 2024 09:58:29 +0200 Subject: [PATCH 4/4] Update ilread.fs --- src/Compiler/AbstractIL/ilread.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compiler/AbstractIL/ilread.fs b/src/Compiler/AbstractIL/ilread.fs index 5537fce124a..61c04f8e4ba 100644 --- a/src/Compiler/AbstractIL/ilread.fs +++ b/src/Compiler/AbstractIL/ilread.fs @@ -928,7 +928,7 @@ type GenericParamsIdx = GenericParamsIdx of numTypars: int * TypeOrMethodDefTag let mkCacheGeneric lowMem _inbase _nm (sz: int) = if lowMem then - id + (fun f x -> f x) else let mutable cache = Unchecked.defaultof<_>