Skip to content

Commit 7382e6f

Browse files
authored
IL: don't keep ILPreTypeDefImpl storage after it was evaluated (#16151)
* IL: don't keep ILPreTypeDefImpl storage after it was evaluated * Fantomas
1 parent bd66d54 commit 7382e6f

File tree

1 file changed

+19
-6
lines changed
  • src/Compiler/AbstractIL

1 file changed

+19
-6
lines changed

src/Compiler/AbstractIL/il.fs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,6 +2876,7 @@ and [<NoEquality; NoComparison>] ILPreTypeDef =
28762876
/// This is a memory-critical class. Very many of these objects get allocated and held to represent the contents of .NET assemblies.
28772877
and [<Sealed>] ILPreTypeDefImpl(nameSpace: string list, name: string, metadataIndex: int32, storage: ILTypeDefStored) =
28782878
let mutable store: ILTypeDef = Unchecked.defaultof<_>
2879+
let mutable storage = storage
28792880

28802881
interface ILPreTypeDef with
28812882
member _.Namespace = nameSpace
@@ -2884,12 +2885,24 @@ and [<Sealed>] ILPreTypeDefImpl(nameSpace: string list, name: string, metadataIn
28842885
member x.GetTypeDef() =
28852886
match box store with
28862887
| null ->
2887-
match storage with
2888-
| ILTypeDefStored.Given td ->
2889-
store <- td
2890-
td
2891-
| ILTypeDefStored.Computed f -> LazyInitializer.EnsureInitialized<ILTypeDef>(&store, Func<_>(fun () -> f ()))
2892-
| ILTypeDefStored.Reader f -> LazyInitializer.EnsureInitialized<ILTypeDef>(&store, Func<_>(fun () -> f metadataIndex))
2888+
let syncObj = storage
2889+
Monitor.Enter(syncObj)
2890+
2891+
try
2892+
match box store with
2893+
| null ->
2894+
let value =
2895+
match storage with
2896+
| ILTypeDefStored.Given td -> td
2897+
| ILTypeDefStored.Computed f -> f ()
2898+
| ILTypeDefStored.Reader f -> f metadataIndex
2899+
2900+
store <- value
2901+
storage <- Unchecked.defaultof<_>
2902+
value
2903+
| _ -> store
2904+
finally
2905+
Monitor.Exit(syncObj)
28932906
| _ -> store
28942907

28952908
and ILTypeDefStored =

0 commit comments

Comments
 (0)