title: ToList eating massive amount of CPU time
body: <p><strong>edit:</strong> I tracked down the cause. Later on in the code I had made a mistake which caused non-colliding records to be merged into the main data store (t in the below code) for every record it didn't collide with, which is basically every record except for the one it does collide with, so after a few thousand compares, the master list would grow exponentially and would be several trillion records instead of a few hundred.</p>
  
  <p>I am attempting to sort, search and merge a large amount of 3d datapoints that are in 300-400 sets. I've run into a issue with a single linq line which seems to be eating 90-95% of my processing time. under about 3,000 sets, the processing time is virtually non-existent, but the moment I hit 3,000 the entire process virtually stops.</p>
  
  <p>dottrace is telling me that all of my processing time is dedicated calling and waiting on .tolist(), but I haven't been able to find a linq alternative that is more efficient. (I can always go back to the c++ implementation I had for this exact problem, but I was hoping linq might have a more efficient solution than what I can reasonably come up with.)</p>
  
  <p>linq in question is</p>
  
  <pre><code>List&lt;Point&gt; collisions = t.Points.Intersect(streamline.Points, new PointsComparer()).ToList();
  </code></pre>
  
  <p>the intent of this query is to take all the points in t and all the points in streamline, compare them, and generate a list of all the duplicate points.</p>
  
  <p>it may be worth noting that t.Points and streamline.Points have been presorted, so I could run a binary search on them, but I'm not sure how one would do that with linq.</p>
  
  <p>would greatly appreciate if anyone can explain what I'm missing here that's causing it to attempt to generate so many lists.</p>
  
  <p>for good measure, here are the point and PointsComparer Classes. (both  t.points and streamline.Points are list)</p>
  
  <pre><code>public class PointsComparer : IEqualityComparer&lt;Point&gt; {
      public bool Equals(Point x, Point y) {
          if (object.ReferenceEquals(x,y)) return true;
          return x.X.Equals(y.X) &amp;&amp; Equals(x.Y, y.Y) &amp;&amp; Equals(x.Z, y.Z);
      }
  
      public int GetHashCode(Point obj) {
          int xhash = obj.X.GetHashCode();
          int yhash = obj.Y.GetHashCode();
          int zhash = obj.Z.GetHashCode();
  
          return xhash ^ yhash ^ zhash;
  
      }
  }
  public class Point {
      public float X;
      public float Y;
      public float Z;
      public float[] Scalars = {};
      public int OriginalIndex;
  }
  </code></pre>