{"id":104,"date":"2009-12-16T06:45:58","date_gmt":"2009-12-16T06:45:58","guid":{"rendered":"http:\/\/www.themissingdocs.net\/wordpress\/?p=104"},"modified":"2009-12-16T06:45:58","modified_gmt":"2009-12-16T06:45:58","slug":"linq-missed-optimization-and-the-fun-of-interfaces","status":"publish","type":"post","link":"https:\/\/www.themissingdocs.net\/?p=104","title":{"rendered":"LINQ missed optimization (and the fun of interfaces)"},"content":{"rendered":"<p>LINQ is a bit of an interesting beast, it makes certain things easier, but it also performs so much worse in many scenarios.<\/p>\n<p>I had assumed that LINQ internally would have a decent amount of optimizations, some of its operations will perform better if you know that the incoming collection is ICollection<T> and can thus get the count, for instance.  ToArray for instance will use the count property, and CopyTo from ICollection<T> to improve performance. (Although it then goes and copies the result array into another identical array&#8230;)<\/p>\n<p>However, say you have a List<int> and you use the Select operation on it, the internally constructed deferred execution object implements IEnumerable<T> and not ICollection<T>.  This is despite the fact the count is still known, since the select operation is just a conversion of elements.  A CopyTo method could be written as well.  Things start to get stretched once we hit the Contains method though (as needed for the interface, not required for LINQ optimization&#8230;), which would have to enumerate the entire data set, applying the select conversion to each element&#8230; and what does that mean anyway?<\/p>\n<p>The net result is that something like list.Select(a=>a.ToString()).ToArray() actually internally has to create an array out of an IEnumerable with no count information&#8230; It does this using repeated copying to size doubling arrays, and then trimming the final array by copying into a new array of the right size.<br \/>\nAll that performance loss to avoid a couple of lines of code&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LINQ is a bit of an interesting beast, it makes certain things easier, but it also performs so much worse in many scenarios. I had assumed that LINQ internally would have a decent amount of optimizations, some of its operations will perform better if you know that the incoming collection is ICollection and can thus &hellip; <a href=\"https:\/\/www.themissingdocs.net\/?p=104\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">LINQ missed optimization (and the fun of interfaces)<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-random-musings"],"_links":{"self":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=104"}],"version-history":[{"count":0,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/104\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}