Skip to content

Optimization: Dispatch Translator by virtual functions instead of switch statements #118

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

Merged
merged 13 commits into from
Jan 2, 2024
Prev Previous commit
Next Next commit
WhileSection
  • Loading branch information
SergeiPavlov committed Jan 2, 2024
commit 198c13b26bb3ef5735775d96f1e6546382422a58
13 changes: 3 additions & 10 deletions Orm/Xtensive.Orm/Sql/Compiler/SqlCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2034,11 +2034,11 @@ public virtual void Visit(SqlVariant node)
public virtual void Visit(SqlWhile node)
{
using (context.EnterScope(node)) {
AppendTranslated(node, WhileSection.Entry);
translator.WhileEntry(context);
node.Condition.AcceptVisitor(this);
AppendTranslated(node, WhileSection.Statement);
translator.WhileStatement(context);
node.Statement.AcceptVisitor(this);
AppendTranslated(node, WhileSection.Exit);
translator.WhileExit(context);
}
}

Expand Down Expand Up @@ -2717,13 +2717,6 @@ protected void AppendTranslated(SqlVariable node)
translator.Translate(context, node);
}

protected void AppendTranslated(SqlWhile node, WhileSection section)
{
AppendSpaceIfNecessary();
translator.Translate(context, node, section);
AppendSpaceIfNecessary();
}

protected void AppendTranslated(SqlExtract extract, ExtractSection section)
{
AppendSpaceIfNecessary();
Expand Down
7 changes: 0 additions & 7 deletions Orm/Xtensive.Orm/Sql/Compiler/SqlNodeSections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,6 @@ public enum UpdateSection
Limit = 5,
}

public enum WhileSection
{
Entry = 0,
Exit = 1,
Statement = 2,
}

public enum QueryExpressionSection
{
Entry = 0,
Expand Down
34 changes: 14 additions & 20 deletions Orm/Xtensive.Orm/Sql/Compiler/SqlTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1892,26 +1892,20 @@ public virtual void Translate(SqlCompilerContext context, SqlUserFunctionCall no
/// <param name="node">Expression to translate.</param>
public virtual void Translate(SqlCompilerContext context, SqlVariable node) => context.Output.Append("@").Append(node.Name);

/// <summary>
/// Translates <see cref="SqlWhile"/> statement and writes result to to <see cref="SqlCompilerContext.Output"/>.
/// </summary>
/// <param name="context">The compiler context.</param>
/// <param name="node">Statement to translate.</param>
/// <param name="section">Particular section to translate.</param>
public virtual void Translate(SqlCompilerContext context, SqlWhile node, WhileSection section)
{
switch (section) {
case WhileSection.Entry:
_ = context.Output.AppendOpeningPunctuation("WHILE (");
break;
case WhileSection.Statement:
_ = context.Output.AppendClosingPunctuation(") BEGIN");
break;
case WhileSection.Exit:
_ = context.Output.Append("END");
break;
}
}
public virtual void WhileEntry(SqlCompilerContext context) =>
context.Output.AppendSpaceIfNecessary()
.AppendOpeningPunctuation("WHILE (")
.AppendSpaceIfNecessary();

public virtual void WhileStatement(SqlCompilerContext context) =>
context.Output.AppendSpaceIfNecessary()
.AppendClosingPunctuation(") BEGIN")
.AppendSpaceIfNecessary();

public virtual void WhileExit(SqlCompilerContext context) =>
context.Output.AppendSpaceIfNecessary()
.AppendClosingPunctuation("END")
.AppendSpaceIfNecessary();

/// <summary>
/// Translates <see cref="SqlCommand"/> statement and writes result to to <see cref="SqlCompilerContext.Output"/>.
Expand Down