dotnet/roslyn

SyntaxToken.GetNextToken is very slow for long Child/Token lists - which causes SyntaxNormalizer to be slow as well

Open

#72,861 opened on 2024年4月3日

GitHub で見る
 (1 comment) (0 reactions) (0 assignees)C# (4,257 forks)batch import
Area-CompilersBughelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (平均マージ 6d 17h) (30d で 256 merged PRs)

説明

Version Used:

Steps to Reproduce: Parse the following code with a long array initializer - and Normalize whitespace.

Expected Behavior: It should terminate in a reasonable time.

Actual Behavior: Computes like forever


The performance hit seems to be caused mainly by SyntaxNavigator.GetNextToken which is called by SyntaxNormalizer.GetNextRelevantToken. To skip the previous tokens it simply iterates over all childs and tokens until it finds itself. For small token lists this is fine - but for long lists this algorithm is very bad. Couldn't we do for example something like binary search based on the token positon / span?

In addition: To iterate over the nodes it calls SyntaxNode.ChildNodesAndTokens() - which needs to compute the count all the time.

Close to that function is ChildThatContainsPosition() which seems to be very much faster - perhaps we could use that function ...

コントリビューターガイド