Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to try to use Plinq in all Linq queries?

I read that PLinq will automatically use non parallel Linq if it finds PLinq to be more expensive. So I figured then why not use PLinq for everything (when possible) and let the runtime decide which one to use.

The apps will be deployed to multicore servers and I am OK to develop a little more code to deal with parallelism.

What are the pitfalls of using plinq as a default?

like image 817
Tony_Henrich Avatar asked May 23 '10 21:05

Tony_Henrich


People also ask

On which datasources do LINQ queries work?

In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, . NET collections, and any other format for which a LINQ provider is available.

How many ways can you write LINQ queries?

LINQ provides you three different ways to write a LINQ query in C# or VB.

Which extension method do you need to run a parallel query in Plinq?

If you specify Default as a parameter to the WithExecutionMode extension method, PLINQ will execute the query in parallel if an improvement in performance is evident in executing the query in parallel. If not, PLINQ would execute the query just like a LINQ query.

What is AsParallel in LINQ C#?

AsParallel(IEnumerable) Enables parallelization of a query. AsParallel<TSource>(Partitioner<TSource>) Enables parallelization of a query, as sourced by a custom partitioner that is responsible for splitting the input sequence into partitions.


1 Answers

One pit fall is you lose the ability to leverage ordering in sets.

Take the following code:

var results = new int { 0 ,1 ,2 ,3 };
var doSomethingSpecial = (from r in results.AsParallel() select r / 2).ToArray();

You can't count on the results coming in order so the result could be any permutations of the set. This is one of the largest pitfalls, in the sense if you are dealing with ordered data, then you could be losing the performance benefits due of the cost of sorting.

Another issue is you lose the ability to catch known exceptions. So i couldn't catch a null pointer exception (not saying you should ever do that) or even catch a FormatException.

There are a tons of reasonse why you should not always use Plinq in all cases, and i will highlight just one more. Don't read too uch into the "automatic use of non parallel Linq", it can only handle the barrier cases where the query is to simple, or would be too complex to run parallel.

Always keep in mind that the more use PLINQ the more resources you will be consuming on the server, which are taking away from other running threads.

Resources:

MSDN PLNQ white paper

Paul Kimmel on PLINQ

like image 97
Nix Avatar answered Oct 12 '22 13:10

Nix