[PLinq] Add the plumbery for checking source data size when possible and setting...
authorJérémie Laval <jeremie.laval@gmail.com>
Thu, 2 Sep 2010 09:21:25 +0000 (10:21 +0100)
committerJérémie Laval <jeremie.laval@gmail.com>
Thu, 2 Sep 2010 09:35:45 +0000 (10:35 +0100)
mcs/class/System.Core/System.Linq.Parallel.QueryNodes/QueryStartNode.cs
mcs/class/System.Core/System.Linq.Parallel/QueryCheckerVisitor.cs

index db835b67bbf59d7a1b6b1a7109e95cd72a3b8003..fe68467e7a7a7679825d6dabde907ddb23c36beb 100644 (file)
@@ -55,6 +55,19 @@ namespace System.Linq.Parallel.QueryNodes
                        this.customPartitioner = custom;
                }
 
+               // If possible, this property will return the number of element the query
+               // is going to process. If that number if pretty low, executing the query
+               // sequentially is better
+               internal int Count {
+                       get {
+                               if (source == null)
+                                       return -1;
+
+                               ICollection coll = source as ICollection;
+                               return coll == null ? -1 : coll.Count;
+                       }
+               }
+
                public override void Visit (INodeVisitor visitor)
                {
                        visitor.Visit<T> (this);
index 546228e8d9d1ac2bde9289fec53c29c176fa4a99..ae175fa971c23532ed45f68007012e8cd6393bd7 100644 (file)
@@ -35,6 +35,8 @@ namespace System.Linq.Parallel
 
        internal class QueryCheckerVisitor : INodeVisitor
        {
+               const int minSequentialThreshold = 20;
+
                // Information gathering
                ParallelMergeOptions? options = null;
                ParallelExecutionMode? mode = null;
@@ -75,6 +77,10 @@ namespace System.Linq.Parallel
                                behindOrderGuard = false;
                        if (degreeOfParallelism != null)
                                partitionCount = degreeOfParallelism.Value;
+
+                       int count;
+                       if ((count = node.Count) != -1 && count < minSequentialThreshold)
+                               ShouldBeSequential = true;
                }
 
                public void Visit<T, TParent> (QueryStreamNode<T, TParent> node)