Skip to content

Commit 8cf25a0

Browse files
committed
managed pools must use array directly for performance
1 parent 8e13192 commit 8cf25a0

54 files changed

Lines changed: 736 additions & 661 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

LinqGen.Benchmarks/Cases/AddVsAddRange.cs

Lines changed: 0 additions & 170 deletions
This file was deleted.

LinqGen.Benchmarks/Cases/ArraySelectToArray.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using BenchmarkDotNet.Attributes;
44
using Cathei.LinqGen;
5+
using Cathei.LinqGen.Hidden;
56
using StructLinq;
67
using NetFabric.Hyperlinq;
78
using StructLinq.Range;
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// LinqGen.Benchmarks, Maxwell Keonwoo Kang <code.athei@gmail.com>, 2022
2+
3+
using BenchmarkDotNet.Attributes;
4+
using Cathei.LinqGen;
5+
using Cathei.LinqGen.Hidden;
6+
using StructLinq;
7+
using NetFabric.Hyperlinq;
8+
using StructLinq.Range;
9+
10+
namespace Cathei.LinqGen.Benchmarks.Cases;
11+
12+
[MemoryDiagnoser]
13+
public class ListVsPooledList
14+
{
15+
private int[] TestData { get; set; } = null!;
16+
17+
[Params(100, 10_000, 1_000_000)]
18+
public int Count { get; set; }
19+
20+
private List<int> sharedList;
21+
private PooledListNative<int> sharedNative;
22+
private PooledListManaged<int> sharedManaged;
23+
24+
[GlobalSetup]
25+
public void GlobalSetup()
26+
{
27+
TestData = Utils.Generate(42, Count).ToArray();
28+
29+
sharedList = new List<int>(Count);
30+
sharedNative = new PooledListNative<int>(Count);
31+
sharedManaged = new PooledListManaged<int>(Count);
32+
}
33+
34+
[GlobalCleanup]
35+
public void GlobalCleanup()
36+
{
37+
sharedNative.Dispose();
38+
sharedManaged.Dispose();
39+
}
40+
41+
[Benchmark(Baseline = true)]
42+
public List<int> List()
43+
{
44+
var buffer = new List<int>(TestData.Length);
45+
// var buffer = sharedList;
46+
47+
buffer.Clear();
48+
49+
for (int i = 0; i < TestData.Length; ++i)
50+
{
51+
buffer.Add(TestData[i] * 2);
52+
}
53+
54+
return buffer;
55+
}
56+
57+
[Benchmark()]
58+
public PooledListNative<int> Native()
59+
{
60+
using var buffer = new PooledListNative<int>(TestData.Length);
61+
62+
// var buffer = sharedNative;
63+
64+
buffer.Clear();
65+
66+
for (int i = 0; i < TestData.Length; ++i)
67+
{
68+
buffer.Add(TestData[i] * 2);
69+
}
70+
71+
return buffer;
72+
}
73+
74+
[Benchmark()]
75+
public PooledListManaged<int> Managed()
76+
{
77+
using var buffer = new PooledListManaged<int>(TestData.Length);
78+
79+
// var buffer = sharedManaged;
80+
81+
buffer.Clear();
82+
83+
for (int i = 0; i < TestData.Length; ++i)
84+
{
85+
buffer.Add(TestData[i] * 2);
86+
}
87+
88+
return buffer;
89+
}
90+
91+
// [Benchmark(Baseline = true)]
92+
// public int[] Linq()
93+
// {
94+
// return TestData
95+
// .Select(x=> x * 2)
96+
// .ToArray();
97+
// }
98+
//
99+
// [Benchmark]
100+
// public int[] LinqGenDelegate()
101+
// {
102+
// return TestData
103+
// .Specialize()
104+
// .Select(x => x * 2)
105+
// .ToArray();
106+
// }
107+
//
108+
// [Benchmark]
109+
// public int[] LinqGenStruct()
110+
// {
111+
// return TestData
112+
// .Specialize()
113+
// .Select(new Selector())
114+
// .ToArray();
115+
// }
116+
//
117+
// [Benchmark]
118+
// public int[] StructLinqDelegate()
119+
// {
120+
// return TestData
121+
// .ToStructEnumerable()
122+
// .Select(x => x * 2)
123+
// .ToArray();
124+
// }
125+
//
126+
// [Benchmark]
127+
// public int[] StructLinqStruct()
128+
// {
129+
// var selector = new Selector();
130+
//
131+
// return TestData
132+
// .ToStructEnumerable()
133+
// .Select(ref selector, x => x, x => x)
134+
// .ToArray(x => x);
135+
// }
136+
//
137+
// [Benchmark]
138+
// public int[] HyperLinqDelegate()
139+
// {
140+
// return TestData
141+
// .AsValueEnumerable()
142+
// .Select(x => x * 2)
143+
// .ToArray();
144+
// }
145+
//
146+
// [Benchmark]
147+
// public int[] HyperLinqStruct()
148+
// {
149+
// return TestData
150+
// .AsValueEnumerable()
151+
// .Select<int, Selector>()
152+
// .ToArray();
153+
// }
154+
155+
readonly struct Selector :
156+
StructLinq.IFunction<int, int>,
157+
NetFabric.Hyperlinq.IFunction<int, int>,
158+
IStructFunction<int, int>
159+
{
160+
public int Eval(int element)
161+
{
162+
return element * 2;
163+
}
164+
165+
public int Invoke(int arg)
166+
{
167+
return arg * 2;
168+
}
169+
}
170+
}

LinqGen.Unity/Assets/Scripts/LinqGenSampleBurst.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class LinqGenSampleBurst : MonoBehaviour
1313
void Start()
1414
{
1515
var input = new NativeArray<int>(myArray, Allocator.Persistent);
16-
var output = new NativeArray<int>(1, Allocator.Persistent);
16+
var output = new NativeArray<int>(myArray.Length, Allocator.Persistent);
1717

1818
var job = new LinqGenSampleJob()
1919
{
@@ -23,7 +23,10 @@ void Start()
2323

2424
job.Schedule().Complete();
2525

26-
Debug.Log("The result of the sum is: " + output[0]);
26+
foreach (var item in output)
27+
{
28+
Debug.Log(item);
29+
}
2730

2831
input.Dispose();
2932
output.Dispose();
@@ -41,7 +44,14 @@ public struct LinqGenSampleJob : IJob
4144

4245
public void Execute()
4346
{
44-
Output[0] = Input.Specialize().Select(new Selector()).Sum();
47+
int index = 0;
48+
49+
foreach (var item in Input.Specialize()
50+
.Select(new Selector())
51+
.OrderBy(new Comparer()))
52+
{
53+
Output[index++] = item;
54+
}
4555
}
4656
}
4757

@@ -52,3 +62,11 @@ public int Invoke(int arg)
5262
return arg * 10;
5363
}
5464
}
65+
66+
public struct Comparer : IComparer<int>
67+
{
68+
public int Compare(int x, int y)
69+
{
70+
return x - y;
71+
}
72+
}

0 commit comments

Comments
 (0)