-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFixedBufferVsManagedArray.cs
199 lines (181 loc) · 7.32 KB
/
FixedBufferVsManagedArray.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
using BenchmarkDotNet.Loggers;
namespace jemalloc.Benchmarks
{
[OrderProvider(methodOrderPolicy: MethodOrderPolicy.Declared)]
public class FixedBufferVsManagedArrayBenchmark<T> : JemBenchmark<T, int> where T : struct, IEquatable<T>, IComparable<T>, IConvertible
{
public int ArraySize => Parameter;
public readonly T fill = typeof(T) == typeof(TestUDT) ?
JemUtil.ValToGenericStruct<TestUDT, T>(TestUDT.MakeTestRecord(JemUtil.Rng)) : GM<T>.Random();
public readonly (T factor, T max) mul = GM<T>.RandomMultiplyFactorAndValue();
public override void GlobalSetup()
{
DebugInfoThis();
base.GlobalSetup();
Info($"Array size is {ArraySize}.");
T[] managedArray = new T[ArraySize];
SetValue("managedArray", managedArray);
FixedBuffer<T> nativeArray = new FixedBuffer<T>(ArraySize);
nativeArray.Acquire();
SetValue("nativeArray", nativeArray);
if (Operation == Operation.FILL)
{
Info($"Array fill value is {fill}.");
SetValue("fill", fill);
}
else if (Operation == Operation.MATH)
{
Info($"Array fill value is {mul.max}.");
nativeArray.Fill(mul.max);
new Span<T>(managedArray).Fill(mul.max);
SetValue("fill", mul.max);
Info($"Array multiply factor is {mul.factor}.");
SetValue("mul", mul.factor);
}
}
#region Fill
[Benchmark(Description = "Fill a managed array with a single value.")]
[BenchmarkCategory("Fill")]
public void FillManagedArray()
{
T[] managedArray = GetValue<T[]>("managedArray");
T fill = GetValue<T>("fill");
for (int i = 0; i < managedArray.Length; i++)
{
managedArray[i] = fill;
}
}
[Benchmark(Description = "Fill a FixedBuffer on the system unmanaged heap with a single value.")]
[BenchmarkCategory("Fill")]
public void FillFixedBuffer()
{
FixedBuffer<T> nativeArray = GetValue<FixedBuffer<T>>("nativeArray");
T fill = GetValue<T>("fill");
nativeArray.Fill(fill);
}
[Benchmark(Description = "Create and Fill a managed array with a single value.")]
[BenchmarkCategory("Fill")]
public void FillManagedArrayWithCreate()
{
T[] managedArray = new T[ArraySize];
T fill = GetValue<T>("fill");
for (int i = 0; i < managedArray.Length; i++)
{
managedArray[i] = fill;
}
managedArray = null;
}
[Benchmark(Description = "Create and Fill a FixedBuffer on the system unmanaged heap with a single value.")]
[BenchmarkCategory("Fill")]
public void FillFixedBufferWithCreate()
{
FixedBuffer<T> nativeArray = new FixedBuffer<T>(ArraySize);
T fill = GetValue<T>("fill");
nativeArray.Fill(fill);
nativeArray.Free();
}
[GlobalCleanup(Target = nameof(FillFixedBuffer))]
public void FillValidateAndCleanup()
{
InfoThis();
T[] managedArray = GetValue<T[]>("managedArray");
FixedBuffer<T> nativeArray = GetValue<FixedBuffer<T>>("nativeArray");
T fill = GetValue<T>("fill");
for (int i = 0; i < ArraySize / 1000; i++)
{
int index = GM<T>.Rng.Next(0, ArraySize);
if (!nativeArray[index].Equals(fill))
{
Log.WriteLineError($"Native array at index {index} is {nativeArray[index]} not {fill}.");
throw new Exception();
}
}
nativeArray.Release();
nativeArray.Free();
managedArray = null;
RemoveValue("managedArray");
RemoveValue("nativeArray");
RemoveValue("fill");
}
#endregion
#region Arithmetic
[Benchmark(Description = "Multiply all values of a managed array with a single value.")]
[BenchmarkCategory("Arithmetic")]
public void ArithmeticMutiplyManagedArray()
{
T mul = GetValue<T>("mul");
T fill = GetValue<T>("fill");
T[] m = GetValue<T[]>("managedArray");
new Span<T>(m).Fill(fill);
for (int i = 0; i < m.Length; i++)
{
m[i] = GM<T>.Multiply(m[i], mul);
}
this.SetStatistic($"{nameof(ArithmeticMutiplyManagedArray)}_PrivateMemory", JemUtil.PrintBytes(JemUtil.ProcessPrivateMemory));
}
[Benchmark(Description = "Vector multiply all values of a native array with a single value.")]
[BenchmarkCategory("Arithmetic")]
public void ArithmeticMultiplyNativeArray()
{
DebugInfoThis();
T mul = GetValue<T>("mul");
T fill = GetValue<T>("fill");
FixedBuffer<T> array = GetValue<FixedBuffer<T>>("nativeArray");
array.Fill(fill);
array.VectorMultiply(mul);
this.SetStatistic($"{nameof(ArithmeticMultiplyNativeArray)}_PrivateMemory", JemUtil.PrintBytes(JemUtil.ProcessPrivateMemory));
}
[GlobalCleanup(Target = nameof(ArithmeticMultiplyNativeArray))]
public void ArithmeticMultiplyValidateAndCleanup()
{
InfoThis();
int index;
FixedBuffer<T> nativeArray = GetValue<FixedBuffer<T>>("nativeArray");
T[] managedArray = GetValue<T[]>("managedArray");
T mul = GetValue<T>("mul");
T fill = GetValue<T>("fill");
T val = GM<T>.Multiply(fill, mul);
for (int i = 0; i < ArraySize % 100; i++)
{
index = GM<T>.Rng.Next(0, ArraySize);
if (!nativeArray[index].Equals(val))
{
Log.WriteLineError($"Native array at index {index} is {nativeArray[index]} not {val}.");
throw new Exception();
}
else if (!managedArray[index].Equals(val))
{
Log.WriteLineError($"Managed array at index {index} is {managedArray[index]} not {val}.");
throw new Exception();
}
}
managedArray = null;
nativeArray.Release();
nativeArray.Free();
RemoveValue("managedArray");
RemoveValue("nativeArray");
RemoveValue("fill");
RemoveValue("mul");
}
#endregion
#region Create
[BenchmarkCategory("Create")]
[Benchmark(Description = "Create arrays on the .NET LOH", Baseline = true)]
public void CreateManagedArray()
{
T[] someData = new T[ArraySize];
}
[BenchmarkCategory("Create")]
[Benchmark(Description = "Create SafeArrays on the system unmanaged heap")]
public void CreateNativeArray()
{
SafeArray<T> array = new SafeArray<T>(ArraySize);
}
#endregion
}
}