Skip to content

Commit 8e13192

Browse files
committed
testing dynamic array performance
1 parent 8640d1f commit 8e13192

52 files changed

Lines changed: 1097 additions & 548 deletions

Some content is hidden

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

.idea/.idea.LinqGen/.idea/indexLayout.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// LinqGen.Benchmarks, Maxwell Keonwoo Kang <code.athei@gmail.com>, 2022
2+
3+
using BenchmarkDotNet.Attributes;
4+
using Cathei.LinqGen;
5+
using StructLinq;
6+
using NetFabric.Hyperlinq;
7+
using StructLinq.Range;
8+
9+
namespace Cathei.LinqGen.Benchmarks.Cases;
10+
11+
[MemoryDiagnoser]
12+
public class ArraySelectToArray
13+
{
14+
private int[] TestData { get; set; } = null!;
15+
16+
[Params(100, 10_000, 1_000_000)]
17+
public int Count { get; set; }
18+
19+
[GlobalSetup]
20+
public void GlobalSetup()
21+
{
22+
TestData = Utils.Generate(42, Count).ToArray();
23+
}
24+
25+
[Benchmark(Baseline = true)]
26+
public int[] Linq()
27+
{
28+
return TestData
29+
.Select(x=> x * 2)
30+
.ToArray();
31+
}
32+
33+
[Benchmark]
34+
public int[] LinqGenDelegate()
35+
{
36+
return TestData
37+
.Specialize()
38+
.Select(x => x * 2)
39+
.ToArray();
40+
}
41+
42+
[Benchmark]
43+
public int[] LinqGenStruct()
44+
{
45+
return TestData
46+
.Specialize()
47+
.Select(new Selector())
48+
.ToArray();
49+
}
50+
51+
[Benchmark]
52+
public int[] StructLinqDelegate()
53+
{
54+
return TestData
55+
.ToStructEnumerable()
56+
.Select(x => x * 2)
57+
.ToArray();
58+
}
59+
60+
[Benchmark]
61+
public int[] StructLinqStruct()
62+
{
63+
var selector = new Selector();
64+
65+
return TestData
66+
.ToStructEnumerable()
67+
.Select(ref selector, x => x, x => x)
68+
.ToArray(x => x);
69+
}
70+
71+
[Benchmark]
72+
public int[] HyperLinqDelegate()
73+
{
74+
return TestData
75+
.AsValueEnumerable()
76+
.Select(x => x * 2)
77+
.ToArray();
78+
}
79+
80+
[Benchmark]
81+
public int[] HyperLinqStruct()
82+
{
83+
return TestData
84+
.AsValueEnumerable()
85+
.Select<int, Selector>()
86+
.ToArray();
87+
}
88+
89+
readonly struct Selector :
90+
StructLinq.IFunction<int, int>,
91+
NetFabric.Hyperlinq.IFunction<int, int>,
92+
IStructFunction<int, int>
93+
{
94+
public int Eval(int element)
95+
{
96+
return element * 2;
97+
}
98+
99+
public int Invoke(int arg)
100+
{
101+
return arg * 2;
102+
}
103+
}
104+
}

LinqGen.Tests/Evaluations/ToArrayTests.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ namespace Cathei.LinqGen.Tests;
1111
[TestFixture]
1212
public class ToArrayTests
1313
{
14-
1514
[Test]
1615
public void Test_ArrayWhereToArray()
1716
{
@@ -21,12 +20,18 @@ public void Test_ArrayWhereToArray()
2120
.Where(x => x % 2 == 0)
2221
.Select(x => x * 2)
2322
.ToArray();
24-
25-
2623
}
2724

25+
[Test]
26+
public void Test_BigArrayToArray()
27+
{
28+
int[] values = new int[1_000_000];
2829

29-
30+
var arr =values.Specialize()
31+
.Where(x => x % 2 == 0)
32+
.Select(x => x * 2)
33+
.ToArray();
34+
}
3035

3136
[TestCase(0, 0)]
3237
[TestCase(0, 10)]
512 Bytes
Binary file not shown.

LinqGen.Unity/Packages/com.cathei.linqgen/Runtime/Core/Collections.meta

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

LinqGen.Unity/Packages/com.cathei.linqgen/Runtime/Core/Collections/HashHelpers.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// LinqGen, Maxwell Keonwoo Kang <code.athei@gmail.com>, 2022
2+
3+
using System;
4+
using System.Runtime.CompilerServices;
5+
6+
namespace Cathei.LinqGen.Hidden
7+
{
8+
/// <summary>
9+
/// This interface is guideline for dynamic array implementation
10+
/// </summary>
11+
public interface IDynamicArray<T> : IDisposable
12+
{
13+
void IncreaseCapacity(int newSize, int copyCount = 0);
14+
15+
void Clear();
16+
17+
void CopyTo(T[] array, int count);
18+
19+
ref T this[int index] { get; }
20+
21+
ref T this[uint index] { get; }
22+
23+
int Length { get; }
24+
}
25+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Buffers;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace Cathei.LinqGen.Hidden
8+
{
9+
public struct DynamicArrayManaged<T> : IDynamicArray<T>
10+
{
11+
private T[] _array;
12+
13+
private static readonly T[] EmptyArray = Array.Empty<T>();
14+
15+
public ref T this[int index]
16+
{
17+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
18+
get => ref _array[index];
19+
}
20+
21+
public ref T this[uint index]
22+
{
23+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
24+
get => ref _array[index];
25+
}
26+
27+
public int Length
28+
{
29+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30+
get => _array.Length;
31+
}
32+
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
public DynamicArrayManaged(int capacity) : this()
35+
{
36+
_array = capacity > 0 ? SharedArrayPool<T>.Rent(capacity) : EmptyArray;
37+
}
38+
39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
public void IncreaseCapacity(int newSize, int copyCount = 0)
41+
{
42+
var newItems = SharedArrayPool<T>.Rent(newSize);
43+
if (copyCount > 0)
44+
Array.Copy(_array, newItems, copyCount);
45+
ReturnArray();
46+
_array = newItems;
47+
}
48+
49+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50+
public void Clear()
51+
{
52+
Array.Clear(_array, 0, _array.Length);
53+
}
54+
55+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
56+
private void ReturnArray()
57+
{
58+
if (_array == null)
59+
return;
60+
61+
try
62+
{
63+
// Clear the elements so that the gc can reclaim the references.
64+
SharedArrayPool<T>.Return(_array, true);
65+
}
66+
catch (ArgumentException)
67+
{
68+
// oh well, the array pool didn't like our array
69+
}
70+
71+
_array = null!;
72+
}
73+
74+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
75+
public void CopyTo(T[] array, int count)
76+
{
77+
if (_array == null)
78+
return;
79+
80+
Array.Copy(_array, array, count);
81+
}
82+
83+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
84+
public void Dispose()
85+
{
86+
ReturnArray();
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)