C# programmers - be very careful with that array! C++ programmers - what is careful?
@billycrooks8401
9 ай бұрын
C programmers: safety?
@ivandrofly
8 ай бұрын
Ahah good one
@jongeduard
2 ай бұрын
Rust programmers, C# very safe then? Exposing the internal buffer should take ownership over it and no-one else can mutate it meanwhile. 🧐
@LCTesla
Жыл бұрын
Advice in the category of "this is cool but NEVER do this"
@lexpsd
9 ай бұрын
if your list is ensurely not changing at the moment, then there is nothing to actaully worry about
@tPlayerioT
7 ай бұрын
i have no idea whats the point of it tho
@bob-xm7ny
6 ай бұрын
@@lexpsd you can never know what will change later. This is a landmine waiting to be stepped on.
@c.Orange
3 ай бұрын
@@tPlayerioTpeople keep repeating "dont do this" as it wont throw an exception if the list changes. But this can be used as a feature. as it is a feature of it. In game development you may want to iterate over the span of a really long list to optimize things, and mutate the list while it is already being iterated over to make things performant. In some cases this is the best option ive found as it is just simple and works. yes, absolutely do not do this where you aren't meant to, but do use it when you have alot of items where you either know the list doesnt mutate, or when you specifically want to mutate the list purposefully while iterating the span.
@marino3034
2 ай бұрын
Why specifically?
@EdKolis
Жыл бұрын
Phew, I thought you were going to use reflection to get the internal list!
@lexpsd
9 ай бұрын
reflection is hella slow to use it in this context
@SimpMcSimpy
Жыл бұрын
It's faster but you have to be careful. Any modification will create a new internal array and your span will no longer point to the right element in the memory. This is useful ONLY if you work with lists that have static pointer and have to loop huge arrays (I would say order of 500 million to billion or more). DO NOT do this for small arrays !!!! Difference is measured in billionth part of a second.
@denislobov8248
Жыл бұрын
Thank you, sir, I'm happy the world has people like you.
@petrusion2827
Жыл бұрын
Saying "Any modification will create a new internal array" is misleading, the correct thing to say would be "Any modification *could* create a new internal array" (for example creating a new list, then adding 1000 elements only reallocates the array 7 times, not 1000 times). That being said though, you should still obviously never mutate the list while you're working with the span. What do you mean static pointer? Also, while I agree you shouldn't start putting this everywhere, saying it barely makes any difference is doing a disservice to spans. In a situation where you have a list of structs with multiple fields and you only need to edit one of those fields for each struct, spans are going to be much faster (20x faster in fact in my testing with a 4-double struct) because they can loop without needless bounds checking and can loop on references to the structs themselves instead of copying data back and forth. If looping the list is a *hotpath* then the span will help a lot even for small lists. You argue this speedup is only worth it on large arrays but for those you're getting bottlenecked by the speed of RAM anyway (in my testing for 10, 100 and 1000 elements the speedup was 20x, for a million elements it was 4x).
@denislobov8248
Жыл бұрын
@@petrusion2827 A lot of information, thank you as well. Can you clarify the static pointers topic, if that's okay?
@petrusion2827
Жыл бұрын
@@denislobov8248 The original comment by AzhKumb stated "This is useful ONLY if you work with lists that have static pointer". In my reply, I'm asking what they meant by that.
@GameDevNerd
Жыл бұрын
You just described game programming and Unity perfectly 🎮 🫠
@leulgirma
Жыл бұрын
it would be nice to add the senario we should use this type of techniques.
@smwnl9072
Жыл бұрын
Thank you Nick for improving our C# skills!
@yuitachibana8829
Жыл бұрын
Look like a potential for Undefined Behavior in Managed Language
@minnesotasteve
Жыл бұрын
Premature optimization. You really shouldn’t be doing this unless you are in the 1% of 1% of special case applications.
@nickchapsas
Жыл бұрын
It's a technique showcase, not a recommendation.
@minnesotasteve
Жыл бұрын
@@nickchapsas you don’t realize how many junior developers I have to deal with who are convinced things like this is faster. Or we should have all static methods because it’s faster. Or any number of these things they see on the internet. It results in utterly unreadable and untestable code.
@TheBuzzSaw
Жыл бұрын
It's also an interop issue. You cannot pass a list to a method that only accepts spans. This gets around that and avoids the issues of sudden mutation (because methods interacting with spans are blind to the list).
@askarmoslehipoor171
Жыл бұрын
thanks bro 💙
@StephenOwen
10 ай бұрын
Is span of T much faster than list or ienum of T? If so, have you done a video about when it’s faster or why?
@shafialanower3820
4 ай бұрын
I have noticed in most cases AI to be helpful in explaining things like code or concepts than do the whole problem for you. First example, if you ask ai to solve one of the beginner exercise in skill foundry you will notice they solve it under program classic and static keyword which can confuse the beginners even more
@JimmieZaccai
4 ай бұрын
What is the use case for this? Why should it be preferred?
@divanvanzyl7545
Жыл бұрын
How do you see that "IL code"? Is that an VS extension?
@TommyTheITGuy
Жыл бұрын
Why would you want to iterate through a Span instead of a List/Array?
@nickchapsas
Жыл бұрын
Because its faster, especially with lists
@artiartem
2 ай бұрын
@@nickchapsas Why is this faster?
@sporksto4372
2 ай бұрын
@@artiartemBecause it offers less abstraction.
@IamPali2024
10 ай бұрын
I’m sick of these guys “tips and tricks” that never represent any real world scenarios
@astrahcat1212
8 ай бұрын
Is ToList() and ToArray() bad?
@smwnl9072
8 ай бұрын
They create a new collection. Thus consuming more CPU & memory.
@ad9291
Жыл бұрын
how about just don't do this at all
@Sweenus987
11 ай бұрын
How exactly is it faster? Not doubting just wondering how it works. I would've thought that with C# lists being contiguous in memory it would be about as fast as you can get
@smwnl9072
8 ай бұрын
AFAIK C# lists are not contiguous in memory. Arrays are.
@Sweenus987
8 ай бұрын
@@smwnl9072 After double checking, it looks like it's value types that are contiguous but reference types only the references stored in the list are contiguous
@vytah
8 ай бұрын
If you're using iterators, they check for concurrent modifications of the collection. If you use indexing, it needs to reread the array pointer each iteration. Using a span avoids both of those operations.
@bob-xm7ny
6 ай бұрын
That will never get through a code review.
@kyryllvlasiuk
Жыл бұрын
Haha, I would say this is a benefit, that it won't throw an exception.
@SimonClarkstone
Жыл бұрын
But you'll silently get a wrong answer, which sounds worse. (Or right answer, depending on the circumstances.)
@Aethenthebored
Жыл бұрын
"Fail fast, fail early" Not knowing there's a problem is really really bad, always.
@sotsch9280
Жыл бұрын
Sometimes you are talking too fast and too slurred...
@denislobov8248
Жыл бұрын
Hmmm, I can understand him just fine in this one, but sometimes I miss a word or two as well.
@petrusion2827
Жыл бұрын
If you're having trouble understanding, thats what the subtitles are for
@xury2426
8 ай бұрын
If you have 10 items in list , why care ?
@smwnl9072
8 ай бұрын
No it wouldn't matter if it's only 10 items. However if your collection runs to the thousands or even millions.. There's also the side of applying best practices in your coding. If you are already doing this with 10 items, then when it scales to the millions, your code would just/still work.
@vifvrTtb0vmFtbyrM_Q
Жыл бұрын
Wait, span is a structure pushed onto the stack. The array in the list is on the heap. When you convert a list to a span, clr must copy the bytes from the heap to the stack. Right ? And for the CPU, access to the stack or heap is the same thing. When the CPU predictor realizes that a memory address is in a register, it copies the bytes into the cache. Where can you profit from performance with this type? The only benefit of arranging data on the stack, it seems to me, is that you no need to call the memory allocator and that after exiting the function, the data is automatically lost.
@FractalWanderer
Жыл бұрын
It is my understanding that if you define a span with an already existing implementation of an array, no new array is allocated, and the span acts as a window into the array.
@vifvrTtb0vmFtbyrM_Q
Жыл бұрын
@@FractalWanderer So why in MSDN says: "Span is a ref struct that is allocated on the stack rather than on the managed heap" ?
@FractalWanderer
Жыл бұрын
@Алексей Иванов It is, and I'm no expert, but my interpretation was that the pointers that define the window into the array are what would be allocated on the stack, and that is what might be considered the "span" when you define it with an already existing array, although it may be the case for heap allocated arrays that it does reallocate memory. After doing a bit more reading though, I do not think it is the case that a memory reallocation occures. The "span" is just a window into the array with two pointers that define the edges of the window. Spans themselves are never allowed to be elevated to the heap, so you are absolutely correct about that
@Moon-D0G
Жыл бұрын
Dam why c# is so complicated
@xouxoful
Жыл бұрын
Compared to what?
@midewestmond9442
8 ай бұрын
Compared to language like rust in 2023, visibility modifiers should be inferred by now, but c# like being explicit
@JimmieZaccai
4 ай бұрын
I'm learning it and it's not. This is something that looks confusing to me also as a beginner. Stick with Microsoft developer channel on yt for learning. It's practical
Пікірлер: 63