Skip to content

Commit b4b928e

Browse files
ThoriumKevinRansom
authored andcommitted
concurrent dictionary fixes for PrettyNaming and QueryExtensions (#1563)
1 parent 56c3255 commit b4b928e

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

src/fsharp/FSharp.Core/QueryExtensions.fs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,8 @@ module internal Adapters =
5757
System.Threading.Monitor.Exit(d)
5858

5959
#else
60-
let d = new System.Collections.Concurrent.ConcurrentDictionary<Type,_>(HashIdentity.Structural)
61-
fun x ->
62-
let mutable res = Unchecked.defaultof<_>
63-
if d.TryGetValue(x ,&res) then res else let res = f x in d.[x] <- res; res
60+
let d = new System.Collections.Concurrent.ConcurrentDictionary<Type,'b>(HashIdentity.Structural)
61+
fun x -> d.GetOrAdd(x, fun r -> f r)
6462
#endif
6563

6664
let isPartiallyImmutableRecord : Type -> bool =

src/fsharp/PrettyNaming.fs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,11 @@ module internal Microsoft.FSharp.Compiler.PrettyNaming
152152

153153
/// Memoize compilation of custom operators.
154154
/// They're typically used more than once so this avoids some CPU and GC overhead.
155-
let compiledOperators = ConcurrentDictionary<_,_> (System.StringComparer.Ordinal)
155+
let compiledOperators = ConcurrentDictionary<_,string> (System.StringComparer.Ordinal)
156156

157-
fun op ->
157+
fun opp ->
158158
// Has this operator already been compiled?
159-
match compiledOperators.TryGetValue op with
160-
| true, opName -> opName
161-
| false, _ ->
159+
compiledOperators.GetOrAdd(opp, fun (op:string) ->
162160
let opLength = op.Length
163161
let sb = new System.Text.StringBuilder (opNamePrefix, opNamePrefix.Length + (opLength * maxOperatorNameLength))
164162
for i = 0 to opLength - 1 do
@@ -173,8 +171,7 @@ module internal Microsoft.FSharp.Compiler.PrettyNaming
173171
let opName = sb.ToString ()
174172

175173
// Cache the compiled name so it can be reused.
176-
compiledOperators.TryAdd (op, opName) |> ignore
177-
opName
174+
opName)
178175

179176
// +++ GLOBAL STATE
180177
/// Compiles an operator into a mangled operator name.

0 commit comments

Comments
 (0)