From f98bc5e45e549f4cb66d3370568a3b7ad8c6292d Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Fri, 20 Oct 2023 14:48:42 +0200 Subject: [PATCH 1/2] IL: don't keep ILPreTypeDefImpl storage after it was evaluated --- src/Compiler/AbstractIL/il.fs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Compiler/AbstractIL/il.fs b/src/Compiler/AbstractIL/il.fs index 2c96d10f52..f287599021 100644 --- a/src/Compiler/AbstractIL/il.fs +++ b/src/Compiler/AbstractIL/il.fs @@ -2876,6 +2876,7 @@ and [] ILPreTypeDef = /// This is a memory-critical class. Very many of these objects get allocated and held to represent the contents of .NET assemblies. and [] ILPreTypeDefImpl(nameSpace: string list, name: string, metadataIndex: int32, storage: ILTypeDefStored) = let mutable store: ILTypeDef = Unchecked.defaultof<_> + let mutable storage = storage interface ILPreTypeDef with member _.Namespace = nameSpace @@ -2884,12 +2885,22 @@ and [] ILPreTypeDefImpl(nameSpace: string list, name: string, metadataIn member x.GetTypeDef() = match box store with | null -> - match storage with - | ILTypeDefStored.Given td -> - store <- td - td - | ILTypeDefStored.Computed f -> LazyInitializer.EnsureInitialized(&store, Func<_>(fun () -> f ())) - | ILTypeDefStored.Reader f -> LazyInitializer.EnsureInitialized(&store, Func<_>(fun () -> f metadataIndex)) + let syncObj = storage + Monitor.Enter(syncObj) + try + match box store with + | null -> + let value = + match storage with + | ILTypeDefStored.Given td -> td + | ILTypeDefStored.Computed f -> f () + | ILTypeDefStored.Reader f -> f metadataIndex + store <- value + storage <- Unchecked.defaultof<_> + value + | _ -> store + finally + Monitor.Exit(syncObj) | _ -> store and ILTypeDefStored = From b7726ec69e4f8f95cf84b4b59de939f7a8720158 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Fri, 20 Oct 2023 15:58:58 +0200 Subject: [PATCH 2/2] Fantomas --- src/Compiler/AbstractIL/il.fs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Compiler/AbstractIL/il.fs b/src/Compiler/AbstractIL/il.fs index f287599021..b918bbe908 100644 --- a/src/Compiler/AbstractIL/il.fs +++ b/src/Compiler/AbstractIL/il.fs @@ -2887,6 +2887,7 @@ and [] ILPreTypeDefImpl(nameSpace: string list, name: string, metadataIn | null -> let syncObj = storage Monitor.Enter(syncObj) + try match box store with | null -> @@ -2895,6 +2896,7 @@ and [] ILPreTypeDefImpl(nameSpace: string list, name: string, metadataIn | ILTypeDefStored.Given td -> td | ILTypeDefStored.Computed f -> f () | ILTypeDefStored.Reader f -> f metadataIndex + store <- value storage <- Unchecked.defaultof<_> value