Repository navigation
Expand file tree
/
Copy pathMetadataCache.cs
More file actions
132 lines (111 loc) · 4.86 KB
/
Copy pathMetadataCache.cs
File metadata and controls
132 lines (111 loc) · 4.86 KB
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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Reflection.PortableExecutable;
using Microsoft.CodeAnalysis.InternalUtilities;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CompilerServer
{
internal class MetadataAndSymbolCache
{
// Store 100 entries -- arbitrary number
private const int CacheSize = 100;
private readonly ConcurrentLruCache<FileKey, Metadata> _metadataCache =
new ConcurrentLruCache<FileKey, Metadata>(CacheSize);
private ModuleMetadata CreateModuleMetadata(string path, bool prefetchEntireImage)
{
// TODO: exception handling?
var fileStream = FileUtilities.OpenRead(path);
var options = PEStreamOptions.PrefetchMetadata;
if (prefetchEntireImage)
{
options |= PEStreamOptions.PrefetchEntireImage;
}
return ModuleMetadata.CreateFromStream(fileStream, options);
}
private ImmutableArray<ModuleMetadata> GetAllModules(ModuleMetadata manifestModule, string assemblyDir)
{
ArrayBuilder<ModuleMetadata> moduleBuilder = null;
foreach (string moduleName in manifestModule.GetModuleNames())
{
if (moduleBuilder == null)
{
moduleBuilder = ArrayBuilder<ModuleMetadata>.GetInstance();
moduleBuilder.Add(manifestModule);
}
var module = CreateModuleMetadata(PathUtilities.CombineAbsoluteAndRelativePaths(assemblyDir, moduleName), prefetchEntireImage: false);
moduleBuilder.Add(module);
}
return (moduleBuilder != null) ? moduleBuilder.ToImmutableAndFree() : ImmutableArray.Create(manifestModule);
}
internal Metadata GetMetadata(string fullPath, MetadataReferenceProperties properties)
{
// Check if we have an entry in the dictionary.
FileKey? fileKey = GetUniqueFileKey(fullPath);
Metadata metadata;
if (fileKey.HasValue && _metadataCache.TryGetValue(fileKey.Value, out metadata) && metadata != null)
{
return metadata;
}
if (properties.Kind == MetadataImageKind.Module)
{
var result = CreateModuleMetadata(fullPath, prefetchEntireImage: true);
//?? never add modules to cache?
return result;
}
else
{
var primaryModule = CreateModuleMetadata(fullPath, prefetchEntireImage: false);
// Get all the modules, and load them. Create an assembly metadata.
var allModules = GetAllModules(primaryModule, Path.GetDirectoryName(fullPath));
Metadata result = AssemblyMetadata.Create(allModules);
result = _metadataCache.GetOrAdd(fileKey.Value, result);
return result;
}
}
/// <summary>
/// A unique file key encapsulates a file path, and change date
/// that can be used as the key to a dictionary.
/// If a file hasn't changed name or timestamp, we assume
/// it is unchanged.
///
/// Returns null if the file doesn't exist or otherwise can't be accessed.
/// </summary>
private FileKey? GetUniqueFileKey(string filePath)
{
try
{
return FileKey.Create(filePath);
}
catch (Exception)
{
// There are several exceptions that can occur here: NotSupportedException or PathTooLongException
// for a bad path, UnauthorizedAccessException for access denied, etc. Rather than listing them all,
// just catch all exceptions.
return null;
}
}
}
internal sealed class CachingMetadataReference : PortableExecutableReference
{
private static readonly MetadataAndSymbolCache s_mdCache = new MetadataAndSymbolCache();
public CachingMetadataReference(string fullPath, MetadataReferenceProperties properties)
: base(properties, fullPath)
{
}
protected override DocumentationProvider CreateDocumentationProvider()
{
return DocumentationProvider.Default;
}
protected override Metadata GetMetadataImpl()
{
return s_mdCache.GetMetadata(FilePath, Properties);
}
protected override PortableExecutableReference WithPropertiesImpl(MetadataReferenceProperties properties)
{
return new CachingMetadataReference(this.FilePath, properties);
}
}
}