Skip to content

Fix #2319 caching of constant Field expressions #2321

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 1 commit into from
Oct 20, 2016
Merged

Fix #2319 caching of constant Field expressions #2321

merged 1 commit into from
Oct 20, 2016

Conversation

gmarz
Copy link
Contributor

@gmarz gmarz commented Oct 18, 2016

We only cache Field expressions that do not contain variables. For
instance:

  Field<Project>(p => p.Name)
  Field<Project>(p => p.Name.Suffix("foo"))
  Field<Project>(p => p.Metadata["foo"])

are all cachable since they only contain constant expressions.

whereas

  Field<Project>(p => p.Name.Suffix(myVariable))
  Field<Project>(p => p.Metadata[myVariable])

are not cachable since we can't evaluate the value of myVariable to be
used as a cache key.

However, there was a bug which this commit fixes where we would cache a
variable expression that ended with a constant. For instance:

  Field<Project>(p => p.Metadata[myVariable].Suffix("foo"))

This expression was incorrectly cached because HasConstantExpressionVisitor
only concerned itself with the last method call: Suffix("foo").

With this change, HasConstantExpressionVisitor has been renamed to
HasVariableExpressionVisitor to better align with what it actually
does. And now accounts for every node in the expression, ensuring that
we only cache expressions that contain only constants.

Copy link
Member

@Mpdreamz Mpdreamz left a comment

Choose a reason for hiding this comment

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

Small nitpick otherwise LGTM 👍

@@ -14,7 +14,9 @@ namespace Nest
{
internal class HasConstantExpressionVisitor : ExpressionVisitor
{
public bool Found { get; private set; }
public bool Found => this.FoundConstants.Any(b => b);
Copy link
Member

Choose a reason for hiding this comment

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

.Count > 0 since its an ICollection

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're checking for any true values within the collection, so Count > 0 isn't enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unless we only conditionally add to the collection...

Copy link
Member

Choose a reason for hiding this comment

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

You're right, i wonder if we can just make it a bool flag that once set is never unset? Would safe on allocs too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, good call. No need to maintain a collection at all, as long as we still visit each method call then we can use just the Found flag. I've changed this with my last commit.

@Mpdreamz
Copy link
Member

Lgtm 👍

@russcam
Copy link
Contributor

russcam commented Oct 18, 2016

LGTM 👍

@brandondahler
Copy link

Shouldn't the expression visitor be renamed HasNonConstantExpressionVisitor?

@@ -35,7 +41,7 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
var lastArg = node.Arguments.Last();
var constantExpression = lastArg as ConstantExpression;
this.Found = constantExpression == null;
return node;

Choose a reason for hiding this comment

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

This can be left in to prevent traversing inner nodes unnecessarily.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so. We need to actually visit all of the inner nodes to ensure there are only constants in the expression.

@@ -50,16 +56,9 @@ protected override Expression VisitMethodCall(MethodCallExpression node)
var lastArg = node.Arguments.Last();
var constantExpression = lastArg as ConstantExpression;
this.Found = constantExpression == null;
return node;

Choose a reason for hiding this comment

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

This can be left in to prevent traversing inner nodes unnecessarily.

We only cache Field expressions that do not contain variables.  For
instance:

  Field<Project>(p => p.Name)
  Field<Project>(p => p.Name.Suffix("foo"))
  Field<Project>(p => p.Metadata["foo"])

are all cachable since they only contain constant expressions.

whereas

  Field<Project>(p => p.Name.Suffix(myVariable))
  Field<Project>(p => p.Metadata[myVariable])

are not cachable since we can't evaluate the value of `myVariable` to be
used as a cache key.

However, there was a bug which this commit fixes where we would cache a
variable expression that ended with a constant.  For instance:

  Field<Project>(p => p.Metadata[myVariable].Suffix("foo"))

This expression was incorrectly cached because HasConstantExpressionVisitor
only concerned itself with the last method call: `Suffix("foo")`.

With this change, HasConstantExpressionVisitor has been renamed to
HasVariableExpressionVisitor to better align with what it actually
does.  And now accounts for every node in the expression, ensuring that
we only cache expressions that contain only constants.
@gmarz
Copy link
Contributor Author

gmarz commented Oct 20, 2016

Shouldn't the expression visitor be renamed HasNonConstantExpressionVisitor?

@brandondahler Agreed, it was confusing, I've renamed it to HasVariableExpressionVisitor.

@gmarz gmarz merged commit 515f212 into 2.x Oct 20, 2016
@gmarz gmarz deleted the fix/2319-2.x branch October 20, 2016 17:44
gmarz added a commit that referenced this pull request Oct 20, 2016
gmarz added a commit that referenced this pull request Oct 20, 2016
@gmarz
Copy link
Contributor Author

gmarz commented Oct 20, 2016

Ported to 5.x and master.

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.

4 participants