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
Time to rename channel to OnlySpans
It's good naming for site with programmers nudes and etc :)))
There's a Monty Python sketch in this.
@@GambitVil But only for open source (where the bits are exposed)
oh man so good!
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!
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.
Finally a span-ish c# video
I shouldn't have laughed at this as much as I did
It caught me off guard and made me smile a bit, thank you!
Thanks, stuck in framework 4.8 so this will help!
There's dozens of us, dozens!
You can also use the Span nuget package in old frameworks.
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!
Thanks for this I wasn’t aware of either. I’m a noob but you’ve taught me a lot
Bought your course to support you. Always learn something interesting from your videos. Best person on UA-cam for C# stuff. Thank you!
"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.
Why past tense? Did they change it?
@@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.
We have three names:
Nick
Rick
**some pause**
Richard
Thank you @Nick, I still work on projects in .NET 4.x and I didn't even know this ArraySegment even existed before.
Very interesting! Thanks
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
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.
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?
Richard Chapsas, that was cool🤣🤣
I wonder if Rick and Dick are the same Human but with split personalities.
👆🙄 I found it less amusing...
.
.
.
😜
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?
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.
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.
@@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?
@@anm3037 Great to know. I will seek more information on it.
Instead of AsSpan().Slice(...), calling AsSpan overload which takes a range parameter should be even faster
Seeing how simple and common that pattern is, I imagine that it is lowered this way during compilation
Nice video. :)
I am curious about your Benchmarks.cs file. Is it accessible from somewhere?
Spans will become a lot better when collection expressions arrive in C# 12.
Span ints = [1, 2, 3]; // will be stack-allocated
isn't it the same as currently supported Span ints = stackalloc[] { 1, 2, 3 }; ?
"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 :)
@@modernkennnernDid you just reply to yourself to boost comment engagement
@@mariocamspam72 no(?)
@@qj0nyes but no "unsafe" declaration 😂
Can you use spans only for sequencial items? That if you enumeratithhe there you cannot to AsSpan afterwards?
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.
afaik that'd cause allocations because of enumeration. Also you can't randomly access the result obv.
@@_maxt Doing that I guess gives you a heap-allocated IEnumerable which doesn't allow index access (only looping)
Hello, this is Nick Spanchap. Today we talk about Spanlike structures.
Poor old Wick Chapsas :( The guy never gets a chance.
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.
Can be very useful especially if you are following functional programming practices: your data is immutable.
throw new EntityMissingException("Sick Chapsas");
3 slices Nick, Rick and emm what
I just wonder if the compiler could translate the Linq solution into span behind the scenes
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.
@@finickyflamethere’s no proof for that. Nothing will do that for you
I am very disappointed in the lack of macros in C#. Source generators are awesome but they can only do so much.
@@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
You killed me with the Scientific Paint drawing... 🤣
Yeah me too... maybe a drawing pad would help 😅
Span is c# window to thr world of c++
"Richard" 😆
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.
indeed very scientific
Nick is the "Keep Coding" podcast still done? noticed you haven't had an episode since March.
Resuming in September
Great 👍
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.
is it technically the same as slices in Go?
Technically, yes.
LOL, Richard
What Chapsas?
CSharpians
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?
every struct will be boxed if used as interface, so AFAIK it's still the fastest way to fulfil contract requiring IList
Rather than boxing with the interface, use the interfaces for generic type constraints.
The Dometrain course is hitting an error when attempting to purchase after card authorization.
span is only ignored because it can't be used in real world (async everywhere) applications
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.
Yes there is. There is a "Span video: " part pointing to the Span video
@@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.
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.
No, they are still useful
@@anm3037 Since span literally does exactly what these do, why?
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?
@@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.
@@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
Pute c# is safe but sacrifices efficiency cause it uses morr allocationes and copying.
that's where types like Span or ArraySegment come handy as they're both safe and efficient
Why can't they do this shit internally in Linq
It would be really cool to have a SpanAction version of LINQ
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
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.
First
You know what? I rememeer in Pascal we had assembler inline code to make it faster. Spans are like an inline c++ in c#
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.
Thank you for the awesome C# content