77
88namespace BenchmarkDotNet . Disassemblers
99{
10- internal static class SourceCodeProvider
10+ internal class SourceCodeProvider : IDisposable
1111 {
12- private static readonly Dictionary < SourceFile , string [ ] > SourceFileCache = new Dictionary < SourceFile , string [ ] > ( ) ;
13- private static readonly Dictionary < SourceFile , string > SourceFilePathsCache = new Dictionary < SourceFile , string > ( ) ;
12+ private readonly Dictionary < SourceFile , string [ ] > sourceFileCache = new Dictionary < SourceFile , string [ ] > ( ) ;
13+ private readonly Dictionary < SourceFile , string > sourceFilePathsCache = new Dictionary < SourceFile , string > ( ) ;
14+ private readonly Dictionary < PdbInfo , ManagedSymbolModule > pdbReaders = new Dictionary < PdbInfo , ManagedSymbolModule > ( ) ;
15+ private readonly SymbolReader symbolReader = new SymbolReader ( TextWriter . Null ) { SymbolPath = SymbolPath . MicrosoftSymbolServerPath } ;
1416
15- internal static IEnumerable < Sharp > GetSource ( ClrMethod method , ILToNativeMap map )
17+ public void Dispose ( )
1618 {
17- var sourceLocation = method . GetSourceLocation ( map . ILOffset ) ;
19+ symbolReader . Dispose ( ) ;
20+ }
21+
22+ internal IEnumerable < Sharp > GetSource ( ClrMethod method , ILToNativeMap map )
23+ {
24+ var sourceLocation = GetSourceLocation ( method , map . ILOffset ) ;
1825 if ( sourceLocation == null )
1926 yield break ;
2027
@@ -39,12 +46,12 @@ internal static IEnumerable<Sharp> GetSource(ClrMethod method, ILToNativeMap map
3946 }
4047 }
4148
42- private static string GetFilePath ( SourceFile sourceFile )
43- => SourceFilePathsCache . TryGetValue ( sourceFile , out string filePath ) ? filePath : sourceFile . Url ;
49+ private string GetFilePath ( SourceFile sourceFile )
50+ => sourceFilePathsCache . TryGetValue ( sourceFile , out string filePath ) ? filePath : sourceFile . Url ;
4451
45- private static string ReadSourceLine ( SourceFile file , int line )
52+ private string ReadSourceLine ( SourceFile file , int line )
4653 {
47- if ( ! SourceFileCache . TryGetValue ( file , out string [ ] contents ) )
54+ if ( ! sourceFileCache . TryGetValue ( file , out string [ ] contents ) )
4855 {
4956 // GetSourceFile method returns path when file is stored on the same machine
5057 // otherwise it downloads it from the Symbol Server and returns the source code ;)
@@ -56,14 +63,14 @@ private static string ReadSourceLine(SourceFile file, int line)
5663 if ( File . Exists ( wholeFileOrJustPath ) )
5764 {
5865 contents = File . ReadAllLines ( wholeFileOrJustPath ) ;
59- SourceFilePathsCache . Add ( file , wholeFileOrJustPath ) ;
66+ sourceFilePathsCache . Add ( file , wholeFileOrJustPath ) ;
6067 }
6168 else
6269 {
6370 contents = wholeFileOrJustPath . Split ( new string [ ] { Environment . NewLine } , StringSplitOptions . None ) ;
6471 }
6572
66- SourceFileCache . Add ( file , contents ) ;
73+ sourceFileCache . Add ( file , contents ) ;
6774 }
6875
6976 return line - 1 < contents . Length
@@ -99,17 +106,8 @@ private static string GetSmartPointer(string sourceLine, int? start, int? end)
99106
100107 return new string ( prefix ) ;
101108 }
102- }
103-
104- internal static class ClrSourceExtensions
105- {
106- // TODO Not sure we want this to be a shared dictionary, especially without
107- // any synchronization. Probably want to put this hanging off the Context
108- // somewhere, or inside SymbolCache.
109- private static readonly Dictionary < PdbInfo , ManagedSymbolModule > s_pdbReaders = new Dictionary < PdbInfo , ManagedSymbolModule > ( ) ;
110- private static readonly SymbolReader symbolReader = new SymbolReader ( TextWriter . Null ) { SymbolPath = SymbolPath . MicrosoftSymbolServerPath } ;
111109
112- internal static SourceLocation GetSourceLocation ( this ClrMethod method , int ilOffset )
110+ internal SourceLocation GetSourceLocation ( ClrMethod method , int ilOffset )
113111 {
114112 var reader = GetReaderForMethod ( method ) ;
115113 if ( reader == null )
@@ -118,7 +116,7 @@ internal static SourceLocation GetSourceLocation(this ClrMethod method, int ilOf
118116 return reader . SourceLocationForManagedCode ( ( uint ) method . MetadataToken , ilOffset ) ;
119117 }
120118
121- internal static SourceLocation GetSourceLocation ( this ClrStackFrame frame )
119+ internal SourceLocation GetSourceLocation ( ClrStackFrame frame )
122120 {
123121 var reader = GetReaderForMethod ( frame . Method ) ;
124122 if ( reader == null )
@@ -145,15 +143,15 @@ private static int FindIlOffset(ClrStackFrame frame)
145143 return last ;
146144 }
147145
148- private static ManagedSymbolModule GetReaderForMethod ( ClrMethod method )
146+ private ManagedSymbolModule GetReaderForMethod ( ClrMethod method )
149147 {
150148 ClrModule module = method ? . Type ? . Module ;
151149 PdbInfo info = module ? . Pdb ;
152150
153151 ManagedSymbolModule ? reader = null ;
154152 if ( info != null )
155153 {
156- if ( ! s_pdbReaders . TryGetValue ( info , out reader ) )
154+ if ( ! pdbReaders . TryGetValue ( info , out reader ) )
157155 {
158156 string pdbPath = info . Path ;
159157 if ( pdbPath != null )
@@ -173,7 +171,7 @@ private static ManagedSymbolModule GetReaderForMethod(ClrMethod method)
173171 }
174172 }
175173
176- s_pdbReaders [ info ] = reader ;
174+ pdbReaders [ info ] = reader ;
177175 }
178176 }
179177
0 commit comments