mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 12:07:05 +00:00
perf: Micro-optimization for copying arrays
This commit is contained in:
parent
0e16606d75
commit
68d98021a5
1 changed files with 20 additions and 2 deletions
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue