Sorting, Filtering, Grouping and Aggregating on all CPU cores using RadGridView for WPF and PLINQ

by XAML Team | Comments 6

As some of you may already know the Q2 release of RadGridView for WPF ships with a brand new “data engine” entirely based on LINQ. This means that all data operations including sorting, filtering, grouping, aggregating and paging will be pushed down to the IQueryProvider when RadGridView is bound to an IQueryable. For example if you are using LinqToSql, LinqToEntities or OpenAccess ORM all gird operations are executed on the SQL server using the magic of LINQ providers.

Staying on the cutting edge I decide to try out PLINQ (Parallel LINQ) and enable RadGridView to execute its data operations on all available CPU cores. PLINQ enables LINQ queries to be execute simultaneously on all cores using the new task parallel library that will be available in .NET 4. PLINQ is straight forward to use when you are constructing your queries in code, but this is not the case when you want to enable IQueryable over IParallelEnumerable. Unfortunately PLINQ did not provide built-in query provider, so I ended up implementing ParallelQuery class similar to EnumerableQuery for LINQ to Objects. This class internally rewrites the generated Expression Tree and proxies all calls to the ParallelEnumerable extension methods.

On the following screenshots you can see the CPU utilization before and after the PLINQ is used.

Before:

LINQ

After:

PLINQ

As you can see when PLINQ is taking place both cores are fully utilized. You can imagine what will happen when you have 4 or 8 cores. And all of this for free with a simple one line change you have to do before binding the RadGridView.

[Update: If you are wondering why the CPU is utilized at its maximum and the occupied RAM is almost 3GB you should know that these screenshots are taken when the RadGridView is bound to collection with 15,000,000 items. Yes you heard me right this is 15 million.]

You can download the sample source here

P.S. Did I mention that this “data engine” will going to used in our ASP.NET MVC Grid.

Posted in: wpf linq

6 Comments

Kevin
Would this apply to the Silverlight GridView as well?
Stefan Dobrev
@kevin Unfortunately PLINQ is currently not available in Silverlight. Maybe Silverlight 4 will have it built in? You can vote for it here: http://silverlight.uservoice.com/pages/4325-feature-suggestions/suggestions/310712-plinq-and-tpl
Antonio
I have tried to download this sample a few times and everytime I try to unzip it I get errors saying that the file is corrupt ?
Stefan Dobrev
@Antonio I have just downloaded and extracted the example successfully on my PC. I'm using 7zip.
Igor Ostrovsky
Stefan, why does RadGridView use IQueryable rather than IEnumerable as an interface for LINQ queries? Since IQueryable implements IEnumerable, IEnumerable would seem to be the best "common denominator" between all LINQ providers.

Or is there something about your particular use case that forces you to use IQueryable?

Thanks,
Igor Ostrovsky
PS.: I'm a developer on PLINQ.
Stefan Dobrev
Hi Igor,

We are requiring IEnumerable in order to bind RadGridView. The grid internally transform this IEnumerable to an IQueryable and executes all its data operations via LINQ Expressions back in the query provider. In the case when you pass an IQueryable the grid will not do any transformation on the data source and create the expressions directly using the passed query provider.

Or is there something about your particular use case that forces you to use IQueryable?
Yes, In the example I need to pass an IQueryable with a query provider that can translate the LINQ expression trees (that the grid generates) to the correct calls on the ParallelEnumerable class, so that the query can be executed in parallel.

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by XAML Team - or - read latest articles in Developer Tools