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

[Release/3.1] ReplacingEV uses two arrays instead of dictionary (#19858) #19867

Merged
merged 3 commits into from
Feb 14, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions src/EFCore/Query/ReplacingExpressionVisitor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
Expand All @@ -14,20 +15,41 @@ public class ReplacingExpressionVisitor : ExpressionVisitor
private readonly Expression[] _originals;
private readonly Expression[] _replacements;

private readonly IDictionary<Expression, Expression> _quirkReplacements;

public static Expression Replace(Expression original, Expression replacement, Expression tree)
{
return new ReplacingExpressionVisitor(new[] { original }, new[] { replacement }).Visit(tree);
}

public ReplacingExpressionVisitor(Expression[] originals, Expression[] replacements)
{
_originals = originals;
_replacements = replacements;
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue19737", out var isEnabled) && isEnabled)
roji marked this conversation as resolved.
Show resolved Hide resolved
{
_quirkReplacements = new Dictionary<Expression, Expression>();
for (var i = 0; i < originals.Length; i++)
{
_quirkReplacements[originals[i]] = replacements[i];
}
}
else
{
_originals = originals;
_replacements = replacements;
}
}

public ReplacingExpressionVisitor(IDictionary<Expression, Expression> replacements)
: this(replacements.Keys.ToArray(), replacements.Values.ToArray())
{
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue19737", out var isEnabled) && isEnabled)
{
_quirkReplacements = replacements;
}
else
{
_originals = replacements.Keys.ToArray();
_replacements = replacements.Values.ToArray();
}
}

public override Expression Visit(Expression expression)
Expand All @@ -37,13 +59,23 @@ public override Expression Visit(Expression expression)
return expression;
}

// We use two arrays rather than a dictionary because hash calculation here can be prohibitively expensive
// for deep trees. Locality of reference makes arrays better for the small number of replacements anyway.
for (var i = 0; i < _originals.Length; i++)
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue19737", out var isEnabled) && isEnabled)
{
if (_quirkReplacements.TryGetValue(expression, out var replacement))
{
return replacement;
}
}
else
{
if (expression.Equals(_originals[i]))
// We use two arrays rather than a dictionary because hash calculation here can be prohibitively expensive
// for deep trees. Locality of reference makes arrays better for the small number of replacements anyway.
for (var i = 0; i < _originals.Length; i++)
{
return _replacements[i];
if (expression.Equals(_originals[i]))
{
return _replacements[i];
}
}
}

Expand Down