1+ using BenchmarkDotNet . Attributes ;
2+ using BenchmarkDotNet . Jobs ;
3+
4+ namespace Benchmark . Queue
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 queue = new Benchmark . Struct . QueueOfJobClass ( ( nuint ) Size , & memory ) ;
27+ for ( int i = 0 ; i < Size ; i ++ )
28+ {
29+ item . ChangePtr ( queue . BackFuture ( ) ) ;
30+ item . SetInt32 ( in i ) ;
31+ item . SetInt64 ( i * 2 ) ;
32+ js2W . ChangePtr ( item . JobClass2Ptr ) ;
33+ js2W . SetInt32 ( i + 3 ) ;
34+ js2W . SetInt64 ( i * 3 ) ;
35+ queue . PushFuture ( ) ;
36+ }
37+
38+ if ( j > 50 )
39+ {
40+ queue . Clear ( ) ;
41+ }
42+ else
43+ {
44+ while ( queue . TryPop ( ) )
45+ {
46+ }
47+ }
48+ }
49+
50+ using var queue2 = new Benchmark . Struct . QueueOfJobClass ( ( nuint ) Size , & memory ) ;
51+ for ( int i = 0 ; i < Size ; i ++ )
52+ {
53+ item . ChangePtr ( queue2 . BackFuture ( ) ) ;
54+ item . SetInt32 ( in i ) ;
55+ item . SetInt64 ( i * 2 ) ;
56+ js2W . ChangePtr ( item . JobClass2Ptr ) ;
57+ js2W . SetInt32 ( i + 3 ) ;
58+ js2W . SetInt64 ( i * 3 ) ;
59+ queue2 . PushFuture ( ) ;
60+ }
61+
62+ if ( j > 50 )
63+ {
64+ queue2 . Clear ( ) ;
65+ }
66+ else
67+ {
68+ while ( queue2 . TryPop ( ) )
69+ {
70+ }
71+ }
72+ }
73+ }
74+ }
75+ }
76+
77+ [ Benchmark ( Baseline = true , Description = "System.Collections.Generic" ) ]
78+ public void SystemCollectionsStack ( )
79+ {
80+ unsafe
81+ {
82+ for ( int j = 0 ; j < 100 ; j ++ )
83+ {
84+ {
85+ var queue = new System . Collections . Generic . Queue < JobClass > ( Size ) ;
86+ for ( int i = 0 ; i < Size ; i ++ )
87+ {
88+ queue . Enqueue (
89+ new JobClass ( i , i * 2 )
90+ {
91+ JobClass2 = new JobClass2 ( i + 3 , i * 3 )
92+ }
93+ ) ;
94+ }
95+
96+ if ( j > 50 )
97+ {
98+ queue . Clear ( ) ;
99+ }
100+ else
101+ {
102+ while ( queue . TryDequeue ( out _ ) )
103+ {
104+ }
105+ }
106+ }
107+
108+ var queue2 = new System . Collections . Generic . Queue < JobClass > ( Size ) ;
109+ for ( int i = 0 ; i < Size ; i ++ )
110+ {
111+ queue2 . Enqueue (
112+ new JobClass ( i , i * 2 )
113+ {
114+ JobClass2 = new JobClass2 ( i + 3 , i * 3 )
115+ }
116+ ) ;
117+ }
118+
119+ if ( j > 50 )
120+ {
121+ queue2 . Clear ( ) ;
122+ }
123+ else
124+ {
125+ while ( queue2 . TryDequeue ( out _ ) )
126+ {
127+ }
128+ }
129+ }
130+ }
131+ }
132+ }
133+ }
0 commit comments