2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ using System . Collections . Immutable ;
6
+
5
7
namespace Microsoft . CodeAnalysis ;
6
8
7
9
internal partial struct SymbolKey
@@ -24,12 +26,27 @@ protected sealed override SymbolKeyResolution Resolve(
24
26
return default ;
25
27
}
26
28
27
- using var result = PooledArrayBuilder < IModuleSymbol > . GetInstance ( ) ;
28
- foreach ( var assembly in containingSymbolResolution . OfType < IAssemblySymbol > ( ) )
29
+ using var result = PooledArrayBuilder < IModuleSymbol > . GetInstance ( containingSymbolResolution . SymbolCount ) ;
30
+ foreach ( var symbol in containingSymbolResolution )
29
31
{
32
+ if ( symbol is not IAssemblySymbol assembly )
33
+ continue ;
34
+
30
35
// Don't check ModuleIds for equality because in practice, no-one uses them,
31
36
// and there is no way to set netmodule name programmatically using Roslyn
32
- result . AddValuesIfNotNull ( assembly . Modules ) ;
37
+ var assemblyModules = assembly . Modules ;
38
+ if ( assemblyModules is ImmutableArray < IModuleSymbol > modules )
39
+ {
40
+ // Avoid allocations if possible
41
+ // https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2136177
42
+ result . AddValuesIfNotNull ( modules ) ;
43
+ }
44
+ else
45
+ {
46
+ // Visual Basic implementation of IAssemblySymbol.Modules relies on covariance of IEnumerable<T>, so
47
+ // the preceding concrete type check will fail.
48
+ result . AddValuesIfNotNull ( assemblyModules ) ;
49
+ }
33
50
}
34
51
35
52
return CreateResolution ( result , $ "({ nameof ( ModuleSymbolKey ) } failed)", out failureReason ) ;
0 commit comments