Skip to content

Commit

Permalink
SyntaxValueProvider: avoid performance issue with syntax list contain…
Browse files Browse the repository at this point in the history
…ing many items (#83483)
  • Loading branch information
cston committed Mar 21, 2023
1 parent 8e8ab0c commit 0b03ca6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,16 @@ void processMember(
// For any other node, just keep recursing deeper to see if we can find an attribute. Note: we cannot
// terminate the search anywhere as attributes may be found on things like local functions, and that
// means having to dive deep into statements and expressions.
foreach (var child in node.ChildNodesAndTokens().Reverse())
var childNodesAndTokens = node.ChildNodesAndTokens();

// Avoid performance issue in ChildSyntaxList when iterating the child list in reverse
// (see https://github.com/dotnet/roslyn/issues/66475) by iterating forward first to
// ensure child nodes are realized.
foreach (var childNode in childNodesAndTokens)
{
}

foreach (var child in childNodesAndTokens.Reverse())
{
if (child.IsNode)
nodeStack.Append(child.AsNode()!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,40 @@ partial class C
Assert.Empty(diagnostics);
}

[Fact]
public static void SyntaxListWithManyItems()
{
const int nItems = 200000;
var builder = new System.Text.StringBuilder();
builder.AppendLine(
"""
using Microsoft.Extensions.Logging;
class Program
{
[LoggerMessage(EventId = 1, Level = LogLevel.Debug, Message = "M1")]
static partial void M1(ILogger logger)
{
""");
builder.AppendLine(" int[] values = new[] { ");
for (int i = 0; i < nItems; i++)
{
builder.Append("0, ");
}
builder.AppendLine("};");
builder.AppendLine("}");
builder.AppendLine("}");

string source = builder.ToString();
Compilation compilation = CompilationHelper.CreateCompilation(source);
LoggerMessageGenerator generator = new LoggerMessageGenerator();

(ImmutableArray<Diagnostic> diagnostics, _) =
RoslynTestUtils.RunGenerator(compilation, generator);

Assert.Single(diagnostics);
Assert.Equal(DiagnosticDescriptors.LoggingMethodHasBody.Id, diagnostics[0].Id);
}

private static async Task<IReadOnlyList<Diagnostic>> RunGenerator(
string code,
bool wrap = true,
Expand Down

0 comments on commit 0b03ca6

Please sign in to comment.