Skip to content

Commit

Permalink
Fix System.Collections.Immutable!ImmutableArray<T>.Builder.Sort() to …
Browse files Browse the repository at this point in the history
…not generate Comparer wrapper each time

System.Collections.Immutable!ImmutableArray<T>.Builder.Sort() (3 overloads) creates a new Comparer wrapper each time. This can get expensive if you call Sort a lot.

This fix optmizes method Sort() alone among the 3 overloads. Sort() uses the default comparer which through this fix is getting cached into a static field in type Comparer such that the Comparer wrapper is not created each time Sort() is called.
Existing testcase Sort(), covers this case.
  • Loading branch information
richamsft committed Nov 14, 2014
1 parent 83abca2 commit f4169b3
Showing 1 changed file with 3 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ public void ReverseContents()
/// </summary>
public void Sort()
{
Array.Sort(this.elements, 0, this.Count, new Comparer(Comparer<T>.Default));
Array.Sort(this.elements, 0, this.Count, Comparer.Default);
}

/// <summary>
Expand Down Expand Up @@ -702,6 +702,8 @@ private sealed class Comparer : IComparer<RefAsValueType<T>>
{
private readonly IComparer<T> comparer;

public static readonly Comparer Default = new Comparer(Comparer<T>.Default);

internal Comparer(IComparer<T> comparer = null)
{
Requires.NotNull(comparer, "comparer");
Expand Down

0 comments on commit f4169b3

Please sign in to comment.