From 4d67261e5e6d6c912e3d433641c5f2d62c61a9ee Mon Sep 17 00:00:00 2001 From: Steffen Forkmann Date: Wed, 18 Jul 2018 16:54:22 +0200 Subject: [PATCH 1/3] Shortcut empty arrays for groupBy --- src/fsharp/FSharp.Core/array.fs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/fsharp/FSharp.Core/array.fs b/src/fsharp/FSharp.Core/array.fs index f9cced85d3b..b92e3d95034 100644 --- a/src/fsharp/FSharp.Core/array.fs +++ b/src/fsharp/FSharp.Core/array.fs @@ -405,10 +405,12 @@ namespace Microsoft.FSharp.Collections loop 0 let inline groupByImpl (comparer:IEqualityComparer<'SafeKey>) (keyf:'T->'SafeKey) (getKey:'SafeKey->'Key) (array: 'T[]) = + let length = array.Length + if length = 0 then Microsoft.FSharp.Primitives.Basics.Array.zeroCreateUnchecked 0 else let dict = Dictionary<_,ResizeArray<_>> comparer // Build the groupings - for i = 0 to (array.Length - 1) do + for i = 0 to length - 1 do let v = array.[i] let safeKey = keyf v let mutable prev = Unchecked.defaultof<_> From 28e2de193ed471663e63f77079d268610235231d Mon Sep 17 00:00:00 2001 From: Steffen Forkmann Date: Wed, 18 Jul 2018 16:59:53 +0200 Subject: [PATCH 2/3] Shortcut empty seqs for groupBy --- src/fsharp/FSharp.Core/seq.fs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/fsharp/FSharp.Core/seq.fs b/src/fsharp/FSharp.Core/seq.fs index f057e8f3fd0..f8ef697a8e5 100644 --- a/src/fsharp/FSharp.Core/seq.fs +++ b/src/fsharp/FSharp.Core/seq.fs @@ -1032,6 +1032,8 @@ namespace Microsoft.FSharp.Collections let inline groupByImpl (comparer:IEqualityComparer<'SafeKey>) (keyf:'T->'SafeKey) (getKey:'SafeKey->'Key) (seq:seq<'T>) = checkNonNull "seq" seq + if isEmpty seq then empty else + let dict = Dictionary<_,ResizeArray<_>> comparer // Previously this was 1, but I think this is rather stingy, considering that we are already paying From 352c139175fd447a92a3d281b12f238c8678ed3d Mon Sep 17 00:00:00 2001 From: Steffen Forkmann Date: Wed, 18 Jul 2018 16:40:27 +0200 Subject: [PATCH 3/3] Don't create dict for empty lists --- src/fsharp/FSharp.Core/local.fs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/fsharp/FSharp.Core/local.fs b/src/fsharp/FSharp.Core/local.fs index 779b543cd21..42297795184 100644 --- a/src/fsharp/FSharp.Core/local.fs +++ b/src/fsharp/FSharp.Core/local.fs @@ -196,6 +196,10 @@ module internal List = cons let groupBy (comparer:IEqualityComparer<'SafeKey>) (keyf:'T->'SafeKey) (getKey:'SafeKey->'Key) (list: 'T list) = + match list with + | [] -> [] + | _ -> + let dict = Dictionary<_, _ list []> comparer // Build the groupings