Make Your LINQ Up to 10x Faster!

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ก.พ. 2024
  • Use code MODULAR20 and get 20% off the brand new "Deep Dive into Modular Monoliths" course on Dometrain: dometrain.com/course/deep-div...
    Become a Patreon and get special perks: / nickchapsas
    Hello, everybody, I'm Nick, and in this video, I will show you how you can improve LINQ's performance in .NET by using hardware acceleration with a Nuget package called SimdLinq.
    Give SimdLinq a star on GitHub: github.com/Cysharp/SimdLinq
    Workshops: bit.ly/nickworkshops
    Don't forget to comment, like and subscribe :)
    Social Media:
    Follow me on GitHub: github.com/Elfocrash
    Follow me on Twitter: / nickchapsas
    Connect on LinkedIn: / nick-chapsas
    Keep coding merch: keepcoding.shop
    #csharp #dotnet

ความคิดเห็น • 75

  • @muhamedkarajic
    @muhamedkarajic 4 หลายเดือนก่อน +58

    "... a bunch of advanced stuff which I don't understand" - love that honesty.

  • @nofatchicks6
    @nofatchicks6 4 หลายเดือนก่อน +60

    Always thought that one of the benefits of Linq was that the operations could be improved over time, without breaking the API.

    • @josh_flash
      @josh_flash 4 หลายเดือนก่อน +5

      Yes, exactly this! Adding a 3rd party dependency is not what I would consider "making my LINQ faster." Beyond that, this seems to rely on SIMD, which might not work as expected if you are targeting some embedded systems or WASM.

  • @carlinhos10002
    @carlinhos10002 4 หลายเดือนก่อน +99

    I like you change shirt and hairstyle for the ads

    • @nickchapsas
      @nickchapsas  4 หลายเดือนก่อน +26

      Always

    • @pavlindrom
      @pavlindrom 4 หลายเดือนก่อน +18

      Helps skipping ahead to the end of the ad ;P

    • @okmarshall
      @okmarshall 4 หลายเดือนก่อน +3

      @@pavlindrom this guy living in 2034.

  • @jongeduard
    @jongeduard 4 หลายเดือนก่อน +7

    Yep, cool stuff.
    7:28 What we see here looks more magical than it is. It's just some good old C style pointer arithmetic, wrapped in a bunch of dotnet helper methods, which look quite verbose compared to actual C code. Each ref variable is literally a pointer. The function works with these in the for loops.
    The code basically copies the Span data to the Vector, does the Sum calculation with that, and then copies the result back.
    But what I actually silently hope for with though is that all the methods like Select, Where and Aggregate will get efficient Span based versions as well in the future, so that we won't have loops at all, making C# code look a bit more like Rust code, which uses zero loops most of the time.
    So in other words, I believe in zero cost functional programming.

  •  4 หลายเดือนก่อน +14

    what are the breaking changes compared to the Linq implementation by MS?

  • @Kingside88
    @Kingside88 4 หลายเดือนก่อน +3

    Thank you Nick. Yes the min/max thing is really cool.

  • @sevketevli6927
    @sevketevli6927 3 หลายเดือนก่อน +3

    I learned about a very useful library I didn't know about, thank you.

  • @marketfinds
    @marketfinds 4 หลายเดือนก่อน +7

    That's pretty impressive that runtimes are getting that much smarter to handle basic operations that much quicker!

  • @tomwimmenhove4652
    @tomwimmenhove4652 3 หลายเดือนก่อน +10

    The need for all these specialized methods actually point to a weakness in the C#/.NET compiler. For example, if you write the simple for-loop example you used in C or C++, a compiler like gcc would automatically detect that it could be vectorized, and generate SIMD instructions during compilation, eliminating the need for writing specific specialized methods in the first place. I wish Microsoft spent more time optimizing it's compiler rather than essentially forcing people to come up with complex solutions to speed up your code, whereas a smart compiler could do all this for you. Optimization should be a big part of a compiler recognizing code patterns, rather than having the programmer coming up with ways to write more performant code.

  • @pauljohnsonbringbackdislik1469
    @pauljohnsonbringbackdislik1469 4 หลายเดือนก่อน +21

    Wow! It took over 2500ns with LINQ on .NET 6. And since .NET 8 it is around 45x faster (57ns on my PC)? I would not believe if I would not run it myself locally.

  • @nocturne6320
    @nocturne6320 4 หลายเดือนก่อน +14

    An interesting benchmark to see would be how fast it is when doing these operations over data from a model object.
    Eg. You have a list of users and your want an average of their age, so you'd first have to select the age from the model and then do the average. How much faster would LinQ be then a loop then?

    • @danny10846
      @danny10846 3 หลายเดือนก่อน +1

      It would be the exact same

  • @mrsajjad30
    @mrsajjad30 4 หลายเดือนก่อน

    Very informative. Thanks for sharing

  • @arthurchia2828
    @arthurchia2828 3 หลายเดือนก่อน +2

    Nice content! Love .Net!
    Just wanna bring up another point of view, sometimes the optimisation could come from side way.
    For eg, algorithm of adding number from 1 to n.
    Could be simplified mathematically to formula
    n (n + 1) / 2

  • @L1da77
    @L1da77 4 หลายเดือนก่อน +22

    LongSum you say. Swedish has a word thats pronounced the same way and its Långsam. Difference is that långsam means slow ^^

    • @NickSteffen
      @NickSteffen 4 หลายเดือนก่อน +1

      Pre 64 bit machines long addition was really slow, so it’s an apt name. They effectively took 4 times as long as a regular sum. Now with 64 bit, it’s effectively just a regular add. That said unless you were doing hundreds or thousands of them it probably didn’t matter much.

    • @KeyboardKrieger
      @KeyboardKrieger 3 หลายเดือนก่อน

      In German "langsam" means slow too 😁

  • @Daniel15au
    @Daniel15au 3 หลายเดือนก่อน +2

    This is nice.
    Honestly this isn't going to be the bottleneck for most people though. Most people aren't calling Sum tens of thousands of times.

  • @weicco
    @weicco 4 หลายเดือนก่อน +5

    Well, if you get pointer to the continuous array of (unmanaged) memory you are summing, it is really fast to calculate sum using simple pointer arithmetic. And that's what this library seems to do. But that's what .NET don't want you to do 😄 because otherwise you'd be writing C.

    • @maschyt
      @maschyt 4 หลายเดือนก่อน +1

      If you just do pointer arithmetics to advance over the items one by one to sum, that would be slower than LINQ with its vectorization. On my computer summing 2000 integers with a for-loop is 555ns, LINQ is 108ns and getting a pointer to the array, advancing the pointer until the pointer of the array end is reached is 378ns.

    • @weicco
      @weicco 3 หลายเดือนก่อน

      @@maschyt you are right but if you run the operation so that you split the memory into blocks, process each block in its own thread, and finally sum everything up, you get the speed up - and this is exactly what the library does.
      I've implemented some image processing stuff this way. I split the image horizontally and ran the processing parallel in threads. But I wrote that in C to get down to the machine level without .net coming in my way, because .net does not really want you to access things directly.

  • @scuroguardiano9787
    @scuroguardiano9787 3 หลายเดือนก่อน +1

    I hope that in future Microsoft will add automatic SIMD optimization to normal for loops aswell. C/C++ compiler can vectorize some simple for loops, so Microsoft have here another opportunity for improvement.

  • @EtienneFortin
    @EtienneFortin 4 หลายเดือนก่อน

    I'm a bit surprised that in the loop over all values the compiler wasn't able to vectorize it. Pretty straight forward loop to vectorize for a compiler I would think.

  • @ssa2955
    @ssa2955 3 หลายเดือนก่อน

    Refreshing to hear frome someone like Nick saying he doesent understand some C# code/techniques.
    The last traces of imposter syndrom went suddenly away 😀
    Thank you

  • @T___Brown
    @T___Brown 4 หลายเดือนก่อน

    Can you ask MS if they will be incorporating these changes in core?

  • @krazaam6205
    @krazaam6205 4 หลายเดือนก่อน +2

    "The concept of writing faster code than microsoft is not unique" :D

  • @rhesusfabio
    @rhesusfabio 3 หลายเดือนก่อน

    Is there a way to apply this library to all linq operations in a old .framework (4.7) solution?

  • @randomphilic27
    @randomphilic27 4 หลายเดือนก่อน +3

    Which C# and .NET book will be best to read to have good grasp on fundamentals and understand how these internally works?

    • @MaximilienNoal
      @MaximilienNoal 4 หลายเดือนก่อน +1

      C# in Depth is still good.

    • @amallkrishna
      @amallkrishna 4 หลายเดือนก่อน

      C# in a Nutshell is an amazing book :)

    • @DJDoena
      @DJDoena 4 หลายเดือนก่อน +1

      A few years ago I made a video series about the internals here on youtube. It's in German but the auto-generated subtitles seem to work reasonably well.

    • @randomphilic27
      @randomphilic27 4 หลายเดือนก่อน +2

      how about pro C# 10 with .NET 6

  • @djdejlig8270
    @djdejlig8270 4 หลายเดือนก่อน

    Yo Nick, what theme are you using for Rider?

    • @czachowsky
      @czachowsky 4 หลายเดือนก่อน

      my guess is "Visual studio dark"

  • @adamdegerman960
    @adamdegerman960 4 หลายเดือนก่อน +1

    Yes, this is kind of interesting, but we all know that linq is most often used to do select, order/sort operations

  • @novaardent4528
    @novaardent4528 4 หลายเดือนก่อน

    if you were forced into a c# version that doesn't have this yet implemented, could one emulate the API and implementation to achieve this? I assume yes

  • @diadetediotedio6918
    @diadetediotedio6918 4 หลายเดือนก่อน

    I think you can use interceptors to resolve LINQ in a more performant way (I did this with some things already), but the lack of closure captures makes it very strange. If only microsoft stopped with the "don't modify source code" bs.

  • @Thorarin
    @Thorarin 4 หลายเดือนก่อน +1

    I'm curious what behavior they are breaking with this, that is allowing this much extra performance 🤔

    • @fgsdhdfgjfyhsg
      @fgsdhdfgjfyhsg 4 หลายเดือนก่อน

      Memory safety I think? They're using unsafe stuff from what I saw

    • @billy65bob
      @billy65bob 4 หลายเดือนก่อน +2

      The github says they don't take things like 'NaN' into account, and that many of the methods are 'unchecked' (meaning they don't check for overflows).
      Also mentions there are slight rounding inconsistencies in floating point results, due to values being evaluated in a very different order.

  • @kahnfatman
    @kahnfatman 3 หลายเดือนก่อน

    Back in my Dataset days -- apparently MS intends to download the entire database onto a windows form...

  • @ivank6486
    @ivank6486 3 หลายเดือนก่อน

    SIMDeez nuts!

  • @briannielsbergh
    @briannielsbergh 2 หลายเดือนก่อน

    LoL, try doing something in the Ms or sec range, and check if it's still 10x faster

  • @anlKaynarr
    @anlKaynarr 3 หลายเดือนก่อน

    Great Video.
    In my i7 4770 CPU for loop is still faster :)

  • @user-nk4xc8rc6p
    @user-nk4xc8rc6p 4 หลายเดือนก่อน

    Hi! Could you make a video explaining differences between visual studio and your IDE, plis? Thanks

    • @JollyGiant19
      @JollyGiant19 4 หลายเดือนก่อน +2

      It’s JetBrains Rider, you can look up comparisons for it

    • @user-nk4xc8rc6p
      @user-nk4xc8rc6p 4 หลายเดือนก่อน

      @@JollyGiant19 i know i can look it up. I was asking for It to someone who has expirience with it

    • @natescode
      @natescode 4 หลายเดือนก่อน

      VS is from Microsoft
      Rider is from Jet brains
      Rider is cross platform and works really well. I like Riders default key bindings better.

  • @chimnaywanoskar198
    @chimnaywanoskar198 3 หลายเดือนก่อน

    Is it true linq join has poor performance than SQL view??

  • @LeMustache
    @LeMustache 4 หลายเดือนก่อน +1

    Hi Nick. Could you ban that bot with inappropriate profile picture that keeps posting generic comments like "Wow, the production quality is off the charts" on your videos. I can see it under every video of yours.

    • @MaximilienNoal
      @MaximilienNoal 4 หลายเดือนก่อน

      It's everywhere on yt. I keep reporting it but yt doesn't give a damn.

    • @nickchapsas
      @nickchapsas  4 หลายเดือนก่อน +4

      I keep banning it and it keeps coming back

    • @mariocamspam72
      @mariocamspam72 4 หลายเดือนก่อน

      I think its hilarious, I reply to it every time with equally nonsensical crap 😂

  • @Kolbein837
    @Kolbein837 4 หลายเดือนก่อน +3

    Wow, the production quality is off the charts

  • @yufgyug3735
    @yufgyug3735 4 หลายเดือนก่อน +5

    i really think .net ecosystem lacks resources for learning how to write high performance code, memory management etc

    • @sidaz22
      @sidaz22 3 หลายเดือนก่อน

      I agree.

  • @colin87
    @colin87 4 หลายเดือนก่อน +2

    First() or [0]?

    • @MaximilienNoal
      @MaximilienNoal 4 หลายเดือนก่อน +6

      FirstOrDefault usually...

    • @litus01
      @litus01 4 หลายเดือนก่อน +1

      prefer.?Last(to => to.post);

    • @fifty-plus
      @fifty-plus 4 หลายเดือนก่อน

      If you have an IList First() will call [0] for you. First operates on IEnumerable so it has other checks to get you the first element for differing implementations, like IPartition.

  • @szikig
    @szikig 4 หลายเดือนก่อน +1

    Fastest way to sum range: ( + ) * ( / 2) :-)

    • @alfflasymphonyx
      @alfflasymphonyx 4 หลายเดือนก่อน +1

      This works only if the interval between each number is the same

    • @johnnykeems2911
      @johnnykeems2911 3 หลายเดือนก่อน +1

      Obviously that will work in very particular case when the range represents arithmetic progression, because actually this is the formula for sum of arithmetic progression, but as
      @alfflasymphonyx said, not any range is arithmetic progression

    • @szikig
      @szikig 3 หลายเดือนก่อน

      @@johnnykeems2911 You are all wrong. Nick talked about the fastest way to get the sum of Enumerable.Range. Well, that's what I wrote. And that was a joke, of course. You don't have to take it seriously.

    • @johnnykeems2911
      @johnnykeems2911 3 หลายเดือนก่อน

      @@szikig I'm a programmer, which means I'm kind of pedantic person. Your answer joke or not, is generally incorrect, so I dont really understand how I could be wrong. If it is a joke, that's fine, cant read your mind to know when you are joking, I just noticed that this formula only works when the sequence is an arithmetic progression.

    • @szikig
      @szikig 3 หลายเดือนก่อน

      @@johnnykeems2911 and this is what is always true for Enumerable.Range, right ?