Skip to content

Commit 737cc38

Browse files
committed
[ToArray] Use GC.AllocateUninitializedArray for array allocation.
1 parent 7263133 commit 737cc38

9 files changed

+51
-33
lines changed

Documents/BenchmarksResults/ToArrayComparison.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical
1717
```
1818
| Method | Job | Runtime | Mean | Error | StdDev | Code Size | Gen 0 | Gen 1 | Gen 2 | Allocated |
1919
|------------------------ |-------------- |-------------- |----------:|----------:|----------:|----------:|--------:|-------:|------:|----------:|
20-
| ToListThenToArray | .NET 4.8 | .NET 4.8 | 29.983 μs | 0.1955 μs | 0.1829 μs | 0.58 KB | 36.1328 | - | - | 167.59 KB |
21-
| ToPooledListThenToArray | .NET 4.8 | .NET 4.8 | 17.976 μs | 0.1138 μs | 0.1065 μs | 1.56 KB | 8.4534 | 0.0305 | - | 39.11 KB |
22-
| UseCountForToArray | .NET 4.8 | .NET 4.8 | 6.248 μs | 0.0391 μs | 0.0366 μs | 0.09 KB | 8.4686 | 0.0076 | - | 39.11 KB |
23-
| StructLinqToArray | .NET 4.8 | .NET 4.8 | 5.788 μs | 0.0217 μs | 0.0203 μs | 0.45 KB | 8.4686 | 0.0076 | - | 39.11 KB |
24-
| ToListThenToArray | .NET Core 5.0 | .NET Core 5.0 | 22.911 μs | 0.1639 μs | 0.1533 μs | 1.33 KB | 36.1328 | 0.0305 | - | 167.41 KB |
25-
| ToPooledListThenToArray | .NET Core 5.0 | .NET Core 5.0 | 19.488 μs | 0.0558 μs | 0.0522 μs | 1.26 KB | 8.4534 | 1.0376 | - | 39.09 KB |
26-
| UseCountForToArray | .NET Core 5.0 | .NET Core 5.0 | 6.230 μs | 0.0173 μs | 0.0153 μs | 0.09 KB | 8.4686 | 1.0529 | - | 39.09 KB |
27-
| StructLinqToArray | .NET Core 5.0 | .NET Core 5.0 | 5.886 μs | 0.0441 μs | 0.0412 μs | 0.23 KB | 8.4686 | 1.0529 | - | 39.09 KB |
20+
| ToListThenToArray | .NET 4.8 | .NET 4.8 | 33.230 μs | 0.3353 μs | 0.2972 μs | 0.58 KB | 36.1328 | - | - | 167.59 KB |
21+
| ToPooledListThenToArray | .NET 4.8 | .NET 4.8 | 18.844 μs | 0.3548 μs | 0.3796 μs | 1.56 KB | 8.4534 | 0.0305 | - | 39.11 KB |
22+
| UseCountForToArray | .NET 4.8 | .NET 4.8 | 11.251 μs | 0.0773 μs | 0.0723 μs | 0.09 KB | 8.4686 | 0.0153 | - | 39.11 KB |
23+
| StructLinqToArray | .NET 4.8 | .NET 4.8 | 6.066 μs | 0.0157 μs | 0.0122 μs | 0.45 KB | 8.4686 | 0.0076 | - | 39.11 KB |
24+
| ToListThenToArray | .NET Core 5.0 | .NET Core 5.0 | 22.805 μs | 0.1764 μs | 0.1650 μs | 1.33 KB | 36.1328 | 0.0305 | - | 167.41 KB |
25+
| ToPooledListThenToArray | .NET Core 5.0 | .NET Core 5.0 | 17.015 μs | 0.0669 μs | 0.0593 μs | 1.35 KB | 8.4534 | 1.0376 | - | 39.09 KB |
26+
| UseCountForToArray | .NET Core 5.0 | .NET Core 5.0 | 5.552 μs | 0.0181 μs | 0.0170 μs | 0.09 KB | 8.4686 | 1.0529 | - | 39.09 KB |
27+
| StructLinqToArray | .NET Core 5.0 | .NET Core 5.0 | 4.978 μs | 0.0164 μs | 0.0153 μs | 0.32 KB | 8.4686 | 1.0529 | - | 39.09 KB |

Documents/BenchmarksResults/ToArrayOnArraySelect.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical
1414

1515

1616
```
17-
| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | Code Size |
18-
|----------------------- |----------:|----------:|----------:|------:|-------:|-------:|------:|----------:|----------:|
19-
| Linq | 16.353 μs | 0.0435 μs | 0.0340 μs | 1.00 | 8.4534 | 1.0376 | - | 39.13 KB | 0.96 KB |
20-
| StructLinq | 16.378 μs | 0.0595 μs | 0.0556 μs | 1.00 | 8.4534 | 1.0376 | - | 39.15 KB | 0.42 KB |
21-
| StructLinqZeroAlloc | 18.904 μs | 0.1015 μs | 0.0950 μs | 1.16 | 8.4534 | 1.0376 | - | 39.12 KB | 0.58 KB |
22-
| StructLinqWithFunction | 7.727 μs | 0.0387 μs | 0.0362 μs | 0.47 | 8.4686 | 1.0529 | - | 39.09 KB | 0.78 KB |
17+
| Method | Mean | Error | StdDev | Ratio | Code Size | Gen 0 | Gen 1 | Gen 2 | Allocated |
18+
|----------------------- |---------:|---------:|---------:|------:|----------:|-------:|-------:|------:|----------:|
19+
| Linq | 18.95 μs | 0.081 μs | 0.072 μs | 1.00 | 0.96 KB | 8.4534 | 1.0376 | - | 39.13 KB |
20+
| StructLinq | 18.33 μs | 0.128 μs | 0.113 μs | 0.97 | 0.51 KB | 8.4534 | 1.0376 | - | 39.15 KB |
21+
| StructLinqZeroAlloc | 20.81 μs | 0.108 μs | 0.090 μs | 1.10 | 0.66 KB | 8.4534 | 1.0376 | - | 39.12 KB |
22+
| StructLinqWithFunction | 10.48 μs | 0.035 μs | 0.031 μs | 0.55 | 0.87 KB | 8.4686 | 1.0529 | - | 39.09 KB |

Documents/BenchmarksResults/ToArrayOnArraySelectOfClass.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical
1414

1515

1616
```
17-
| Method | Mean | Error | StdDev | Ratio | Code Size | Gen 0 | Gen 1 | Gen 2 | Allocated |
18-
|----------------------- |----------:|----------:|----------:|------:|----------:|-------:|-------:|------:|----------:|
19-
| Linq | 24.196 μs | 0.0898 μs | 0.0796 μs | 1.00 | 0.15 KB | 8.4534 | 1.0376 | - | 39.13 KB |
20-
| StructLinq | 24.224 μs | 0.1128 μs | 0.1055 μs | 1.00 | 0.05 KB | 8.4534 | 1.0376 | - | 39.15 KB |
21-
| StructLinqZeroAlloc | 24.116 μs | 0.0938 μs | 0.0877 μs | 1.00 | 0.05 KB | 8.4534 | 1.0376 | - | 39.09 KB |
22-
| StructLinqWithFunction | 8.959 μs | 0.1163 μs | 0.1088 μs | 0.37 | 0.78 KB | 8.4686 | 1.0529 | - | 39.09 KB |
17+
| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated | Code Size |
18+
|----------------------- |---------:|---------:|---------:|------:|-------:|-------:|------:|----------:|----------:|
19+
| Linq | 17.17 μs | 0.129 μs | 0.115 μs | 1.00 | 8.4534 | 1.0376 | - | 39.13 KB | 1.36 KB |
20+
| StructLinq | 18.69 μs | 0.070 μs | 0.062 μs | 1.09 | 8.4534 | 1.0376 | - | 39.15 KB | 0.52 KB |
21+
| StructLinqZeroAlloc | 18.33 μs | 0.113 μs | 0.106 μs | 1.07 | 8.4534 | 1.0376 | - | 39.09 KB | 0.9 KB |
22+
| StructLinqWithFunction | 13.08 μs | 0.019 μs | 0.017 μs | 0.76 | 8.4686 | 1.0529 | - | 39.09 KB | 0.87 KB |

Documents/BenchmarksResults/ToArrayOnArrayWhere.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical
1616
```
1717
| Method | Mean | Error | StdDev | Ratio | Gen 0 | Gen 1 | Gen 2 | Allocated |
1818
|----------------------- |---------:|---------:|---------:|------:|--------:|-------:|------:|----------:|
19-
| Linq | 24.99 μs | 0.122 μs | 0.108 μs | 1.00 | 11.3220 | 1.4038 | - | 52.19 KB |
20-
| StructLinq | 32.34 μs | 0.259 μs | 0.242 μs | 1.29 | 4.2114 | 0.2441 | - | 19.62 KB |
21-
| StructLinqZeroAlloc | 26.59 μs | 0.140 μs | 0.131 μs | 1.06 | 4.2114 | 0.2747 | - | 19.55 KB |
22-
| StructLinqWithFunction | 11.46 μs | 0.034 μs | 0.028 μs | 0.46 | 4.2267 | 0.2747 | - | 19.55 KB |
19+
| Linq | 24.80 μs | 0.136 μs | 0.128 μs | 1.00 | 11.3220 | 1.4038 | - | 52.19 KB |
20+
| StructLinq | 25.10 μs | 0.069 μs | 0.061 μs | 1.01 | 4.2419 | 0.2747 | - | 19.62 KB |
21+
| StructLinqZeroAlloc | 27.87 μs | 0.170 μs | 0.159 μs | 1.12 | 4.2114 | 0.2747 | - | 19.55 KB |
22+
| StructLinqWithFunction | 11.15 μs | 0.065 μs | 0.057 μs | 0.45 | 4.2267 | 0.2747 | - | 19.55 KB |

Documents/BenchmarksResults/ToArrayOnArrayWhereOfClass.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical
1616
```
1717
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
1818
|----------------------- |----------:|---------:|---------:|------:|--------:|--------:|-------:|------:|----------:|
19-
| Linq | 59.20 μs | 0.296 μs | 0.247 μs | 1.00 | 0.00 | 22.4609 | 5.6152 | - | 103.73 KB |
20-
| StructLinq | 106.67 μs | 0.345 μs | 0.323 μs | 1.80 | 0.01 | 8.4229 | 0.9766 | - | 39.15 KB |
21-
| StructLinqZeroAlloc | 109.09 μs | 0.883 μs | 0.826 μs | 1.84 | 0.02 | 8.4229 | 0.9766 | - | 39.09 KB |
22-
| StructLinqWithFunction | 100.58 μs | 0.640 μs | 0.567 μs | 1.70 | 0.01 | 8.4229 | 0.9766 | - | 39.09 KB |
19+
| Linq | 59.35 μs | 0.443 μs | 0.393 μs | 1.00 | 0.00 | 22.4609 | 5.6152 | - | 103.73 KB |
20+
| StructLinq | 121.58 μs | 0.616 μs | 0.481 μs | 2.05 | 0.02 | 8.3008 | 0.9766 | - | 39.15 KB |
21+
| StructLinqZeroAlloc | 111.41 μs | 0.519 μs | 0.485 μs | 1.88 | 0.01 | 8.4229 | 0.9766 | - | 39.09 KB |
22+
| StructLinqWithFunction | 87.99 μs | 0.383 μs | 0.359 μs | 1.48 | 0.01 | 8.4229 | 0.9766 | - | 39.09 KB |

src/StructLinq/ToArray/StructCollection.ToArray.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Runtime.CompilerServices;
3+
using StructLinq.Utils;
34

45
// ReSharper disable once CheckNamespace
56
namespace StructLinq
@@ -11,7 +12,7 @@ private static T[] ToArray<T, TEnumerator>(ref TEnumerator enumerator)
1112
where TEnumerator : struct, ICollectionEnumerator<T>
1213
{
1314
var count = enumerator.Count;
14-
var result = new T[count];
15+
var result = ArrayHelpers.Create<T>(count);
1516
for (int i = 0; i < count; i++)
1617
{
1718
result[i] = enumerator.Get(i);
@@ -25,7 +26,7 @@ private static T[] ToRefArray<T, TEnumerator>(ref TEnumerator enumerator)
2526
where TEnumerator : struct, IRefCollectionEnumerator<T>
2627
{
2728
var count = enumerator.Count;
28-
var result = new T[count];
29+
var result = ArrayHelpers.Create<T>(count);
2930
for (int i = 0; i < count; i++)
3031
{
3132
ref var item = ref result[i];

src/StructLinq/Utils/ArrayHelpers.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
4+
namespace StructLinq.Utils
5+
{
6+
public static class ArrayHelpers
7+
{
8+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9+
public static T[] Create<T>(int count)
10+
{
11+
#if NET5_0_OR_GREATER
12+
return GC.AllocateUninitializedArray<T>(count);
13+
#else
14+
return new T[count];
15+
#endif
16+
}
17+
}
18+
}

src/StructLinq/Utils/Collections/PooledList.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void Dispose()
8787
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8888
public T[] ToArray()
8989
{
90-
var result = new T[Size];
90+
var result = ArrayHelpers.Create<T>(Size);
9191
System.Array.Copy(Items, result, Size);
9292
return result;
9393
}

src/StructLinq/Utils/UnsafeHelpers.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Runtime.CompilerServices;
1+
using System.Runtime.CompilerServices;
32
using InlineIL;
43
using static InlineIL.IL.Emit;
54
// ReSharper disable EntityNameCapturedOnly.Global

0 commit comments

Comments
 (0)