LinqBindingList Performance - A Suggestion

LinqBindingList Performance - A Suggestion

Old forum URL: forums.lhotka.net/forums/t/7450.aspx


Woggly posted on Thursday, August 13, 2009

Hello!

We had some performance problems when we were editing a large amount of objects in a datagrid, which is bound to a LinqBindingList. By using a profiler we found out, that the method ItemShouldBeInList needs most of the time. We looked at it carefully and found out, that it would be enough for us to only check if the item fits the where-condition. We modified the method like shown below. What do you think? Will this cause any problems later on? We testet our app where the List is used and everything worked fine.

private bool ItemShouldBeInList(T item)
{
InnermostWhereFinder whereFinder = new InnermostWhereFinder();
MethodCallExpression whereExpression = whereFinder.GetInnermostWhere(_expression);
if (whereExpression == null) return false;
if (whereExpression.Arguments.Count if (whereExpression.Arguments[1] == null) return false;
Expression> whereBody = (Expression>)((UnaryExpression)(whereExpression.Arguments[1])).Operand;

List list = new List { item };

return list.AsQueryable().Count(whereBody) > 0;

//var searchable = _list as Linq.IIndexSearchable;
//if (searchable == null)
// return false;
//else
// return searchable.SearchByExpression(whereBody).Contains(item);
}

Thanks in advance!

RockfordLhotka replied on Thursday, August 13, 2009

I added this to the issue tracker, hopefully Aaron has time to look at it in the near future.

Thanks for the suggestion!

AaronErickson replied on Sunday, September 06, 2009

Woggly:
Hello!

We had some performance problems when we were editing a large amount of objects in a datagrid, which is bound to a LinqBindingList. By using a profiler we found out, that the method ItemShouldBeInList needs most of the time. We looked at it carefully and found out, that it would be enough for us to only check if the item fits the where-condition. We modified the method like shown below. What do you think? Will this cause any problems later on? We testet our app where the List is used and everything worked fine.

private bool ItemShouldBeInList(T item)
{
InnermostWhereFinder whereFinder = new InnermostWhereFinder();
MethodCallExpression whereExpression = whereFinder.GetInnermostWhere(_expression);
if (whereExpression == null) return false;
if (whereExpression.Arguments.Count if (whereExpression.Arguments[1] == null) return false;
Expression> whereBody = (Expression>)((UnaryExpression)(whereExpression.Arguments[1])).Operand;

List list = new List { item };

return list.AsQueryable().Count(whereBody) > 0;

//var searchable = _list as Linq.IIndexSearchable;
//if (searchable == null)
// return false;
//else
// return searchable.SearchByExpression(whereBody).Contains(item);
}

Thanks in advance!


Sorry for the late response... been on a project putting in 60 hour weeks for awhile, finally catching up on OSS.

I think your optimization will work for smaller cases, but may not work well for larger cases, where an index can be used. If searchable contains a large number of items, the index will be used through SearchByExpression, whereas AsQueryable will not search using the index.

This really gets exposed on types such as string, where equality of strings is not an "on the CPU" operation. For the general case, I fear this change will make some cases worse.

Copyright (c) Marimer LLC