Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocations in SymbolDeclaredCompilationEvent #74250

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -15,7 +15,7 @@ namespace Microsoft.CodeAnalysis.Diagnostics
/// </summary>
internal sealed class SymbolDeclaredCompilationEvent : CompilationEvent
{
private readonly Lazy<ImmutableArray<SyntaxReference>> _lazyCachedDeclaringReferences;
private ImmutableArray<SyntaxReference> _lazyCachedDeclaringReferences;

public SymbolDeclaredCompilationEvent(
Compilation compilation,
Expand All @@ -25,7 +25,7 @@ public SymbolDeclaredCompilationEvent(
{
SymbolInternal = symbolInternal;
SemanticModelWithCachedBoundNodes = semanticModelWithCachedBoundNodes;
_lazyCachedDeclaringReferences = new Lazy<ImmutableArray<SyntaxReference>>(() => Symbol.DeclaringSyntaxReferences);
_lazyCachedDeclaringReferences = default(ImmutableArray<SyntaxReference>);
}

public ISymbol Symbol => SymbolInternal.GetISymbol();
Expand All @@ -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<SyntaxReference> DeclaringSyntaxReferences => _lazyCachedDeclaringReferences.Value;
public ImmutableArray<SyntaxReference> DeclaringSyntaxReferences
{
get
{
return InterlockedOperations.Initialize(
ref _lazyCachedDeclaringReferences,
static self => self.Symbol.DeclaringSyntaxReferences,
this);
}
}

public override string ToString()
{
Expand Down
Loading