Stackalloc and Spans

  Рет қаралды 8,456

Coding Tutorials

Coding Tutorials

Күн бұрын

Allocating memory on the heap can slow a program down by creating work for the .NET garbage collector. In C# we can improve performance by using the stack instead.
Source code available at: github.com/JasperKent/Spans-a...
Topics include:
- Referencing contiguous memory with Span and ReadOnlySpan
- Allocating on the stack with stackalloc
- Writing high performance methods with zero heap allocation
- Avoiding the need for unsafe code
- Fast text parsing

Пікірлер: 28
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
Do you have performance problems because of heap allocation? Let me know in the comments. Source code available at: github.com/JasperKent/Spans-and-stackalloc Remember to subscribe at kzbin.info/door/qWQzlUDdllnLmtgfSgYTCA And if you liked the video, click the 👍.
@RiversJ
@RiversJ Жыл бұрын
My viewpoint is that heap alloc is pure evil and should only be done as a setup step and almost never as part of routine runtime unless storing data. But I'm a graphics/rendering programmer so that means my environment is rather radically different from the norm!
@PeacefulMindss
@PeacefulMindss Жыл бұрын
I haven't seen anyone explaining Spans this simple and clear way, thank you Sir.
@meredoth2725
@meredoth2725 Жыл бұрын
Amazing video as always! A small correction, at 8:24, you say that a list may not be a contiguous space in memory, actually the list will always be a contiguous space in memory because it is just a wrapper over the c# array, the reason is that when the list grows bigger than its initial length , it will create a new array and copy the old one, but the span will still point to the space in memory where the previous array was . That's why we have the CollectionsMarshal.AsSpan(List) Method, but is only safe to use when we are sure that the list won't relocate the array.
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
Absolutely right.
@subindavid4343
@subindavid4343 10 ай бұрын
This is what is called "Perfection and Dedication". Thank you sir.
@alangamedeveloper7820
@alangamedeveloper7820 Жыл бұрын
Your videos are awesome. I wish everybody would watch them and update their C# knowledge to modern practices. Unfortunately many people are happy to bump .NET to 7 and C# to preview but didn't bump their coding so I still see recent C# that looks very old. Also, in your fast parser you could very easily determine the starts/ends of each number and pass just that section of the Span to the double parser. That should definitely yield more time improvements without all that copying. Keep up the awesome work!
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
Yeah, there's more room for optimization, but getting things off the heap is the big one.
@akeemaweda1716
@akeemaweda1716 11 күн бұрын
Sooooo clear. Thanks a lot for this.
@mihailpeykov
@mihailpeykov 14 күн бұрын
All was good and informative up to the point you implement the "fast" parser using spans. That one was not good. You don't need to copy each number's characters to a stack allocated buffer just to pass them to double.Parse(Span) - you could just do it with spans pointing inside the original string. That would avoid the risks of stack overflow you mentioned and will be slightly faster.
@shoooozzzz
@shoooozzzz 4 ай бұрын
So happy I found your channel! Great video. I plan on subscribing and going through your backlog as this is a great explanation of Span
@robertmrobo8954
@robertmrobo8954 Жыл бұрын
A very good example to show to a junior when explaining how evil early optimazation can be. Just as the speaker said, only do this when you really-really have to. 😊
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
Absolutely. I've never needed to do this is real life.
@jphvnet
@jphvnet 12 күн бұрын
Finally cs gets c++ style hard to read when talking about optimized code... Nice
@Kolmix15
@Kolmix15 9 ай бұрын
I love your content! It's so clearly explained. Thank you sir
@mumk
@mumk Жыл бұрын
Thanks sir, your explanations are crystal clear. Absolutely loving the video
@davidwhite2011
@davidwhite2011 Жыл бұрын
Another great edition of Coding Tutorials.!
@Schnickalodeon
@Schnickalodeon Жыл бұрын
Once again a perfectly explained video :) Thank you!
@dizmo..
@dizmo.. 7 ай бұрын
Sir, this tutorial is amazing, just like your costume!)
@nouchance
@nouchance Жыл бұрын
THANK You Sir
@rauberhotzenplotz7722
@rauberhotzenplotz7722 5 ай бұрын
Are you sure that you need 'number.Fill' at all? After the stackalloc, 'number' should contain only #0 chars, if you don't have a SkipLocalsInit attribute. You could pass a slice of 'number' between zero and 'pos' to double.Parse.
@10Totti
@10Totti Жыл бұрын
Thanks!
@pharoah327
@pharoah327 Ай бұрын
How can we return a Span from a function? Or any value type that has more than one field in it? In my understanding, when we return from a function, we pop the activation record, then pop the return address and follow it back to the caller. Since this pops the span from the stack, how do we use it in the calling function? With heap allocation, we can store a pointer in a register and use that to refer to the memory. Yet if we store a stack pointer to now unallocated memory, that wouldn't be good. So I'm really confused on how this would be used if returned from a function.
@aamirali8114
@aamirali8114 6 ай бұрын
i couldn't understand the benefit of Span if i am able to modify in copy array and changes are reflected in original array too then whats the use i want my copied array changes shouldn't be modified in original array.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 6 ай бұрын
The benefit is that you save the performance overhead of doing a copy. The modification is a side-effect that you may or may not want. If you don't want it, you can either use a ReadonlySpan and prevent changes at all, or take the performance hit of doing a copy.
@10Totti
@10Totti Жыл бұрын
Do you use also Blazor ?
@CodingTutorialsAreGo
@CodingTutorialsAreGo Жыл бұрын
Sometimes, though it's not as widely used as things like Angular or React.
The Dispose Pattern
16:28
Coding Tutorials
Рет қаралды 9 М.
Understanding .NET C# Heaps (Deep Dive)
28:23
Raw Coding
Рет қаралды 12 М.
WHY THROW CHIPS IN THE TRASH?🤪
00:18
JULI_PROETO
Рет қаралды 9 МЛН
2000000❤️⚽️#shorts #thankyou
00:20
あしざるFC
Рет қаралды 13 МЛН
Why You Should Always Help Others ❤️
00:40
Alan Chikin Chow
Рет қаралды 98 МЛН
When someone reclines their seat ✈️
00:21
Adam W
Рет қаралды 24 МЛН
The Fastest Way to Modify a List in C# | Coding Demo
10:30
Zoran Horvat
Рет қаралды 20 М.
The fastest way to iterate a List in C# is NOT what you think
13:42
Nick Chapsas
Рет қаралды 154 М.
How to use enum and flag enum in Unity part I
13:35
El Profesor Kudo
Рет қаралды 1,3 М.
Blazor RenderFragment
23:11
Coding Tutorials
Рет қаралды 1,1 М.
.NET Core Garbage Collection
14:54
Coding Tutorials
Рет қаралды 21 М.
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 759 М.
C# 12 Spread Operator Performance Pitfalls
12:53
Coding Tutorials
Рет қаралды 1 М.
Where are types allocated in .NET and why people get it so wrong
14:35
Don’t waste memory with collections | .NET Tips 1
0:50
Nick Chapsas
Рет қаралды 127 М.
#26 Stack And Heap in Java
12:37
Telusko
Рет қаралды 191 М.
WWDC 2024 - June 10 | Apple
1:43:37
Apple
Рет қаралды 10 МЛН
Где раздвижные смартфоны ?
0:49
Не шарю!
Рет қаралды 783 М.
ВСЕ МОИ ТЕЛЕФОНЫ
14:31
DimaViper Live
Рет қаралды 61 М.
i love you subscriber ♥️ #iphone #iphonefold #shortvideo
0:14
Si pamerR
Рет қаралды 2,5 МЛН
cool watercooled mobile phone radiator #tech #cooler #ytfeed
0:14
Stark Edition
Рет қаралды 9 МЛН
How To Unlock Your iphone With Your Voice
0:34
요루퐁 yorupong
Рет қаралды 20 МЛН