Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
Cache 'base.Visit*' delegates (#64)
Browse files Browse the repository at this point in the history
* Cache delegates in RazorHtmlWriter

* Cache delegates in ClassifiedSpanVisitor
  • Loading branch information
sharwell authored Feb 1, 2022
1 parent 82e91d8 commit eba27a3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -15,8 +13,21 @@ internal class ClassifiedSpanVisitor : SyntaxWalker
{
private readonly RazorSourceDocument _source;
private readonly List<ClassifiedSpanInternal> _spans;

private readonly Action<CSharpCodeBlockSyntax> _baseVisitCSharpCodeBlock;
private readonly Action<CSharpStatementSyntax> _baseVisitCSharpStatement;
private readonly Action<CSharpExplicitExpressionSyntax> _baseVisitCSharpExplicitExpression;
private readonly Action<CSharpImplicitExpressionSyntax> _baseVisitCSharpImplicitExpression;
private readonly Action<RazorDirectiveSyntax> _baseVisitRazorDirective;
private readonly Action<CSharpTemplateBlockSyntax> _baseVisitCSharpTemplateBlock;
private readonly Action<MarkupBlockSyntax> _baseVisitMarkupBlock;
private readonly Action<MarkupTagHelperAttributeValueSyntax> _baseVisitMarkupTagHelperAttributeValue;
private readonly Action<MarkupTagHelperElementSyntax> _baseVisitMarkupTagHelperElement;
private readonly Action<MarkupCommentBlockSyntax> _baseVisitMarkupCommentBlock;
private readonly Action<MarkupDynamicAttributeValueSyntax> _baseVisitMarkupDynamicAttributeValue;

private BlockKindInternal _currentBlockKind;
private SyntaxNode _currentBlock;
private SyntaxNode? _currentBlock;

public ClassifiedSpanVisitor(RazorSourceDocument source)
{
Expand All @@ -27,6 +38,19 @@ public ClassifiedSpanVisitor(RazorSourceDocument source)

_source = source;
_spans = new List<ClassifiedSpanInternal>();

_baseVisitCSharpCodeBlock = base.VisitCSharpCodeBlock;
_baseVisitCSharpStatement = base.VisitCSharpStatement;
_baseVisitCSharpExplicitExpression = base.VisitCSharpExplicitExpression;
_baseVisitCSharpImplicitExpression = base.VisitCSharpImplicitExpression;
_baseVisitRazorDirective = base.VisitRazorDirective;
_baseVisitCSharpTemplateBlock = base.VisitCSharpTemplateBlock;
_baseVisitMarkupBlock = base.VisitMarkupBlock;
_baseVisitMarkupTagHelperAttributeValue = base.VisitMarkupTagHelperAttributeValue;
_baseVisitMarkupTagHelperElement = base.VisitMarkupTagHelperElement;
_baseVisitMarkupCommentBlock = base.VisitMarkupCommentBlock;
_baseVisitMarkupDynamicAttributeValue = base.VisitMarkupDynamicAttributeValue;

_currentBlockKind = BlockKindInternal.Markup;
}

Expand Down Expand Up @@ -66,37 +90,37 @@ node.Parent is RazorDirectiveBodySyntax ||
return;
}

WriteBlock(node, BlockKindInternal.Statement, base.VisitCSharpCodeBlock);
WriteBlock(node, BlockKindInternal.Statement, _baseVisitCSharpCodeBlock);
}

public override void VisitCSharpStatement(CSharpStatementSyntax node)
{
WriteBlock(node, BlockKindInternal.Statement, base.VisitCSharpStatement);
WriteBlock(node, BlockKindInternal.Statement, _baseVisitCSharpStatement);
}

public override void VisitCSharpExplicitExpression(CSharpExplicitExpressionSyntax node)
{
WriteBlock(node, BlockKindInternal.Expression, base.VisitCSharpExplicitExpression);
WriteBlock(node, BlockKindInternal.Expression, _baseVisitCSharpExplicitExpression);
}

public override void VisitCSharpImplicitExpression(CSharpImplicitExpressionSyntax node)
{
WriteBlock(node, BlockKindInternal.Expression, base.VisitCSharpImplicitExpression);
WriteBlock(node, BlockKindInternal.Expression, _baseVisitCSharpImplicitExpression);
}

public override void VisitRazorDirective(RazorDirectiveSyntax node)
{
WriteBlock(node, BlockKindInternal.Directive, base.VisitRazorDirective);
WriteBlock(node, BlockKindInternal.Directive, _baseVisitRazorDirective);
}

public override void VisitCSharpTemplateBlock(CSharpTemplateBlockSyntax node)
{
WriteBlock(node, BlockKindInternal.Template, base.VisitCSharpTemplateBlock);
WriteBlock(node, BlockKindInternal.Template, _baseVisitCSharpTemplateBlock);
}

public override void VisitMarkupBlock(MarkupBlockSyntax node)
{
WriteBlock(node, BlockKindInternal.Markup, base.VisitMarkupBlock);
WriteBlock(node, BlockKindInternal.Markup, _baseVisitMarkupBlock);
}

public override void VisitMarkupTagHelperAttributeValue(MarkupTagHelperAttributeValueSyntax node)
Expand All @@ -107,7 +131,7 @@ public override void VisitMarkupTagHelperAttributeValue(MarkupTagHelperAttribute
if (node.Children.Count > 1 ||
(node.Children.Count == 1 && node.Children[0] is MarkupDynamicAttributeValueSyntax))
{
WriteBlock(node, BlockKindInternal.Markup, base.VisitMarkupTagHelperAttributeValue);
WriteBlock(node, BlockKindInternal.Markup, _baseVisitMarkupTagHelperAttributeValue);
return;
}

Expand Down Expand Up @@ -140,7 +164,7 @@ public override void VisitMarkupEndTag(MarkupEndTagSyntax node)

public override void VisitMarkupTagHelperElement(MarkupTagHelperElementSyntax node)
{
WriteBlock(node, BlockKindInternal.Tag, base.VisitMarkupTagHelperElement);
WriteBlock(node, BlockKindInternal.Tag, _baseVisitMarkupTagHelperElement);
}

public override void VisitMarkupTagHelperStartTag(MarkupTagHelperStartTagSyntax node)
Expand Down Expand Up @@ -202,12 +226,12 @@ public override void VisitMarkupMinimizedAttributeBlock(MarkupMinimizedAttribute

public override void VisitMarkupCommentBlock(MarkupCommentBlockSyntax node)
{
WriteBlock(node, BlockKindInternal.HtmlComment, base.VisitMarkupCommentBlock);
WriteBlock(node, BlockKindInternal.HtmlComment, _baseVisitMarkupCommentBlock);
}

public override void VisitMarkupDynamicAttributeValue(MarkupDynamicAttributeValueSyntax node)
{
WriteBlock(node, BlockKindInternal.Markup, base.VisitMarkupDynamicAttributeValue);
WriteBlock(node, BlockKindInternal.Markup, _baseVisitMarkupDynamicAttributeValue);
}

public override void VisitRazorMetaCode(RazorMetaCodeSyntax node)
Expand Down Expand Up @@ -333,7 +357,7 @@ private static SyntaxList<RazorSyntaxNode> GetRewrittenMarkupStartTagChildren(Ma
return new SyntaxList<RazorSyntaxNode>(builder.ToListNode().CreateRed(node, node.Position));
}

SpanContext latestSpanContext = null;
SpanContext? latestSpanContext = null;
var children = node.Children;
var newChildren = new SyntaxListBuilder(children.Count);
var literals = new List<MarkupTextLiteralSyntax>();
Expand Down
62 changes: 45 additions & 17 deletions src/Microsoft.AspNetCore.Razor.Language/src/RazorHtmlWriter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System;
using System.Diagnostics;
using System.Text;
Expand All @@ -16,6 +14,21 @@ namespace Microsoft.AspNetCore.Razor.Language;
// not all characters in the document are included in the ClassifiedSpans.
internal class RazorHtmlWriter : SyntaxWalker
{
private readonly Action<RazorCommentBlockSyntax> _baseVisitRazorCommentBlock;
private readonly Action<RazorMetaCodeSyntax> _baseVisitRazorMetaCode;
private readonly Action<MarkupTransitionSyntax> _baseVisitMarkupTransition;
private readonly Action<CSharpTransitionSyntax> _baseVisitCSharpTransition;
private readonly Action<CSharpEphemeralTextLiteralSyntax> _baseVisitCSharpEphemeralTextLiteral;
private readonly Action<CSharpExpressionLiteralSyntax> _baseVisitCSharpExpressionLiteral;
private readonly Action<CSharpStatementLiteralSyntax> _baseVisitCSharpStatementLiteral;
private readonly Action<MarkupStartTagSyntax> _baseVisitMarkupStartTag;
private readonly Action<MarkupEndTagSyntax> _baseVisitMarkupEndTag;
private readonly Action<MarkupTagHelperStartTagSyntax> _baseVisitMarkupTagHelperStartTag;
private readonly Action<MarkupTagHelperEndTagSyntax> _baseVisitMarkupTagHelperEndTag;
private readonly Action<MarkupEphemeralTextLiteralSyntax> _baseVisitMarkupEphemeralTextLiteral;
private readonly Action<MarkupTextLiteralSyntax> _baseVisitMarkupTextLiteral;
private readonly Action<UnclassifiedTextLiteralSyntax> _baseVisitUnclassifiedTextLiteral;

private bool _isHtml;

private RazorHtmlWriter(RazorSourceDocument source)
Expand All @@ -28,13 +41,28 @@ private RazorHtmlWriter(RazorSourceDocument source)
Source = source;
Builder = new StringBuilder(Source.Length);
_isHtml = true;

_baseVisitRazorCommentBlock = base.VisitRazorCommentBlock;
_baseVisitRazorMetaCode = base.VisitRazorMetaCode;
_baseVisitMarkupTransition = base.VisitMarkupTransition;
_baseVisitCSharpTransition = base.VisitCSharpTransition;
_baseVisitCSharpEphemeralTextLiteral = base.VisitCSharpEphemeralTextLiteral;
_baseVisitCSharpExpressionLiteral = base.VisitCSharpExpressionLiteral;
_baseVisitCSharpStatementLiteral = base.VisitCSharpStatementLiteral;
_baseVisitMarkupStartTag = base.VisitMarkupStartTag;
_baseVisitMarkupEndTag = base.VisitMarkupEndTag;
_baseVisitMarkupTagHelperStartTag = base.VisitMarkupTagHelperStartTag;
_baseVisitMarkupTagHelperEndTag = base.VisitMarkupTagHelperEndTag;
_baseVisitMarkupEphemeralTextLiteral = base.VisitMarkupEphemeralTextLiteral;
_baseVisitMarkupTextLiteral = base.VisitMarkupTextLiteral;
_baseVisitUnclassifiedTextLiteral = base.VisitUnclassifiedTextLiteral;
}

public RazorSourceDocument Source { get; }

public StringBuilder Builder { get; }

public static RazorHtmlDocument GetHtmlDocument(RazorCodeDocument codeDocument)
public static RazorHtmlDocument? GetHtmlDocument(RazorCodeDocument codeDocument)
{
var options = codeDocument.GetCodeGenerationOptions();
if (options == null || !options.DesignTime)
Expand All @@ -59,72 +87,72 @@ public static RazorHtmlDocument GetHtmlDocument(RazorCodeDocument codeDocument)

public override void VisitRazorCommentBlock(RazorCommentBlockSyntax node)
{
WriteNode(node, isHtml: false, base.VisitRazorCommentBlock);
WriteNode(node, isHtml: false, _baseVisitRazorCommentBlock);
}

public override void VisitRazorMetaCode(RazorMetaCodeSyntax node)
{
WriteNode(node, isHtml: false, base.VisitRazorMetaCode);
WriteNode(node, isHtml: false, _baseVisitRazorMetaCode);
}

public override void VisitMarkupTransition(MarkupTransitionSyntax node)
{
WriteNode(node, isHtml: false, base.VisitMarkupTransition);
WriteNode(node, isHtml: false, _baseVisitMarkupTransition);
}

public override void VisitCSharpTransition(CSharpTransitionSyntax node)
{
WriteNode(node, isHtml: false, base.VisitCSharpTransition);
WriteNode(node, isHtml: false, _baseVisitCSharpTransition);
}

public override void VisitCSharpEphemeralTextLiteral(CSharpEphemeralTextLiteralSyntax node)
{
WriteNode(node, isHtml: false, base.VisitCSharpEphemeralTextLiteral);
WriteNode(node, isHtml: false, _baseVisitCSharpEphemeralTextLiteral);
}

public override void VisitCSharpExpressionLiteral(CSharpExpressionLiteralSyntax node)
{
WriteNode(node, isHtml: false, base.VisitCSharpExpressionLiteral);
WriteNode(node, isHtml: false, _baseVisitCSharpExpressionLiteral);
}

public override void VisitCSharpStatementLiteral(CSharpStatementLiteralSyntax node)
{
WriteNode(node, isHtml: false, base.VisitCSharpStatementLiteral);
WriteNode(node, isHtml: false, _baseVisitCSharpStatementLiteral);
}

public override void VisitMarkupStartTag(MarkupStartTagSyntax node)
{
WriteNode(node, isHtml: true, base.VisitMarkupStartTag);
WriteNode(node, isHtml: true, _baseVisitMarkupStartTag);
}

public override void VisitMarkupEndTag(MarkupEndTagSyntax node)
{
WriteNode(node, isHtml: true, base.VisitMarkupEndTag);
WriteNode(node, isHtml: true, _baseVisitMarkupEndTag);
}

public override void VisitMarkupTagHelperStartTag(MarkupTagHelperStartTagSyntax node)
{
WriteNode(node, isHtml: true, base.VisitMarkupTagHelperStartTag);
WriteNode(node, isHtml: true, _baseVisitMarkupTagHelperStartTag);
}

public override void VisitMarkupTagHelperEndTag(MarkupTagHelperEndTagSyntax node)
{
WriteNode(node, isHtml: true, base.VisitMarkupTagHelperEndTag);
WriteNode(node, isHtml: true, _baseVisitMarkupTagHelperEndTag);
}

public override void VisitMarkupEphemeralTextLiteral(MarkupEphemeralTextLiteralSyntax node)
{
WriteNode(node, isHtml: true, base.VisitMarkupEphemeralTextLiteral);
WriteNode(node, isHtml: true, _baseVisitMarkupEphemeralTextLiteral);
}

public override void VisitMarkupTextLiteral(MarkupTextLiteralSyntax node)
{
WriteNode(node, isHtml: true, base.VisitMarkupTextLiteral);
WriteNode(node, isHtml: true, _baseVisitMarkupTextLiteral);
}

public override void VisitUnclassifiedTextLiteral(UnclassifiedTextLiteralSyntax node)
{
WriteNode(node, isHtml: true, base.VisitUnclassifiedTextLiteral);
WriteNode(node, isHtml: true, _baseVisitUnclassifiedTextLiteral);
}

public override void VisitToken(SyntaxToken token)
Expand Down

0 comments on commit eba27a3

Please sign in to comment.