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

Don't create dict for empty lists #5348

Merged
merged 3 commits into from
Jul 22, 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
4 changes: 3 additions & 1 deletion src/fsharp/FSharp.Core/array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's time to add empty arrays singleton? #5066 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. I could use them in many places

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<_>
Expand Down
4 changes: 4 additions & 0 deletions src/fsharp/FSharp.Core/local.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/fsharp/FSharp.Core/seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down