perf: Micro-optimization for copying arrays

This commit is contained in:
Chris Cameron
2019-09-23 11:43:54 -04:00
parent 0e16606d75
commit 68d98021a5

View File

@@ -810,11 +810,29 @@ namespace ICD.Common.Utils.Extensions
if (count < 0)
throw new ArgumentOutOfRangeException("count");
T[] array = new T[count];
int i = 0;
// Source is already an array
T[] arrayCast = extends as T[];
if (arrayCast != null)
{
T[] output = new T[count];
Array.Copy(arrayCast, output, count);
return output;
}
// Dumb sequence case
T[] array = new T[count];
int i = 0;
foreach (T item in extends)
{
array[i++] = item;
if (i >= count)
break;
}
if (i != count)
throw new ArgumentOutOfRangeException("count");
return array;
}