提交 63ffc585 编写于 作者: S Sam Harwell

Avoid allocations in IsDefinedInSourceTree hot paths

Fixes #23462
上级 407d764e
......@@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.CodeAnalysis.Text;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.CSharp.Symbols
......@@ -868,6 +869,30 @@ public override ImmutableArray<SyntaxReference> DeclaringSyntaxReferences
}
}
// This method behaves the same was as the base class, but avoids allocations associated with DeclaringSyntaxReferences
internal override bool IsDefinedInSourceTree(SyntaxTree tree, TextSpan? definedWithinSpan, CancellationToken cancellationToken)
{
var declarations = declaration.Declarations;
if (IsImplicitlyDeclared && declarations.IsEmpty)
{
return ContainingSymbol.IsDefinedInSourceTree(tree, definedWithinSpan, cancellationToken);
}
foreach (var declaration in declarations)
{
cancellationToken.ThrowIfCancellationRequested();
var syntaxRef = declaration.SyntaxReference;
if (syntaxRef.SyntaxTree == tree &&
(!definedWithinSpan.HasValue || syntaxRef.Span.IntersectsWith(definedWithinSpan.Value)))
{
return true;
}
}
return false;
}
#endregion
#region Members
......
......@@ -463,11 +463,12 @@ internal override bool IsDefinedInSourceTree(SyntaxTree tree, TextSpan? definedW
}
// Check if any namespace declaration block intersects with the given tree/span.
foreach (var syntaxRef in this.DeclaringSyntaxReferences)
foreach (var declaration in _mergedDeclaration.Declarations)
{
cancellationToken.ThrowIfCancellationRequested();
if (syntaxRef.SyntaxTree != tree)
var declarationSyntaxRef = declaration.SyntaxReference;
if (declarationSyntaxRef.SyntaxTree != tree)
{
continue;
}
......@@ -477,7 +478,7 @@ internal override bool IsDefinedInSourceTree(SyntaxTree tree, TextSpan? definedW
return true;
}
var syntax = syntaxRef.GetSyntax(cancellationToken);
var syntax = NamespaceDeclarationSyntaxReference.GetSyntax(declarationSyntaxRef, cancellationToken);
if (syntax.FullSpan.IntersectsWith(definedWithinSpan.Value))
{
return true;
......
......@@ -2,8 +2,8 @@
using System.Diagnostics;
using System.Threading;
using Microsoft.CodeAnalysis.Syntax;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Syntax;
namespace Microsoft.CodeAnalysis.CSharp
{
......@@ -19,6 +19,11 @@ public NamespaceDeclarationSyntaxReference(SyntaxReference reference)
}
protected override SyntaxNode Translate(SyntaxReference reference, CancellationToken cancellationToken)
{
return GetSyntax(reference, cancellationToken);
}
internal static SyntaxNode GetSyntax(SyntaxReference reference, CancellationToken cancellationToken)
{
var node = (CSharpSyntaxNode)reference.GetSyntax(cancellationToken);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册