From 4f0162274e4e1ba0dba7ac3d1dcf9d53077802bf Mon Sep 17 00:00:00 2001 From: Tuomas Hietanen Date: Sat, 24 Sep 2016 14:22:15 +0100 Subject: [PATCH] concurrent dictionary fixes for PrettyNaming and QueryExtensions --- src/fsharp/FSharp.Core/QueryExtensions.fs | 6 ++---- src/fsharp/PrettyNaming.fs | 11 ++++------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/fsharp/FSharp.Core/QueryExtensions.fs b/src/fsharp/FSharp.Core/QueryExtensions.fs index 78a98f9c44a..4e56401e500 100644 --- a/src/fsharp/FSharp.Core/QueryExtensions.fs +++ b/src/fsharp/FSharp.Core/QueryExtensions.fs @@ -57,10 +57,8 @@ module internal Adapters = System.Threading.Monitor.Exit(d) #else - let d = new System.Collections.Concurrent.ConcurrentDictionary(HashIdentity.Structural) - fun x -> - let mutable res = Unchecked.defaultof<_> - if d.TryGetValue(x ,&res) then res else let res = f x in d.[x] <- res; res + let d = new System.Collections.Concurrent.ConcurrentDictionary(HashIdentity.Structural) + fun x -> d.GetOrAdd(x, fun r -> f r) #endif let isPartiallyImmutableRecord : Type -> bool = diff --git a/src/fsharp/PrettyNaming.fs b/src/fsharp/PrettyNaming.fs index bb967ab8871..3be1286ed53 100755 --- a/src/fsharp/PrettyNaming.fs +++ b/src/fsharp/PrettyNaming.fs @@ -152,13 +152,11 @@ module internal Microsoft.FSharp.Compiler.PrettyNaming /// Memoize compilation of custom operators. /// They're typically used more than once so this avoids some CPU and GC overhead. - let compiledOperators = ConcurrentDictionary<_,_> (System.StringComparer.Ordinal) + let compiledOperators = ConcurrentDictionary<_,string> (System.StringComparer.Ordinal) - fun op -> + fun opp -> // Has this operator already been compiled? - match compiledOperators.TryGetValue op with - | true, opName -> opName - | false, _ -> + compiledOperators.GetOrAdd(opp, fun (op:string) -> let opLength = op.Length let sb = new System.Text.StringBuilder (opNamePrefix, opNamePrefix.Length + (opLength * maxOperatorNameLength)) for i = 0 to opLength - 1 do @@ -173,8 +171,7 @@ module internal Microsoft.FSharp.Compiler.PrettyNaming let opName = sb.ToString () // Cache the compiled name so it can be reused. - compiledOperators.TryAdd (op, opName) |> ignore - opName + opName) // +++ GLOBAL STATE /// Compiles an operator into a mangled operator name.