1- using System ;
2- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
33using System . Runtime . CompilerServices ;
4- using System . Text ;
5- using BenchmarkDotNet . Attributes ;
6- using Microsoft . CodeAnalysis . CSharp . Syntax ;
7- using Microsoft . Diagnostics . Runtime . Interop ;
8-
9- namespace BitFaster . Caching . Benchmarks
10- {
11- // Is it possible to write a class to eliminate the dispose code for types that are not IDisposable?
12- // https://github.com/dotnet/runtime/issues/4920
13- [ DisassemblyDiagnoser ( printSource : true ) ]
14- [ MemoryDiagnoser ]
15- public class DisposerBench
4+ using System . Text ;
5+ using BenchmarkDotNet . Attributes ;
6+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
7+ using Microsoft . Diagnostics . Runtime . Interop ;
8+
9+ namespace BitFaster . Caching . Benchmarks
10+ {
11+ // Is it possible to write a class to eliminate the dispose code for types that are not IDisposable?
12+ // https://github.com/dotnet/runtime/issues/4920
13+ [ DisassemblyDiagnoser ( printSource : true ) ]
14+ [ MemoryDiagnoser ]
15+ public class DisposerBench
1616 {
17- [ Benchmark ( Baseline = true ) ]
18- public void HandWritten ( )
19- {
20- for ( int i = 0 ; i < 1000 ; i ++ )
21- {
17+ [ Benchmark ( Baseline = true ) ]
18+ public void HandWritten ( )
19+ {
20+ for ( int i = 0 ; i < 1000 ; i ++ )
21+ {
2222 NotDisposable notDisposable = new NotDisposable ( ) ;
23- Disposable disposable = new Disposable ( ) ;
23+ Disposable disposable = new Disposable ( ) ;
2424 disposable . Dispose ( ) ;
25- }
25+ }
2626 }
2727
28- [ Benchmark ( ) ]
29- public void NotOptimized ( )
28+ [ Benchmark ( ) ]
29+ public void NotOptimized ( )
3030 {
31- for ( int i = 0 ; i < 1000 ; i ++ )
32- {
31+ for ( int i = 0 ; i < 1000 ; i ++ )
32+ {
3333 NotDisposable notDisposable = new NotDisposable ( ) ;
3434 Disposable disposable = new Disposable ( ) ;
3535
@@ -45,69 +45,69 @@ public void NotOptimized()
4545 }
4646 }
4747
48- [ Benchmark ( ) ]
49- public void GenericDisposerReadonlyProperty ( )
48+ [ Benchmark ( ) ]
49+ public void GenericDisposerReadonlyProperty ( )
5050 {
51- for ( int i = 0 ; i < 1000 ; i ++ )
52- {
51+ for ( int i = 0 ; i < 1000 ; i ++ )
52+ {
5353 NotDisposable notDisposable = new NotDisposable ( ) ;
54- Disposable disposable = new Disposable ( ) ;
54+ Disposable disposable = new Disposable ( ) ;
5555 Disposer < Disposable > . Dispose ( disposable ) ;
5656 Disposer < NotDisposable > . Dispose ( notDisposable ) ;
5757 }
58- }
59-
60- [ Benchmark ( ) ]
61- public void GenericDisposerStdCheck ( )
58+ }
59+
60+ [ Benchmark ( ) ]
61+ public void GenericDisposerStdCheck ( )
6262 {
63- for ( int i = 0 ; i < 1000 ; i ++ )
64- {
63+ for ( int i = 0 ; i < 1000 ; i ++ )
64+ {
6565 NotDisposable notDisposable = new NotDisposable ( ) ;
66- Disposable disposable = new Disposable ( ) ;
66+ Disposable disposable = new Disposable ( ) ;
6767 Disposer2 < Disposable > . Dispose ( disposable ) ;
6868 Disposer2 < NotDisposable > . Dispose ( notDisposable ) ;
6969 }
70- }
71- }
72-
73- public static class Disposer < T >
74- {
75- // try using a static readonly field
76- private static readonly bool shouldDispose = typeof ( IDisposable ) . IsAssignableFrom ( typeof ( T ) ) ;
77-
78- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
79- public static void Dispose ( T value )
80- {
81- if ( shouldDispose )
82- {
83- ( ( IDisposable ) value ) . Dispose ( ) ;
84- }
85- }
86- }
87-
70+ }
71+ }
72+
73+ public static class Disposer < T >
74+ {
75+ // try using a static readonly field
76+ private static readonly bool shouldDispose = typeof ( IDisposable ) . IsAssignableFrom ( typeof ( T ) ) ;
77+
78+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
79+ public static void Dispose ( T value )
80+ {
81+ if ( shouldDispose )
82+ {
83+ ( ( IDisposable ) value ) . Dispose ( ) ;
84+ }
85+ }
86+ }
87+
8888 public static class Disposer2 < T >
89- {
90- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
91- public static void Dispose ( T value )
92- {
89+ {
90+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
91+ public static void Dispose ( T value )
92+ {
9393 if ( value is IDisposable d )
9494 {
9595 d . Dispose ( ) ;
96- }
97- }
98- }
99-
100- public class NotDisposable
96+ }
97+ }
98+ }
99+
100+ public class NotDisposable
101101 { }
102102
103- public class Disposable : IDisposable
104- {
105- private bool isDisposed = false ;
106-
107- public void Dispose ( )
108- {
109- if ( ! isDisposed )
110- this . isDisposed = true ;
111- }
112- }
113- }
103+ public class Disposable : IDisposable
104+ {
105+ private bool isDisposed = false ;
106+
107+ public void Dispose ( )
108+ {
109+ if ( ! isDisposed )
110+ this . isDisposed = true ;
111+ }
112+ }
113+ }
0 commit comments