mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
perf: Micro-optimization for copying arrays
This commit is contained in:
@@ -810,11 +810,29 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
if (count < 0)
|
if (count < 0)
|
||||||
throw new ArgumentOutOfRangeException("count");
|
throw new ArgumentOutOfRangeException("count");
|
||||||
|
|
||||||
T[] array = new T[count];
|
// Source is already an array
|
||||||
int i = 0;
|
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)
|
foreach (T item in extends)
|
||||||
|
{
|
||||||
array[i++] = item;
|
array[i++] = item;
|
||||||
|
if (i >= count)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i != count)
|
||||||
|
throw new ArgumentOutOfRangeException("count");
|
||||||
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user