The High Performance Types You Ignored for Years in .NET

  Рет қаралды 45,372

Nick Chapsas

Nick Chapsas

Күн бұрын

Use code ARCH15 and get 15% off the brand new Solution Architecture course on Dometrain: dometrain.com/...
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/nickwor...
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasG...
Follow me on Twitter: bit.ly/ChapsasT...
Connect on LinkedIn: bit.ly/ChapsasL...
Keep coding merch: keepcoding.shop
#csharp #dotnet

Пікірлер: 118
@user-zk5ym9ut1j
@user-zk5ym9ut1j Жыл бұрын
Time to rename channel to OnlySpans
@GambitVil
@GambitVil Жыл бұрын
It's good naming for site with programmers nudes and etc :)))
@MartinMaat
@MartinMaat Жыл бұрын
There's a Monty Python sketch in this.
@RoyBarina
@RoyBarina Жыл бұрын
@@GambitVil But only for open source (where the bits are exposed)
@rngesus8057
@rngesus8057 Жыл бұрын
oh man so good!
@RiversJ
@RiversJ Жыл бұрын
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!
@ThisIsAGoodUserNameToo
@ThisIsAGoodUserNameToo Жыл бұрын
Finally a span-ish c# video
@nickchapsas
@nickchapsas Жыл бұрын
I shouldn't have laughed at this as much as I did
@insomnyawolf
@insomnyawolf Жыл бұрын
It caught me off guard and made me smile a bit, thank you!
@gctypo2838
@gctypo2838 10 ай бұрын
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.
@JakobStrasser
@JakobStrasser Жыл бұрын
Thanks, stuck in framework 4.8 so this will help!
@joshpatton757
@joshpatton757 Жыл бұрын
There's dozens of us, dozens!
@protox4
@protox4 Жыл бұрын
You can also use the Span nuget package in old frameworks.
@AndyNeko
@AndyNeko Жыл бұрын
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!
@peceed
@peceed Жыл бұрын
"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 Жыл бұрын
Why past tense? Did they change it?
@peceed
@peceed Жыл бұрын
@@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.
@saymehname
@saymehname Жыл бұрын
Thanks for this I wasn’t aware of either. I’m a noob but you’ve taught me a lot
@IronTeddyBear
@IronTeddyBear Жыл бұрын
Thank you @Nick, I still work on projects in .NET 4.x and I didn't even know this ArraySegment even existed before.
@reikooters
@reikooters Жыл бұрын
Bought your course to support you. Always learn something interesting from your videos. Best person on KZbin for C# stuff. Thank you!
@k_pavel
@k_pavel Жыл бұрын
We have three names: Nick Rick **some pause** Richard
@vothaison
@vothaison 10 күн бұрын
A wise man once said: What a poor choice of word!
@modernkennnern
@modernkennnern Жыл бұрын
Spans will become a lot better when collection expressions arrive in C# 12. Span ints = [1, 2, 3]; // will be stack-allocated
@qj0n
@qj0n Жыл бұрын
isn't it the same as currently supported Span ints = stackalloc[] { 1, 2, 3 }; ?
@modernkennnern
@modernkennnern Жыл бұрын
"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 Жыл бұрын
​@@modernkennnernDid you just reply to yourself to boost comment engagement
@modernkennnern
@modernkennnern Жыл бұрын
@@mariocamspam72 no(?)
@maxsteffey6521
@maxsteffey6521 Жыл бұрын
@@qj0nyes but no "unsafe" declaration 😂
@Pouya..
@Pouya.. 10 ай бұрын
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 Жыл бұрын
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.
@carmineos
@carmineos Жыл бұрын
Instead of AsSpan().Slice(...), calling AsSpan overload which takes a range parameter should be even faster
@ZintomV1
@ZintomV1 Жыл бұрын
Seeing how simple and common that pattern is, I imagine that it is lowered this way during compilation
@F1nalspace
@F1nalspace Жыл бұрын
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 Жыл бұрын
Richard Chapsas, that was cool🤣🤣
@TheLilRussia
@TheLilRussia Жыл бұрын
I wonder if Rick and Dick are the same Human but with split personalities.
@richard_developer_agriculture
@richard_developer_agriculture Жыл бұрын
👆🙄 I found it less amusing... . . . 😜
@Haapavuo
@Haapavuo Жыл бұрын
Hello, this is Nick Spanchap. Today we talk about Spanlike structures.
@PatrickHoodDaniel
@PatrickHoodDaniel Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
​@@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 Жыл бұрын
@@anm3037 Great to know. I will seek more information on it.
@yobofunk5689
@yobofunk5689 Жыл бұрын
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?
@pavlindrom
@pavlindrom Жыл бұрын
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 Жыл бұрын
afaik that'd cause allocations because of enumeration. Also you can't randomly access the result obv.
@parlor3115
@parlor3115 Жыл бұрын
@@_maxt Doing that I guess gives you a heap-allocated IEnumerable which doesn't allow index access (only looping)
@petewarner1077
@petewarner1077 Жыл бұрын
Poor old Wick Chapsas :( The guy never gets a chance.
@joga_bonito_aro
@joga_bonito_aro Жыл бұрын
throw new EntityMissingException("Sick Chapsas");
@vertxxyz
@vertxxyz Жыл бұрын
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 Жыл бұрын
Can be very useful especially if you are following functional programming practices: your data is immutable.
@pavfrang
@pavfrang Жыл бұрын
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.
@dmitrykim3096
@dmitrykim3096 Жыл бұрын
Span is c# window to thr world of c++
@carstenberggreen7509
@carstenberggreen7509 Жыл бұрын
Very interesting! Thanks
@garcipat
@garcipat Жыл бұрын
Can you use spans only for sequencial items? That if you enumeratithhe there you cannot to AsSpan afterwards?
@W1ese1
@W1ese1 Жыл бұрын
You killed me with the Scientific Paint drawing... 🤣
@F1nalspace
@F1nalspace Жыл бұрын
Yeah me too... maybe a drawing pad would help 😅
@KareemNOV
@KareemNOV Жыл бұрын
3 slices Nick, Rick and emm what
@dejowe
@dejowe Жыл бұрын
Nice video. :) I am curious about your Benchmarks.cs file. Is it accessible from somewhere?
@adrian_franczak
@adrian_franczak Жыл бұрын
indeed very scientific
@Kingside88
@Kingside88 Жыл бұрын
I just wonder if the compiler could translate the Linq solution into span behind the scenes
@finickyflame
@finickyflame Жыл бұрын
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 Жыл бұрын
@@finickyflamethere’s no proof for that. Nothing will do that for you
@Zullfix
@Zullfix Жыл бұрын
I am very disappointed in the lack of macros in C#. Source generators are awesome but they can only do so much.
@finickyflame
@finickyflame Жыл бұрын
@@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
@rojay12
@rojay12 Жыл бұрын
"Richard" 😆
@daraghodonoghue_
@daraghodonoghue_ Жыл бұрын
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.
@MatinDevs
@MatinDevs 9 ай бұрын
What Chapsas?
@RockTheCage55
@RockTheCage55 Жыл бұрын
Nick is the "Keep Coding" podcast still done? noticed you haven't had an episode since March.
@nickchapsas
@nickchapsas Жыл бұрын
Resuming in September
@p0z1ck
@p0z1ck 11 ай бұрын
span is only ignored because it can't be used in real world (async everywhere) applications
@stefano_schmidt
@stefano_schmidt Жыл бұрын
is it technically the same as slices in Go?
@obinnaokafor6252
@obinnaokafor6252 Жыл бұрын
Technically, yes.
@_maxt
@_maxt Жыл бұрын
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 Жыл бұрын
every struct will be boxed if used as interface, so AFAIK it's still the fastest way to fulfil contract requiring IList
@Zullfix
@Zullfix Жыл бұрын
Rather than boxing with the interface, use the interfaces for generic type constraints.
@der.Schtefan
@der.Schtefan Жыл бұрын
When a serious and revered KZbinr gives up his day job for YT content, and starts having thumbnails like LinusTechTips, it's time to unsubscribe.
@cryptoeric24
@cryptoeric24 Жыл бұрын
LOL, Richard
@user-di8kl4cc5u
@user-di8kl4cc5u Жыл бұрын
CSharpians
@rogerstich7603
@rogerstich7603 Жыл бұрын
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 Жыл бұрын
Yes there is. There is a "Span video: " part pointing to the Span video
@rogerstich7603
@rogerstich7603 Жыл бұрын
@@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.
@Rafael-vn2bo
@Rafael-vn2bo Жыл бұрын
Great 👍
@jameshancock
@jameshancock Жыл бұрын
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 Жыл бұрын
No, they are still useful
@jameshancock
@jameshancock Жыл бұрын
@@anm3037 Since span literally does exactly what these do, why?
@Kwpolska
@Kwpolska Жыл бұрын
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 Жыл бұрын
@@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 Жыл бұрын
@@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
@Kevinf63
@Kevinf63 Жыл бұрын
The Dometrain course is hitting an error when attempting to purchase after card authorization.
@euphonesse55
@euphonesse55 Жыл бұрын
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
@Ashalmawia
@Ashalmawia Жыл бұрын
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.
@dmitrykim3096
@dmitrykim3096 Жыл бұрын
Pute c# is safe but sacrifices efficiency cause it uses morr allocationes and copying.
@qj0n
@qj0n Жыл бұрын
that's where types like Span or ArraySegment come handy as they're both safe and efficient
@dmitrykim3096
@dmitrykim3096 Жыл бұрын
You know what? I rememeer in Pascal we had assembler inline code to make it faster. Spans are like an inline c++ in c#
@bdcp
@bdcp Жыл бұрын
Why can't they do this shit internally in Linq
@Zullfix
@Zullfix Жыл бұрын
It would be really cool to have a SpanAction version of LINQ
@Marywqq_
@Marywqq_ Жыл бұрын
First
@tidus93
@tidus93 Жыл бұрын
Thank you for the awesome C# content
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 253 М.
6 C# Mistakes Microsoft Wants You to Fix
12:57
Nick Chapsas
Рет қаралды 61 М.
The New Option and Result Types of C#
15:05
Nick Chapsas
Рет қаралды 63 М.
Don't Use Polly in .NET Directly. Use this instead!
14:58
Nick Chapsas
Рет қаралды 59 М.
When RESTful architecture isn't enough...
21:02
Dreams of Code
Рет қаралды 275 М.
"I Hate Agile!" | Allen Holub On Why He Thinks Agile And Scrum Are Broken
8:33
Why Most Programmers DON'T Last
18:56
Thriving Technologist
Рет қаралды 306 М.
The 3 Biggest Mistakes of Object Mapping in .NET
11:33
Nick Chapsas
Рет қаралды 64 М.
Stop Using IEnumerable The Wrong Way in .NET! | Code Cop #019
10:10
3 .NET "Best Practices" I Changed My Mind About
10:16
Nick Chapsas
Рет қаралды 103 М.
KMP vs. Flutter - Who Will Win The Cross-Platform Battle?
16:19
Philipp Lackner
Рет қаралды 46 М.