Skip to content

Commit

Permalink
Remove CreateDefensiveCopy and inline it in ImmutableArray.Create (#3…
Browse files Browse the repository at this point in the history
  • Loading branch information
Therzok authored Feb 16, 2020
1 parent d9bc547 commit 9cba68e
Showing 1 changed file with 5 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,18 @@ public static ImmutableArray<T> CreateRange<T>(IEnumerable<T> items)
[Pure]
public static ImmutableArray<T> Create<T>(params T[]? items)
{
if (items == null)
if (items == null || items.Length == 0)
{
return Create<T>();
return ImmutableArray<T>.Empty;
}

// We can't trust that the array passed in will never be mutated by the caller.
// The caller may have passed in an array explicitly (not relying on compiler params keyword)
// and could then change the array after the call, thereby violating the immutable
// guarantee provided by this struct. So we always copy the array to ensure it won't ever change.
return CreateDefensiveCopy(items);
var tmp = new T[items.Length];
Array.Copy(items, tmp, items.Length);
return new ImmutableArray<T>(tmp);
}

/// <summary>
Expand Down Expand Up @@ -541,24 +543,5 @@ public static int BinarySearch<T>(this ImmutableArray<T> array, int index, int l
{
return Array.BinarySearch<T>(array.array!, index, length, value, comparer);
}

/// <summary>
/// Initializes a new instance of the <see cref="ImmutableArray{T}"/> struct.
/// </summary>
/// <param name="items">The array from which to copy.</param>
internal static ImmutableArray<T> CreateDefensiveCopy<T>(T[] items)
{
Debug.Assert(items != null);

if (items.Length == 0)
{
return ImmutableArray<T>.Empty; // use just a shared empty array, allowing the input array to be potentially GC'd
}

// defensive copy
var tmp = new T[items.Length];
Array.Copy(items, tmp, items.Length);
return new ImmutableArray<T>(tmp);
}
}
}

0 comments on commit 9cba68e

Please sign in to comment.