On Optimize performance decreases drastically as the number of conditions increases
Hi,
I am using Delegate Decompiler for many years in our projects and it's of great help. Recently I found a performance issue when running a query with many conditions.
From odata we run query's with a filter by id's that gets translated to EF. filter=(Id eq d6e5324b-b81a-4715-b8d8-7521a14a82d9) or ((Id eq 6c14fa7b-832b-4c2b-b7b1-d18cabcd0bac)
I tried to create a test that mimics the situation. If you increase the loop count the performance of the optimize step decreases drastically.
[Test]
public void TestEveryConditionMutipliesDurationInOptimizer()
{
using var env = new MethodEnvironment(classEnv);
System.Linq.Expressions.Expression<Func<EfItems.Concretes.EfParent, bool>> conditon = x => x.EfParentId == 0;
var predicate = LinqKit.PredicateBuilder.New<EfParent>();
for (int i = 0; i < 25; i++)
{
var id = i;
predicate = predicate.Or(x => x.EfParentId == id);
}
var expr = env.Db.EfParents.Where(predicate).Expression;
var compiled = expr.Decompile();
var optimized = compiled.Optimize();
}
When debugging I think something goes wrong in the method VisitBinary of class OptimizeExpressionVisitor. When calling into the base method you end up in some kind of loop. I tried to find a solution (commented code) but here is where my knowledge stops :)
protected override Expression VisitBinary(BinaryExpression node)
{
......
//return node.Update(left, VisitAndConvert(node.Conversion, "VisitBinary"), right);
return base.VisitBinary(node);
}
Is the Optimize step necessary because EF also caches the query? Do you have any idea what's going on.
Thanks in advance
@boosterch this sounds like the same issue I reported when migrating from 0.31.0 to 0.32.0, which is still open.
Is the Optimize step necessary because EF also caches the query?
This is needed because EF does not understand expressions if they are simply converted.
Possibly solved by #248. Included in 0.34.1.