Skip to content

Commit 68c7c6a

Browse files
committed
[ImmutableArray] add wrapper.
1 parent 944a7a9 commit 68c7c6a

File tree

8 files changed

+111
-15
lines changed

8 files changed

+111
-15
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## ImmutableArrayWhereSelectSum
2+
3+
### Source
4+
[ImmutableArrayWhereSelectSum.cs](../../src/StructLinq.Benchmark/ImmutableArrayWhereSelectSum.cs)
5+
6+
### Results:
7+
``` ini
8+
9+
BenchmarkDotNet=v0.12.0, OS=Windows 10.0.19042
10+
Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
11+
.NET Core SDK=5.0.100-preview.7.20366.6
12+
[Host] : .NET Core 3.1.9 (CoreCLR 4.700.20.47201, CoreFX 4.700.20.47203), X64 RyuJIT
13+
DefaultJob : .NET Core 3.1.9 (CoreCLR 4.700.20.47201, CoreFX 4.700.20.47203), X64 RyuJIT
14+
15+
16+
```
17+
| Method | Mean | Error | StdDev | Ratio | RatioSD | Gen 0 | Gen 1 | Gen 2 | Allocated |
18+
|------------------- |---------:|---------:|---------:|------:|--------:|------:|------:|------:|----------:|
19+
| LINQ | 51.77 us | 0.492 us | 0.461 us | 1.00 | 0.00 | - | - | - | 104 B |
20+
| StructLinq | 68.73 us | 0.904 us | 0.846 us | 1.33 | 0.02 | - | - | - | - |
21+
| StructLinqFunction | 35.82 us | 0.350 us | 0.327 us | 0.69 | 0.01 | - | - | - | - |
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ReSharper disable once CheckNamespace
2+
3+
using System.Collections.Immutable;
4+
using System.Runtime.CompilerServices;
5+
using StructLinq.IList;
6+
7+
// ReSharper disable once CheckNamespace
8+
namespace StructLinq
9+
{
10+
public static partial class BCLStructEnumerable
11+
{
12+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13+
public static IListEnumerable<T, ImmutableArray<T>> ToStructEnumerable<T>(this ImmutableArray<T> enumerable)
14+
{
15+
return new IListEnumerable<T, ImmutableArray<T>>(enumerable, 0, enumerable.Length);
16+
}
17+
18+
}
19+
}

src/StructLinq.BCL/StructLinq.BCL.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<PackageReference Include="InlineIL.Fody" Version="1.6.0">
2323
<PrivateAssets>all</PrivateAssets>
2424
</PackageReference>
25+
<PackageReference Include="System.Collections.Immutable" Version="1.3.0" />
2526
</ItemGroup>
2627

2728
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Collections.Immutable;
2+
using System.Linq;
3+
using BenchmarkDotNet.Attributes;
4+
5+
namespace StructLinq.Benchmark
6+
{
7+
[MemoryDiagnoser]
8+
public class ImmutableArrayWhereSelectSum
9+
{
10+
private const int Count = 10000;
11+
private readonly ImmutableArray<int> array;
12+
13+
public ImmutableArrayWhereSelectSum()
14+
{
15+
array = Enumerable.Range(0, Count).ToImmutableArray();
16+
}
17+
18+
19+
[Benchmark(Baseline = true)]
20+
public int LINQ() => array
21+
.Where(x => (x & 1) == 0)
22+
.Select(x => x * 2)
23+
.Sum();
24+
25+
[Benchmark]
26+
public int StructLinq() => array
27+
.ToStructEnumerable()
28+
.Where(x => (x & 1) == 0, x => x)
29+
.Select(x => x * 2, x => x)
30+
.Sum(x=>x);
31+
32+
[Benchmark]
33+
public int StructLinqFunction()
34+
{
35+
var where = new WherePredicate();
36+
var select = new SelectFunction();
37+
return array
38+
.ToStructEnumerable()
39+
.Where(ref @where, x => x)
40+
.Select(ref @select, x => x, x => x)
41+
.Sum(x => x);
42+
}
43+
}
44+
}

src/StructLinq.Tests/IListTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ namespace StructLinq.Tests
77
{
88
public class IListTests : AbstractCollectionTests<
99
int,
10-
IListEnumerable<int>,
11-
IListEnumerator<int>>
10+
IListEnumerable<int, IList<int>>,
11+
IListEnumerator<int, IList<int>>>
1212
{
1313
[Fact]
1414
public void ShouldSameAsSystem()
@@ -19,7 +19,7 @@ public void ShouldSameAsSystem()
1919
Assert.Equal(sysArray, structArray);
2020
}
2121

22-
protected override IListEnumerable<int> Build(int size)
22+
protected override IListEnumerable<int, IList<int>> Build(int size)
2323
{
2424
IList<int> list = Enumerable.Range(-1, size).ToList();
2525
return list.ToStructEnumerable();

src/StructLinq/IList/IListEnumerable.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44

55
namespace StructLinq.IList
66
{
7-
public struct IListEnumerable<T> : IStructCollection<T, IListEnumerator<T>>
7+
public struct IListEnumerable<T, TList> : IStructCollection<T, IListEnumerator<T, TList>>
8+
where TList : IList<T>
89
{
910
#region private fields
10-
private readonly IList<T> list;
11+
private readonly TList list;
1112
private int length;
1213
private int start;
1314
#endregion
14-
public IListEnumerable(IList<T> list, int start, int length)
15+
public IListEnumerable(TList list, int start, int length)
1516
{
1617
this.list = list;
1718
this.length = length;
1819
this.start = start;
1920
}
2021

2122
[MethodImpl(MethodImplOptions.AggressiveInlining)]
22-
public readonly IListEnumerator<T> GetEnumerator()
23+
public readonly IListEnumerator<T, TList> GetEnumerator()
2324
{
24-
return new IListEnumerator<T>(list, start, Count);
25+
return new IListEnumerator<T, TList>(list, start, Count);
2526
}
2627
public readonly int Count
2728
{
@@ -42,7 +43,7 @@ public void Slice(uint start, uint? length)
4243
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4344
public object Clone()
4445
{
45-
return new IListEnumerable<T>(list, start, length);
46+
return new IListEnumerable<T, TList>(list, start, length);
4647
}
4748

4849
[MethodImpl(MethodImplOptions.AggressiveInlining)]

src/StructLinq/IList/IlistEnumerator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44

55
namespace StructLinq.IList
66
{
7-
public struct IListEnumerator<T> : IStructEnumerator<T>
7+
public struct IListEnumerator<T, TList> : IStructEnumerator<T>
8+
where TList : IList<T>
89
{
910
#region private fields
10-
private readonly IList<T> list;
11+
private readonly TList list;
1112
private readonly int endIndex;
1213
private readonly int start;
1314
private int index;
1415

1516
#endregion
16-
public IListEnumerator(IList<T> list, int start, int length)
17+
public IListEnumerator(TList list, int start, int length)
1718
{
1819
this.list = list;
1920
endIndex = length - 1 + start;
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Runtime.CompilerServices;
34
using StructLinq.IList;
45

@@ -8,9 +9,17 @@ namespace StructLinq
89
public static partial class StructEnumerable
910
{
1011
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11-
public static IListEnumerable<T> ToStructEnumerable<T>(this IList<T> enumerable)
12+
public static IListEnumerable<T, IList<T>> ToStructEnumerable<T>(this IList<T> enumerable)
1213
{
13-
return new IListEnumerable<T>(enumerable, 0, enumerable.Count);
14+
return new IListEnumerable<T, IList<T>>(enumerable, 0, enumerable.Count);
1415
}
16+
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
public static IListEnumerable<T, TList> ToStructEnumerable<T, TList>(this TList enumerable, Func<TList, IList<T>> _)
19+
where TList : IList<T>
20+
{
21+
return new IListEnumerable<T, TList>(enumerable, 0, enumerable.Count);
22+
}
23+
1524
}
1625
}

0 commit comments

Comments
 (0)