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

Avoid unnecessary visit* method calls #9105

Merged
merged 3 commits into from
Nov 29, 2022
Merged

Avoid unnecessary visit* method calls #9105

merged 3 commits into from
Nov 29, 2022

Conversation

shenglol
Copy link
Contributor

@shenglol shenglol commented Nov 23, 2022

Yet another quick and easy optimization. The current SyntaxVisitor is a concrete syntax tree (CST) / parse tree visitor which visits every single syntax tree node, but in most cases, this is not necessary. The PR splits SyntaxVisitor into an AstVisitor for traversing the AST (which doesn't include tokens such as keywords and brackets) and a CstVisitor for visiting the CST. Visitors that need to access all syntax nodes, such as DocumentBuildVisitor, ParseDiagnosticsVisitor, etc., are inherited from CstVisitor. Other visitors are inherited from AstVisitor.

Closes #7403.

Benchmarks

CstVisitor vs. AstVisitor

ast_vs_cst

Compilation

Before

Before

After

After

Microsoft Reviewers: Open in CodeFlow

using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Bicep.Core.Parsing;
using System.Text;
using System.Threading.Tasks;

namespace Bicep.Core.Syntax
{
public abstract class SyntaxVisitor : ISyntaxVisitor
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the changes to the base class, the ISyntaxVisitor interface may not be needed anymore. I'll send a follow up PR to replace references to ISyntaxVisitor with SyntaxVisitor.

namespace Bicep.Core.Syntax
{
/// <summary>
/// Visits an <see href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">abstract syntax tree (AST)</see>.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AST

Is it worth adding a comment somewhere that explains what our definition of AST is in terms of our classes?

@@ -48,7 +48,7 @@ public bool IsDescendant(SyntaxBase node, SyntaxBase potentialAncestor)
return false;
}

private sealed class ParentTrackingVisitor : SyntaxVisitor
private sealed class ParentTrackingVisitor : AstVisitor
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AstVisitor

The original intent of SyntaxHierarchy was to create parent pointers for all the nodes in the CST. Given that all the tests are passing with the change, we are clearly only calculating parents for AST nodes. Worth adding a comment somewhere to indicate that we only create the hierarchy for AST nodes?

Copy link
Member

@majastrz majastrz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some comments about comments.

Copy link
Member

@anthony-c-martin anthony-c-martin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, nice speedup!

@majastrz majastrz merged commit 6fdd5ae into main Nov 29, 2022
@majastrz majastrz deleted the shenglol/ast-visitor branch November 29, 2022 18:39
@shenglol shenglol mentioned this pull request Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make the syntax visitors more "abstract"
4 participants