Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2ebecd4

Browse files
committed
Merge pull request #2646 from James-Ko/patch-5
Don't wrap with ImmutableArray
2 parents c47d44a + a7250b8 commit 2ebecd4

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray`1.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public partial struct ImmutableArray<T> : IReadOnlyList<T>, IList<T>, IEquatable
4040
public static readonly ImmutableArray<T> Empty = new ImmutableArray<T>(new T[0]);
4141

4242
/// <summary>
43-
/// The backing field for this instance. References to this value should never be shared with outside code.
43+
/// The backing field for this instance. References to this value should never be shared with outside code.
4444
/// </summary>
4545
/// <remarks>
4646
/// This would be private, but we make it internal so that our own extension methods can access it.
@@ -583,11 +583,11 @@ public ImmutableArray<T> InsertRange(int index, ImmutableArray<T> items)
583583

584584
if (self.IsEmpty)
585585
{
586-
return new ImmutableArray<T>(items.array);
586+
return items;
587587
}
588588
else if (items.IsEmpty)
589589
{
590-
return new ImmutableArray<T>(self.array);
590+
return self;
591591
}
592592

593593
return self.InsertRange(index, items.array);
@@ -636,7 +636,7 @@ public ImmutableArray<T> AddRange(ImmutableArray<T> items)
636636
if (self.IsEmpty)
637637
{
638638
// Be sure what we return is marked as initialized.
639-
return new ImmutableArray<T>(items.array);
639+
return items;
640640
}
641641
else if (items.IsEmpty)
642642
{
@@ -728,7 +728,7 @@ public ImmutableArray<T> Remove(T item, IEqualityComparer<T> equalityComparer)
728728
self.ThrowNullRefIfNotInitialized();
729729
int index = self.IndexOf(item, equalityComparer);
730730
return index < 0
731-
? new ImmutableArray<T>(self.array)
731+
? self
732732
: self.RemoveAt(index);
733733
}
734734

@@ -876,7 +876,7 @@ public ImmutableArray<T> RemoveAll(Predicate<T> match)
876876

877877
if (self.IsEmpty)
878878
{
879-
return new ImmutableArray<T>(self.array);
879+
return self;
880880
}
881881

882882
List<int> removeIndexes = null;
@@ -942,14 +942,14 @@ public ImmutableArray<T> Sort(int index, int count, IComparer<T> comparer)
942942
Requires.Range(index >= 0, "index");
943943
Requires.Range(count >= 0 && index + count <= self.Length, "count");
944944

945-
if (comparer == null)
946-
{
947-
comparer = Comparer<T>.Default;
948-
}
949-
950945
// 0 and 1 element arrays don't need to be sorted.
951946
if (count > 1)
952947
{
948+
if (comparer == null)
949+
{
950+
comparer = Comparer<T>.Default;
951+
}
952+
953953
// Avoid copying the entire array when the array is already sorted.
954954
bool outOfOrder = false;
955955
for (int i = index + 1; i < index + count; i++)
@@ -970,7 +970,7 @@ public ImmutableArray<T> Sort(int index, int count, IComparer<T> comparer)
970970
}
971971
}
972972

973-
return new ImmutableArray<T>(self.array);
973+
return self;
974974
}
975975

976976
/// <summary>
@@ -1007,7 +1007,7 @@ public Enumerator GetEnumerator()
10071007
/// Returns a hash code for this instance.
10081008
/// </summary>
10091009
/// <returns>
1010-
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
1010+
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
10111011
/// </returns>
10121012
[Pure]
10131013
public override int GetHashCode()
@@ -1592,16 +1592,16 @@ internal void ThrowNullRefIfNotInitialized()
15921592
{
15931593
// Force NullReferenceException if array is null by touching its Length.
15941594
// This way of checking has a nice property of requiring very little code
1595-
// and not having any conditions/branches.
1595+
// and not having any conditions/branches.
15961596
// In a faulting scenario we are relying on hardware to generate the fault.
1597-
// And in the non-faulting scenario (most common) the check is virtually free since
1597+
// And in the non-faulting scenario (most common) the check is virtually free since
15981598
// if we are going to do anything with the array, we will need Length anyways
1599-
// so touching it, and potentially causing a cache miss, is not going to be an
1599+
// so touching it, and potentially causing a cache miss, is not going to be an
16001600
// extra expense.
16011601
var unused = this.array.Length;
16021602

16031603
// This line is a workaround for a bug in C# compiler
1604-
// The code in this line will not be emitted, but it will prevent incorrect
1604+
// The code in this line will not be emitted, but it will prevent incorrect
16051605
// optimizing away of "Length" call above in Release builds.
16061606
// TODO: remove the workaround when building with Roslyn which does not have this bug.
16071607
var unused2 = unused;
@@ -1612,7 +1612,7 @@ internal void ThrowNullRefIfNotInitialized()
16121612
/// <see cref="IsDefault"/> property returns true. The
16131613
/// <see cref="InvalidOperationException"/> message specifies that the operation cannot be performed
16141614
/// on a default instance of <see cref="ImmutableArray{T}"/>.
1615-
///
1615+
///
16161616
/// This is intended for explicitly implemented interface method and property implementations.
16171617
/// </summary>
16181618
private void ThrowInvalidOperationIfNotInitialized()
@@ -1642,7 +1642,7 @@ private ImmutableArray<T> RemoveAtRange(ICollection<int> indexesToRemove)
16421642
if (indexesToRemove.Count == 0)
16431643
{
16441644
// Be sure to return a !IsDefault instance.
1645-
return new ImmutableArray<T>(self.array);
1645+
return self;
16461646
}
16471647

16481648
var newArray = new T[self.Length - indexesToRemove.Count];

0 commit comments

Comments
 (0)