Skip to content

Commit e9b8763

Browse files
author
Vyacheslav
committed
feat: resolve #22
1 parent 04c27dc commit e9b8763

File tree

8 files changed

+599
-6
lines changed

8 files changed

+599
-6
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Jobs;
3+
4+
namespace Benchmark.List
5+
{
6+
[MemoryDiagnoser]
7+
[SimpleJob(RuntimeMoniker.Net60)]
8+
[HideColumns("Error", "StdDev", "Median", "Gen0", "Gen1", "Gen2", "Alloc Ratio", "RatioSD")]
9+
public class ClassOptimalJob
10+
{
11+
[Params(100, 1000, 10000, 100000, 250000, 500000, 1000000)]
12+
public int Size;
13+
14+
[Benchmark(Description = $"StackMemoryCollections")]
15+
public void StackMemory()
16+
{
17+
unsafe
18+
{
19+
using (var memory = new StackMemoryCollections.Struct.StackMemory(JobClassHelper.SizeOf * (nuint)Size))
20+
{
21+
var item = new Benchmark.Struct.JobClassWrapper(memory.Start, false);
22+
var js2W = new Benchmark.Struct.JobClass2Wrapper(memory.Start, false);
23+
for (int j = 0; j < 100; j++)
24+
{
25+
{
26+
using var list = new Benchmark.Struct.ListOfJobClass(5, &memory);
27+
for (int i = 0; i < Size / 2; i++)
28+
{
29+
item.ChangePtr(list.GetFuture());
30+
item.SetInt32(in i);
31+
item.SetInt64(i * 2);
32+
js2W.ChangePtr(item.JobClass2Ptr);
33+
js2W.SetInt32(15);
34+
js2W.SetInt64(36);
35+
list.AddFuture();
36+
}
37+
38+
if (j > 50)
39+
{
40+
list.Clear();
41+
list.TrimExcess();
42+
}
43+
else
44+
{
45+
while (list.Size != 0)
46+
{
47+
list.Remove(list.Size - 1);
48+
}
49+
}
50+
}
51+
52+
using var list2 = new Benchmark.Struct.ListOfJobClass((nuint)Size, &memory);
53+
for (int i = 0; i < Size; i++)
54+
{
55+
item.ChangePtr(list2.GetFuture());
56+
item.SetInt32(in i);
57+
item.SetInt64(i * 2);
58+
js2W.ChangePtr(item.JobClass2Ptr);
59+
js2W.SetInt32(15);
60+
js2W.SetInt64(36);
61+
list2.AddFuture();
62+
}
63+
64+
if (j > 50)
65+
{
66+
list2.Clear();
67+
}
68+
else
69+
{
70+
while (list2.Size != 0)
71+
{
72+
list2.Remove(list2.Size - 1);
73+
}
74+
}
75+
}
76+
}
77+
}
78+
}
79+
80+
[Benchmark(Baseline = true, Description = "System.Collections.Generic")]
81+
public void SystemCollectionsStack()
82+
{
83+
unsafe
84+
{
85+
for (int j = 0; j < 100; j++)
86+
{
87+
{
88+
var list = new System.Collections.Generic.List<JobClass>(5);
89+
for (int i = 0; i < Size / 2; i++)
90+
{
91+
list.Add(new JobClass()
92+
{
93+
Int32 = i,
94+
Int64 = i * 2,
95+
JobClass2 = new JobClass2()
96+
{
97+
Int32 = 15,
98+
Int64 = 36
99+
}
100+
});
101+
}
102+
103+
if (j > 50)
104+
{
105+
list.Clear();
106+
list.TrimExcess();
107+
}
108+
else
109+
{
110+
while (list.Count != 0)
111+
{
112+
list.RemoveAt(list.Count - 1);
113+
}
114+
}
115+
}
116+
117+
var list2 = new System.Collections.Generic.List<JobClass>(Size);
118+
for (int i = 0; i < Size; i++)
119+
{
120+
list2.Add(new JobClass()
121+
{
122+
Int32 = i,
123+
Int64 = i * 2,
124+
JobClass2 = new JobClass2()
125+
{
126+
Int32 = 15,
127+
Int64 = 36
128+
}
129+
});
130+
}
131+
132+
if (j > 50)
133+
{
134+
list2.Clear();
135+
}
136+
else
137+
{
138+
while (list2.Count != 0)
139+
{
140+
list2.RemoveAt(list2.Count - 1);
141+
}
142+
}
143+
}
144+
}
145+
}
146+
}
147+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Jobs;
3+
4+
namespace Benchmark.List
5+
{
6+
[MemoryDiagnoser]
7+
[SimpleJob(RuntimeMoniker.Net60)]
8+
[HideColumns("Error", "StdDev", "Median", "Gen0", "Gen1", "Gen2", "Alloc Ratio", "RatioSD")]
9+
public class PrimitiveOptimalJob
10+
{
11+
[Params(100, 1000, 10000, 100000, 250000, 500000, 1000000)]
12+
public int Size;
13+
14+
[Benchmark(Description = $"StackMemoryCollections")]
15+
public void StackMemory()
16+
{
17+
unsafe
18+
{
19+
using (var memory = new StackMemoryCollections.Struct.StackMemory(sizeof(int) * (nuint)Size))
20+
{
21+
for (int j = 0; j < 100; j++)
22+
{
23+
{
24+
using var list = new StackMemoryCollections.Struct.ListOfInt32(5, &memory);
25+
for (int i = 0; i < Size/2; i++)
26+
{
27+
*list.GetFuture() = i;
28+
list.AddFuture();
29+
}
30+
31+
if(j > 50)
32+
{
33+
list.Clear();
34+
list.TrimExcess();
35+
}
36+
else
37+
{
38+
while (list.Size != 0)
39+
{
40+
list.Remove(list.Size - 1);
41+
}
42+
}
43+
}
44+
45+
using var list2 = new StackMemoryCollections.Struct.ListOfInt32((nuint)Size, &memory);
46+
for (int i = 0; i < Size; i++)
47+
{
48+
*list2.GetFuture() = i;
49+
list2.AddFuture();
50+
}
51+
52+
if (j > 50)
53+
{
54+
list2.Clear();
55+
list2.TrimExcess();
56+
}
57+
else
58+
{
59+
while (list2.Size != 0)
60+
{
61+
list2.Remove(list2.Size - 1);
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}
68+
69+
[Benchmark(Baseline = true, Description = "System.Collections.Generic")]
70+
public void SystemCollectionsStack()
71+
{
72+
unsafe
73+
{
74+
for (int j = 0; j < 100; j++)
75+
{
76+
{
77+
var list = new System.Collections.Generic.List<int>(5);
78+
for (int i = 0; i < Size / 2; i++)
79+
{
80+
list.Add(i);
81+
}
82+
83+
if (j > 50)
84+
{
85+
list.Clear();
86+
list.TrimExcess();
87+
}
88+
else
89+
{
90+
while (list.Count != 0)
91+
{
92+
list.RemoveAt(list.Count - 1);
93+
}
94+
}
95+
}
96+
97+
var list2 = new System.Collections.Generic.List<int>(Size);
98+
for (int i = 0; i < Size; i++)
99+
{
100+
list2.Add(i);
101+
}
102+
103+
if (j > 50)
104+
{
105+
list2.Clear();
106+
}
107+
else
108+
{
109+
while (list2.Count != 0)
110+
{
111+
list2.RemoveAt(list2.Count - 1);
112+
}
113+
}
114+
}
115+
}
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)