Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocations by favoring TryGetValue over TryFind #5715

Merged
merged 8 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.2.3" />
<PackageReference Include="FSharp.Core" Version="4.5.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />
<PackageReference Include="NUnit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
Expand Down
4 changes: 2 additions & 2 deletions fcs/FSharp.Compiler.Service/FSharp.Compiler.Service.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<FSharpSourcesRoot>$(MSBuildProjectDirectory)\..\..\src</FSharpSourcesRoot>
</PropertyGroup>
Expand Down Expand Up @@ -635,7 +635,7 @@
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="FSharp.Core" Version="4.1.18" />
<PackageReference Include="FSharp.Core" Version="4.5.2" />
<PackageReference Include="System.Collections.Immutable" Version="1.5.0" />
<PackageReference Include="System.Reflection.Metadata" Version="1.6.0" />
</ItemGroup>
Expand Down
12 changes: 4 additions & 8 deletions src/absil/illib.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,7 @@ module NameMap =
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module NameMultiMap =
let existsInRange f (m: NameMultiMap<'T>) = NameMap.exists (fun _ l -> List.exists f l) m
let find v (m: NameMultiMap<'T>) = match Map.tryFind v m with None -> [] | Some r -> r
let find v (m: NameMultiMap<'T>) = match m.TryGetValue v with true, r -> r | _ -> []
let add v x (m: NameMultiMap<'T>) = NameMap.add v (x :: find v m) m
let range (m: NameMultiMap<'T>) = Map.foldBack (fun _ x sofar -> x @ sofar) m []
let rangeReversingEachBucket (m: NameMultiMap<'T>) = Map.foldBack (fun _ x sofar -> List.rev x @ sofar) m []
Expand All @@ -1137,7 +1137,7 @@ module NameMultiMap =
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module MultiMap =
let existsInRange f (m: MultiMap<_,_>) = Map.exists (fun _ l -> List.exists f l) m
let find v (m: MultiMap<_,_>) = match Map.tryFind v m with None -> [] | Some r -> r
let find v (m: MultiMap<_,_>) = match m.TryGetValue v with true, r -> r | _ -> []
let add v x (m: MultiMap<_,_>) = Map.add v (x :: find v m) m
let range (m: MultiMap<_,_>) = Map.foldBack (fun _ x sofar -> x @ sofar) m []
let empty : MultiMap<_,_> = Map.empty
Expand All @@ -1148,11 +1148,6 @@ type LayeredMap<'Key,'Value when 'Key : comparison> = Map<'Key,'Value>
type Map<'Key,'Value when 'Key : comparison> with
static member Empty : Map<'Key,'Value> = Map.empty

member m.TryGetValue (key,res:byref<'Value>) =
match m.TryFind key with
| None -> false
| Some r -> res <- r; true

member x.Values = [ for (KeyValue(_,v)) in x -> v ]
member x.AddAndMarkAsCollapsible (kvs: _[]) = (x,kvs) ||> Array.fold (fun x (KeyValue(k,v)) -> x.Add(k,v))
member x.LinearTryModifyThenLaterFlatten (key, f: 'Value option -> 'Value) = x.Add (key, f (x.TryFind key))
Expand All @@ -1162,12 +1157,13 @@ type Map<'Key,'Value when 'Key : comparison> with
[<Sealed>]
type LayeredMultiMap<'Key,'Value when 'Key : equality and 'Key : comparison>(contents : LayeredMap<'Key,'Value list>) =
member x.Add (k,v) = LayeredMultiMap(contents.Add(k,v :: x.[k]))
member x.Item with get k = match contents.TryFind k with None -> [] | Some l -> l
member x.Item with get k = match contents.TryGetValue k with true, l -> l | _ -> []
member x.AddAndMarkAsCollapsible (kvs: _[]) =
let x = (x,kvs) ||> Array.fold (fun x (KeyValue(k,v)) -> x.Add(k,v))
x.MarkAsCollapsible()
member x.MarkAsCollapsible() = LayeredMultiMap(contents.MarkAsCollapsible())
member x.TryFind k = contents.TryFind k
member x.TryGetValue k = contents.TryGetValue k
member x.Values = contents.Values |> List.concat
static member Empty : LayeredMultiMap<'Key,'Value> = LayeredMultiMap LayeredMap.Empty

Expand Down
Loading