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

Commit 3cb3df1

Browse files
committed
Minor ImmutableArray perf tweaks
Removed some dead code and ensured Array.Copy is called with start indexes.
1 parent 8cd5786 commit 3cb3df1

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.Diagnostics.Contracts;
67
using System.Linq;
78
using Validation;
@@ -107,11 +108,12 @@ public static ImmutableArray<T> CreateRange<T>(IEnumerable<T> items)
107108
{
108109
immutableArray.ThrowInvalidOperationIfNotInitialized();
109110

110-
var existingImmutableArray = immutableArray.Array as T[];
111-
if (existingImmutableArray != null || immutableArray.Array == null)
112-
{
113-
return new ImmutableArray<T>(existingImmutableArray);
114-
}
111+
// immutableArray.Array must not be null at this point, and we know it's an
112+
// ImmutableArray<T> or ImmutableArray<SomethingDerivedFromT> as they are
113+
// the only types that could be both IEnumerable<T> and IImmutableArray.
114+
// As such, we know that items is either an ImmutableArray<T> or
115+
// ImmutableArray<TypeDerivedFromT>, and we can cast the array to T[].
116+
return new ImmutableArray<T>((T[])immutableArray.Array);
115117
}
116118

117119
// We don't recognize the source as an array that is safe to use.
@@ -534,14 +536,10 @@ public static int BinarySearch<T>(this ImmutableArray<T> array, int index, int l
534536
/// <summary>
535537
/// Initializes a new instance of the <see cref="ImmutableArray{T}"/> struct.
536538
/// </summary>
537-
/// <param name="items">The array to use or copy from. May be null for "default" arrays.</param>
539+
/// <param name="items">The array from which to copy.</param>
538540
internal static ImmutableArray<T> CreateDefensiveCopy<T>(T[] items)
539541
{
540-
// Some folks lazily initialize fields containing these structs, so retaining a null vs. empty array status is useful.
541-
if (items == null)
542-
{
543-
return default(ImmutableArray<T>);
544-
}
542+
Debug.Assert(items != null);
545543

546544
if (items.Length == 0)
547545
{
@@ -550,7 +548,7 @@ internal static ImmutableArray<T> CreateDefensiveCopy<T>(T[] items)
550548

551549
// defensive copy
552550
var tmp = new T[items.Length];
553-
Array.Copy(items, tmp, items.Length);
551+
Array.Copy(items, 0, tmp, 0, items.Length);
554552
return new ImmutableArray<T>(tmp);
555553
}
556554
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ public ImmutableArray<T> SetItem(int index, T item)
659659
Requires.Range(index >= 0 && index < self.Length, "index");
660660

661661
T[] tmp = new T[self.Length];
662-
Array.Copy(self.array, tmp, self.Length);
662+
Array.Copy(self.array, 0, tmp, 0, self.Length);
663663
tmp[index] = item;
664664
return new ImmutableArray<T>(tmp);
665665
}
@@ -944,7 +944,7 @@ public ImmutableArray<T> Sort(int index, int count, IComparer<T> comparer)
944944
if (outOfOrder)
945945
{
946946
var tmp = new T[self.Length];
947-
Array.Copy(self.array, tmp, self.Length);
947+
Array.Copy(self.array, 0, tmp, 0, self.Length);
948948
Array.Sort(tmp, index, count, comparer);
949949
return new ImmutableArray<T>(tmp);
950950
}

0 commit comments

Comments
 (0)