diff --git a/src/Compilers/Core/Portable/DiagnosticAnalyzer/SymbolDeclaredCompilationEvent.cs b/src/Compilers/Core/Portable/DiagnosticAnalyzer/SymbolDeclaredCompilationEvent.cs index f03018973710f..381f4f62f51ff 100644 --- a/src/Compilers/Core/Portable/DiagnosticAnalyzer/SymbolDeclaredCompilationEvent.cs +++ b/src/Compilers/Core/Portable/DiagnosticAnalyzer/SymbolDeclaredCompilationEvent.cs @@ -2,9 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Collections.Immutable; using Microsoft.CodeAnalysis.Symbols; +using Roslyn.Utilities; namespace Microsoft.CodeAnalysis.Diagnostics { @@ -15,7 +15,7 @@ namespace Microsoft.CodeAnalysis.Diagnostics /// internal sealed class SymbolDeclaredCompilationEvent : CompilationEvent { - private readonly Lazy> _lazyCachedDeclaringReferences; + private ImmutableArray _lazyCachedDeclaringReferences; public SymbolDeclaredCompilationEvent( Compilation compilation, @@ -25,7 +25,7 @@ public SymbolDeclaredCompilationEvent( { SymbolInternal = symbolInternal; SemanticModelWithCachedBoundNodes = semanticModelWithCachedBoundNodes; - _lazyCachedDeclaringReferences = new Lazy>(() => Symbol.DeclaringSyntaxReferences); + _lazyCachedDeclaringReferences = default(ImmutableArray); } public ISymbol Symbol => SymbolInternal.GetISymbol(); @@ -35,7 +35,16 @@ public SymbolDeclaredCompilationEvent( public SemanticModel? SemanticModelWithCachedBoundNodes { get; } // PERF: We avoid allocations in re-computing syntax references for declared symbol during event processing by caching them directly on this member. - public ImmutableArray DeclaringSyntaxReferences => _lazyCachedDeclaringReferences.Value; + public ImmutableArray DeclaringSyntaxReferences + { + get + { + return InterlockedOperations.Initialize( + ref _lazyCachedDeclaringReferences, + static self => self.Symbol.DeclaringSyntaxReferences, + this); + } + } public override string ToString() {