The High Performance Types You Ignored for Years in .NET

Поділитися
Вставка
  • Опубліковано 6 сер 2023
  • Use code ARCH15 and get 15% off the brand new Solution Architecture course on Dometrain: dometrain.com/course/getting-...
    Become a Patreon and get source code access: / nickchapsas
    Hello, everybody, I'm Nick, and in this video, I will show you a type that we had in C# and .NET since .NET Framework 2 that can offer similar benefits to the Span type. I will explain what it is, how it works and why it might still be relevant if you're stuck in an old .NET version.
    Span video: • What is Span in C# and...
    Stephen Toub podcast: • Why is .NET so Insanel...
    Workshops: bit.ly/nickworkshops
    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

КОМЕНТАРІ • 117

  • @user-zk5ym9ut1j
    @user-zk5ym9ut1j 10 місяців тому +242

    Time to rename channel to OnlySpans

    • @GambitVil
      @GambitVil 10 місяців тому +4

      It's good naming for site with programmers nudes and etc :)))

    • @MartinMaat
      @MartinMaat 10 місяців тому +1

      There's a Monty Python sketch in this.

    • @RoyBarina
      @RoyBarina 10 місяців тому

      @@GambitVil But only for open source (where the bits are exposed)

    • @rngesus8057
      @rngesus8057 10 місяців тому

      oh man so good!

    • @RiversJ
      @RiversJ 10 місяців тому +1

      Hell yeah, first time i used them i simplified and optimized in one del deep a performance critical problem from catastrophic to world class performance even compared to native libraries i became a devout convert!

  • @gctypo2838
    @gctypo2838 7 місяців тому +3

    Good to know. I'm stuck with a Framework 4.0 codebase (yes, that's a ".0") and having something that can serve the place of Span is really nice.

  • @ThisIsAGoodUserNameToo
    @ThisIsAGoodUserNameToo 10 місяців тому +44

    Finally a span-ish c# video

    • @nickchapsas
      @nickchapsas  10 місяців тому +19

      I shouldn't have laughed at this as much as I did

    • @insomnyawolf
      @insomnyawolf 9 місяців тому

      It caught me off guard and made me smile a bit, thank you!

  • @JakobStrasser
    @JakobStrasser 10 місяців тому +15

    Thanks, stuck in framework 4.8 so this will help!

    • @joshpatton757
      @joshpatton757 10 місяців тому +12

      There's dozens of us, dozens!

    • @protox4
      @protox4 10 місяців тому +9

      You can also use the Span nuget package in old frameworks.

  • @AndyNeko
    @AndyNeko 10 місяців тому +9

    Thank you so much! I'm reading & coding with book C# 10 in a Nutshell, and this Span thing comes in Chap. 23. I'll get there soon!

  • @saymehname
    @saymehname 9 місяців тому +3

    Thanks for this I wasn’t aware of either. I’m a noob but you’ve taught me a lot

  • @reikooters
    @reikooters 10 місяців тому +14

    Bought your course to support you. Always learn something interesting from your videos. Best person on UA-cam for C# stuff. Thank you!

  • @peceed
    @peceed 10 місяців тому +5

    "Fun" fact: in Java every String is a Span over internal shared char array. This way substring was blazing fast, but it led to many memory leaks because GC was unable to free unused parts of that char array.

    • @rhysvanderwaerden5518
      @rhysvanderwaerden5518 9 місяців тому

      Why past tense? Did they change it?

    • @peceed
      @peceed 9 місяців тому +2

      @@rhysvanderwaerden5518 IIRC - yes.
      Substring creates full copy now.
      The correct move was to make GC that was aware of this issue and could split backed array.

  • @k_pavel
    @k_pavel 10 місяців тому +5

    We have three names:
    Nick
    Rick
    **some pause**
    Richard

  • @IronTeddyBear
    @IronTeddyBear 10 місяців тому +2

    Thank you @Nick, I still work on projects in .NET 4.x and I didn't even know this ArraySegment even existed before.

  • @carstenberggreen7509
    @carstenberggreen7509 10 місяців тому

    Very interesting! Thanks

  • @Pouya..
    @Pouya.. 7 місяців тому

    Nick, please make a video about finding bottlenecks in high-load applications and how we should approach them when it is in production. How we should find out why our app running slow on high-load

  • @IllidanS4
    @IllidanS4 10 місяців тому +1

    ArraySegment is really nice when the byref-like nature of Span is something you cannot work around, like in an "out" parameter, a class field, generics etc. The proper counterpart to Span would be Memory there, but ArraySegment just sounds nicer.

  • @F1nalspace
    @F1nalspace 10 місяців тому +3

    Didn´t knew ArraySegment and StringSegment, but as we already switched to .NET 7, we already use span for all other cases.
    But what about you a case where you need to remove some items in a internally stored collection and you have a method "bool RemoveItems(params IItem items);". It would not make sense to pass down a ReadOnlySpan as argument instead isnt´t it?

  • @sohampatel1063
    @sohampatel1063 10 місяців тому +12

    Richard Chapsas, that was cool🤣🤣

    • @TheLilRussia
      @TheLilRussia 10 місяців тому

      I wonder if Rick and Dick are the same Human but with split personalities.

    • @richard_developer_agriculture
      @richard_developer_agriculture 10 місяців тому +1

      👆🙄 I found it less amusing...
      .
      .
      .
      😜

  • @yobofunk5689
    @yobofunk5689 10 місяців тому +2

    It would be interesting to run the benchmark on struct and class. Wouldn't decrease the benefit as class are supposed to be passed by ref?

  • @PatrickHoodDaniel
    @PatrickHoodDaniel 10 місяців тому +2

    Excellent content. I am always going to your videos to get great C# information, which is my preferred language. However, I have been wanting to get into ML with C#, but python seems to be dominant in this field. How do you feel about this subject. I don't particularly want to learn python, but I may have to. I just love C# so much, it is a shame I would need to switch.

    • @anm3037
      @anm3037 10 місяців тому +1

      You can do it in c#. I attended a semester masters course last year on Advanced ML. I told the professor that I would never use or write the exam in python and he agreed.

    • @pushyoch.8252
      @pushyoch.8252 10 місяців тому

      ​@@anm3037afaik from my time at /g/ "consumer grade" ai domains that can be locally hosted like SD & LLMs (llama & alpaca) are all built & trained on python. are there alternatives?

    • @PatrickHoodDaniel
      @PatrickHoodDaniel 10 місяців тому

      @@anm3037 Great to know. I will seek more information on it.

  • @carmineos
    @carmineos 10 місяців тому +6

    Instead of AsSpan().Slice(...), calling AsSpan overload which takes a range parameter should be even faster

    • @ZintomV1
      @ZintomV1 10 місяців тому +1

      Seeing how simple and common that pattern is, I imagine that it is lowered this way during compilation

  • @dejowe
    @dejowe 10 місяців тому

    Nice video. :)
    I am curious about your Benchmarks.cs file. Is it accessible from somewhere?

  • @modernkennnern
    @modernkennnern 10 місяців тому +5

    Spans will become a lot better when collection expressions arrive in C# 12.
    Span ints = [1, 2, 3]; // will be stack-allocated

    • @qj0n
      @qj0n 10 місяців тому +2

      isn't it the same as currently supported Span ints = stackalloc[] { 1, 2, 3 }; ?

    • @modernkennnern
      @modernkennnern 10 місяців тому +2

      "A lot" might be a stretch.
      I find the current syntax daunting and confusing personally. It'll be a lot easier when it's just as easy to use as any other collection type :)

    • @mariocamspam72
      @mariocamspam72 10 місяців тому

      ​@@modernkennnernDid you just reply to yourself to boost comment engagement

    • @modernkennnern
      @modernkennnern 10 місяців тому

      @@mariocamspam72 no(?)

    • @maxsteffey6521
      @maxsteffey6521 10 місяців тому

      @@qj0nyes but no "unsafe" declaration 😂

  • @garcipat
    @garcipat 10 місяців тому

    Can you use spans only for sequencial items? That if you enumeratithhe there you cannot to AsSpan afterwards?

  • @pavlindrom
    @pavlindrom 10 місяців тому +7

    Wouldn't Take(3) without the ToArray be similarly performant? I thought you'd show that as a comparison too, and now I'm curious how it compares.

    • @_maxt
      @_maxt 10 місяців тому +8

      afaik that'd cause allocations because of enumeration. Also you can't randomly access the result obv.

    • @parlor3115
      @parlor3115 10 місяців тому

      @@_maxt Doing that I guess gives you a heap-allocated IEnumerable which doesn't allow index access (only looping)

  • @Haapavuo
    @Haapavuo 10 місяців тому +1

    Hello, this is Nick Spanchap. Today we talk about Spanlike structures.

  • @petewarner1077
    @petewarner1077 10 місяців тому +1

    Poor old Wick Chapsas :( The guy never gets a chance.

  • @vertxxyz
    @vertxxyz 10 місяців тому +2

    I really wish you used a colour scheme that distinguished between structs and classes.
    It always throws me off and makes it so you have to go to the source/inspect, or have prior knowledge.

  • @pawelwelsberg6800
    @pawelwelsberg6800 10 місяців тому

    Can be very useful especially if you are following functional programming practices: your data is immutable.

  • @satyayuga0
    @satyayuga0 10 місяців тому +2

    throw new EntityMissingException("Sick Chapsas");

  • @KareemNOV
    @KareemNOV 10 місяців тому

    3 slices Nick, Rick and emm what

  • @Kingside88
    @Kingside88 10 місяців тому +1

    I just wonder if the compiler could translate the Linq solution into span behind the scenes

    • @finickyflame
      @finickyflame 10 місяців тому

      Probably not the compiler, but just the code itself. They already do some optimizations depending on the type of collection being used, but they could maybe convert to span as well.

    • @anm3037
      @anm3037 10 місяців тому

      @@finickyflamethere’s no proof for that. Nothing will do that for you

    • @Zullfix
      @Zullfix 10 місяців тому

      I am very disappointed in the lack of macros in C#. Source generators are awesome but they can only do so much.

    • @finickyflame
      @finickyflame 10 місяців тому

      @@anm3037 I'm referring to the enumerable extensions. They already do some optimization depending on the type of collection that is being used. e.g. Enumerable.Count() calls ICollection.Count instead of iterating the whole collection to count. They could update the enumerable extensions to use span when possible

  • @W1ese1
    @W1ese1 10 місяців тому +5

    You killed me with the Scientific Paint drawing... 🤣

    • @F1nalspace
      @F1nalspace 10 місяців тому

      Yeah me too... maybe a drawing pad would help 😅

  • @dmitrykim3096
    @dmitrykim3096 10 місяців тому +2

    Span is c# window to thr world of c++

  • @rojay12
    @rojay12 10 місяців тому +1

    "Richard" 😆

  • @pavfrang
    @pavfrang 10 місяців тому

    If the name was "View" in all span-related types and functions, it would be much clearer from the beginning and more appealing to use.

  • @adrian_franczak
    @adrian_franczak 10 місяців тому +5

    indeed very scientific

  • @RockTheCage55
    @RockTheCage55 10 місяців тому +2

    Nick is the "Keep Coding" podcast still done? noticed you haven't had an episode since March.

    • @nickchapsas
      @nickchapsas  10 місяців тому +5

      Resuming in September

  • @Rafael-vn2bo
    @Rafael-vn2bo 10 місяців тому

    Great 👍

  • @daraghodonoghue_
    @daraghodonoghue_ 10 місяців тому

    Might be a really stupid question. But ill ask anyway.
    Lets say you have an array with 10 items.
    You can do MyArray.Length.
    Is there any reason to to
    MyArray.AsSpan().Count ?
    Surely not.

  • @stefano_schmidt
    @stefano_schmidt 10 місяців тому

    is it technically the same as slices in Go?

  • @cryptoeric24
    @cryptoeric24 10 місяців тому

    LOL, Richard

  • @MatinDevs
    @MatinDevs 6 місяців тому

    What Chapsas?

  • @user-di8kl4cc5u
    @user-di8kl4cc5u 10 місяців тому

    CSharpians

  • @_maxt
    @_maxt 10 місяців тому +2

    One thing I noticed with ArraySegment is that it's implementing a few interfaces. Is that one of it's downfalls because it'd be boxed when used as IList for example?

    • @qj0n
      @qj0n 10 місяців тому +1

      every struct will be boxed if used as interface, so AFAIK it's still the fastest way to fulfil contract requiring IList

    • @Zullfix
      @Zullfix 10 місяців тому +1

      Rather than boxing with the interface, use the interfaces for generic type constraints.

  • @Kevinf63
    @Kevinf63 10 місяців тому

    The Dometrain course is hitting an error when attempting to purchase after card authorization.

  • @p0z1ck
    @p0z1ck 8 місяців тому

    span is only ignored because it can't be used in real world (async everywhere) applications

  • @rogerstich7603
    @rogerstich7603 10 місяців тому

    While i really like your Videos there is one point i hate: In many of your Videos (for example this one) you tease another video from yours. In this case the Video about the span type and you point out that we should take a look at the description to find the link
    But theres no link, not even a hint, just advertising for your courses. So please, if you tell us you link it in the description then link it in the description.

    • @nickchapsas
      @nickchapsas  10 місяців тому +1

      Yes there is. There is a "Span video: " part pointing to the Span video

    • @rogerstich7603
      @rogerstich7603 10 місяців тому

      @@nickchapsas waaaah, im sorry. i havent seen the link because you formatted it. i was searching for a blue font.
      My fault and thanks for the Reply.

  • @jameshancock
    @jameshancock 10 місяців тому

    The question is why hasn't ArraySegment and StringSegement been marked as depreciated and into the dustbin of history so that we can eliminate bloat from the runtime.

    • @anm3037
      @anm3037 10 місяців тому +1

      No, they are still useful

    • @jameshancock
      @jameshancock 10 місяців тому

      @@anm3037 Since span literally does exactly what these do, why?

    • @Kwpolska
      @Kwpolska 10 місяців тому +1

      Because C# is developed by a company which cares about backwards compatibility and would rather keep some older stuff around instead of breaking your code every year?

    • @jameshancock
      @jameshancock 10 місяців тому

      @@Kwpolska Except this is basically a 1 for 1 substitution by just switching to AsSpan() since it's effectively the same thing.
      So marking stuff that is replaced by better, faster, cheaper with almost no work to do it, and lots of warning for 2 or 3 versions is desirable so that you don't have crud all over the runtime.

    • @IsakSavo
      @IsakSavo 10 місяців тому +3

      @@jameshancockthe fact that it isn’t a ref struct means you have more freedom with it. For example in iterators and async methods. It also implements IList which makes it possible to pass as argument to other existing methods not specifically designed for it

  • @dmitrykim3096
    @dmitrykim3096 10 місяців тому

    Pute c# is safe but sacrifices efficiency cause it uses morr allocationes and copying.

    • @qj0n
      @qj0n 10 місяців тому +3

      that's where types like Span or ArraySegment come handy as they're both safe and efficient

  • @bdcp
    @bdcp 10 місяців тому

    Why can't they do this shit internally in Linq

    • @Zullfix
      @Zullfix 10 місяців тому

      It would be really cool to have a SpanAction version of LINQ

  • @euphonesse55
    @euphonesse55 10 місяців тому +1

    I like your content, but you speak a bit too fast, it always feels rushed but it be more effective if there were more moments to digest

    • @Sahuagin
      @Sahuagin 10 місяців тому +1

      eh, I think it's too slow actually. you can use the playback speed to change the speed if that helps. I often watch him at 2x speed.

  • @Marywqq_
    @Marywqq_ 10 місяців тому +1

    First

  • @dmitrykim3096
    @dmitrykim3096 10 місяців тому

    You know what? I rememeer in Pascal we had assembler inline code to make it faster. Spans are like an inline c++ in c#

  • @der.Schtefan
    @der.Schtefan 10 місяців тому

    When a serious and revered UA-camr gives up his day job for YT content, and starts having thumbnails like LinusTechTips, it's time to unsubscribe.

  • @tidus93
    @tidus93 10 місяців тому

    Thank you for the awesome C# content