How IEnumerable can kill your performance in C#

2022 ж. 31 Там.
109 359 Рет қаралды

The first 100 of you can use code SCHOOL2022 for 20% off courses and bundles at dometrain.com
Become a Patreon and get source code access: / nickchapsas
Hello everybody I'm Nick and in this video I will show you how IEnumerable can harm your application's performance. I will explain why it happens, what you can do about it and how to deal with it in future scenarios.
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet

Пікірлер
  • The rule is simple - return precise types, and accept abstracted types. If you return List<T>, then your method's return type should be List<T> not IEnumerable<T>. So consumers can exactly now what is the actual type and if they want to, they can limit it to an interface implicitly.

    @sergiuszzalewski1947@sergiuszzalewski1947 Жыл бұрын
  • I swear this is one of those things resharper has taught me with it's warnings, I rarely see it now because I know better. Great explanation of multiple enumerations.

    @TheBreaded@TheBreaded Жыл бұрын
  • This enumeration style is called co-routine for those who didn't know. You basically have a function on hold that can give you the next element right when you need it 😄

    @marcotroster8247@marcotroster8247 Жыл бұрын
  • A common recurring problem among programmers is not knowing how the code they're using works. At the very least, they should understand what the API commits to doing. Deferred enumeration of IEnumerable is a great feature in C#, but if you're using any API that exposes an IEnumerable object, you should always assume that you need to enumerate at some point, unless your objective is to merely chain subsequent operations to perform on the object.

    @marcusmajarra@marcusmajarra Жыл бұрын
  • I been programming with C# for about 15 years and there are parts about it that still mystify me. Your example of obtaining a count via an IEnumerable reminded me of how I learned on my own a similar situation with your example. In my case I was loading over 100k records. EF was new to me and I couldn't understand why my app was taking a performance hit until I discovered the difference between IEnumerable and IQueryable. From then on it forced me to take into consideration the overall purpose of the program and how to use IEnumerable properly. You are very well versed in the programming language, more than me after working with C# for so long.

    @rafaelm.2056@rafaelm.2056 Жыл бұрын
  • When I started to think about it more deeply, this system is actually very very good:

    @mastermati773@mastermati773 Жыл бұрын
  • Great illustration of how/why this happens. Something I can send to my peers that get confused as to why their code is hitting an API twice when running around with IEnumerable or IQueryable.

    @asteinerd@asteinerd Жыл бұрын
  • You have no idea how much this helped me today! I was looking at a problem where counting an IEnumerable with zero elements in it resulted in a significant delay and I thought I was going crazy! I had no idea that IEnumerable would be lazily evaluated. Thanks for the help! :)

    @michaellombardi3638@michaellombardi3638 Жыл бұрын
  • Great video! I always feel good about myself when I know exactly what the problem is and what your solution is going to be at the start of the video... It doesn't happen often, but when it does, I give myself a gold star :-) Thanks for posting such good content!

    @SmoothSkySailin@SmoothSkySailin Жыл бұрын
  • I seem to remember the LINQ documentation explicitly stating that Enumerables are lazy-evaluated. It is a feature, one that all developers should be cognizant of so that they can force one-time evaluation when appropriate.

    @kenbrady119@kenbrady119 Жыл бұрын
  • This is something I knew about and have been working to pass on to others as well. Thanks for making this video. I will share this with them in the future!

    @stephajn@stephajn Жыл бұрын
  • Great stuff Nick. Your impact on my programming had been tremendous!

    @emmanueladebiyi2109@emmanueladebiyi2109 Жыл бұрын
  • My personal choice is to return an I…Collection, so that the consumer knows that the “inner” code isn’t deferred. Of course, there are situations where an IEnumerable is better, for instance when implementing repositories. But such repositories are mostly consumed from other application specific services.

    @mariorobben794@mariorobben794 Жыл бұрын
  • The biggest problem I have with Linq in general rather then IEnumerables is the heap allocation that takes place when evaluating queries with ToList() and the like in memory-sensitive hot paths. In almost every other scenario it's absolutely fine, but it makes my life hell when I have to do rate calculations on 100-500 messages/s.

    @jamesmussett@jamesmussett Жыл бұрын
  • I learned this the hard way too, but it was a very important and interesting lesson to learn.

    @joost00719@joost00719 Жыл бұрын
  • Great point! There was a riddle posted not long ago, showing what would happen if you essentially

    @marna_li@marna_li Жыл бұрын
  • A bigger problem with methods that return IEnumerable is when they take parameters like a Stream or any IDisposable.

    @superior5129@superior5129 Жыл бұрын
  • it really is quite clean, there is even a warning (at least in visual studio) CA1851: Possible multiple enumerations of IEnumerable collection

    @nocgod@nocgod Жыл бұрын
  • I was just giving my developers a lesson on this exact topic last week. I wish I could have just pointed them at this video! Thanks so much!

    @krftsman@krftsman Жыл бұрын
  • Whoa, for the past one year I was getting sometimes warnings "Possible multiple enumerations" and never knew what does it mean :V Thank you!

    @Arekadiusz@Arekadiusz Жыл бұрын
KZhead