Skip to content

Commit 31ad54f

Browse files
committed
Replace Dictionary+syncObj+lock with Lazy<Dictionary> in IL reader caches
The ILMethodDefs, ILTypeDefs and ILExportedTypesAndForwarders caches are build-once-read-many, so a simple lazy value provides thread-safe init without introducing mutable fields, lock objects or ConcurrentDictionary overhead.
1 parent c7c8212 commit 31ad54f

1 file changed

Lines changed: 21 additions & 33 deletions

File tree

src/ProvidedTypes.fs

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2874,19 +2874,15 @@ module internal AssemblyReader =
28742874

28752875
type ILMethodDefs(larr: Lazy<ILMethodDef[]>) =
28762876

2877-
let mutable lmap : Dictionary<string, ILMethodDef[]> = null
2878-
let syncObj = obj()
2879-
let getmap() =
2880-
lock syncObj (fun () ->
2881-
if isNull lmap then
2882-
let m = Dictionary()
2883-
for y in larr.Force() do
2884-
let key = y.Name
2885-
match m.TryGetValue key with
2886-
| true, lmpak -> m.[key] <- Array.append [| y |] lmpak
2887-
| false, _ -> m.[key] <- [| y |]
2888-
lmap <- m
2889-
lmap)
2877+
let lmap = lazy (
2878+
let m = Dictionary()
2879+
for y in larr.Force() do
2880+
let key = y.Name
2881+
match m.TryGetValue key with
2882+
| true, lmpak -> m.[key] <- Array.append [| y |] lmpak
2883+
| false, _ -> m.[key] <- [| y |]
2884+
m)
2885+
let getmap() = lmap.Value
28902886

28912887
member __.Entries = larr.Force()
28922888
member __.FindByName nm =
@@ -3100,16 +3096,12 @@ module internal AssemblyReader =
31003096

31013097
and ILTypeDefs(larr: Lazy<(string uoption * string * Lazy<ILTypeDef>)[]>) =
31023098

3103-
let mutable lmap : Dictionary<string uoption * string, Lazy<ILTypeDef>> = null
3104-
let syncObj = obj()
3105-
let getmap() =
3106-
lock syncObj (fun () ->
3107-
if isNull lmap then
3108-
let m = Dictionary()
3109-
for (nsp, nm, ltd) in larr.Force() do
3110-
m.[(nsp, nm)] <- ltd
3111-
lmap <- m
3112-
lmap)
3099+
let lmap = lazy (
3100+
let m = Dictionary()
3101+
for (nsp, nm, ltd) in larr.Force() do
3102+
m.[(nsp, nm)] <- ltd
3103+
m)
3104+
let getmap() = lmap.Value
31133105

31143106
member __.Entries =
31153107
[| for (_, _, td) in larr.Force() -> td.Force() |]
@@ -3147,16 +3139,12 @@ module internal AssemblyReader =
31473139
override x.ToString() = "fwd " + x.Name
31483140

31493141
and ILExportedTypesAndForwarders(larr:Lazy<ILExportedTypeOrForwarder[]>) =
3150-
let mutable lmap : Dictionary<string uoption * string, ILExportedTypeOrForwarder> = null
3151-
let syncObj = obj()
3152-
let getmap() =
3153-
lock syncObj (fun () ->
3154-
if isNull lmap then
3155-
let m = Dictionary()
3156-
for ltd in larr.Force() do
3157-
m.[(ltd.Namespace, ltd.Name)] <- ltd
3158-
lmap <- m
3159-
lmap)
3142+
let lmap = lazy (
3143+
let m = Dictionary()
3144+
for ltd in larr.Force() do
3145+
m.[(ltd.Namespace, ltd.Name)] <- ltd
3146+
m)
3147+
let getmap() = lmap.Value
31603148
member __.Entries = larr.Force()
31613149
member __.TryFindByName (nsp, nm) = match getmap().TryGetValue ((nsp, nm)) with true, v -> Some v | false, _ -> None
31623150

0 commit comments

Comments
 (0)