Комментарии:
For the avoidance of doubt: The technique shown in the video is still considered unsafe (like I mentioned in the video's conclusion) and should not be used unless you really know what you're doing. The reason why it is included in the video is because it doesn't require the use of the unsafe keyword hanse why I said "unsafe code territory" in the beginning. This doesn't make it safe. It just makes it a candidate to compare with other things that don't require the unsafe keyword to be compiled. If we were to use the unsafe keyword then I'd include pointers too and that would be a different story.
ОтветитьWill there be an increase in speed if you assign the "length" of the collection to a variable (before the for loop) and use this variable in for expression, instead of getting the length of the collection every time
ОтветитьGetPinnableReference()
ОтветитьNice video! How does the performance compare to optimized unsafe context code? I landed up in unsafe coming from c++ and that boosted my algorithms. But I'm worried people after me will not be familiar with unsafe context. This is a little nicer syntactic wise(although it still is not for everybody) and so I'm all into span and co. when performance stays maximal.
ОтветитьHow can i see the Viewer for the low level code? Thanks
Ответитьsince the span does not take into account new changes to the list, it is a natural cool way to perform Gauss Jordan iterative method for solving linear system. I mean Gauss Jordan, not Gauss Siedel.
ОтветитьI really appreciate all your tips and I don't mean to disrespect, but doesn't all this defies the point of .NET? Unless time is a big constraint on optimizing a specific part of the code, a foreach loop will probably get optimized so much in the next iterations of .NET that writing all this will become unecessary, and I had this actually happened to me at multiple occasions (with reflexion for example).
I took the time to write super efficient and elaborate code (ableit very hard to read and to port), all of this to be obliterated by the next version of .NET that made the much simpler version of what I was trying to do not only as fast but also safer with thread concurrency etc... It feels like you're approaching C# and .NET like it's C++ with the STL, I dig it don't get me wrong! But I find it all the more surprising!
Why not Memory<T>?
ОтветитьWhy do i need it? Game development and because i want to :) Ty for info
ОтветитьWouldn't it be cleaner to just declare an unsafe block and do pointer arithmetic? It'd be the same right?
ОтветитьIt still makes sense to use C# for me and not C++ because I only need this in a handful of methods out of 1000s.
ОтветитьIt's basically how we traverse stuff in old fashioned C. Moving the pointer to the next node, moving the array pointer one step further etc
ОтветитьIf the goal is to ultimately using unsafe method why convert list to listofspans, can we not fetch the address of first element of list also just like we did for array ?
ОтветитьMy question is if you aren’t dealing with an array, but ToArray lose the performance advantage?
ОтветитьI'm really curious why a for loop on a span would be faster than a for loop on an array. Seems like Microsoft needs to do some optimizing on their array code.
ОтветитьLove it. Thanks
Ответитьuse nint for indexing with Unsafe.Add for better code gen than int, it will be faster than span that way. So instead of using var i = 0, do nint i = 0
Ответитьthis is just employing pointer arithmetic over contiguous memory locations right?
ОтветитьIsn't that just pointer arithmetic (with a whole bunch of abstraction)?
ОтветитьAnd this is how we going back to raw C pointers.
ОтветитьThis technique reminds me of writing C and C++ in college.
ОтветитьOMG. Firstly I would just go back to simple pointers if I go unsafe anyway. Secondly it only works with an int array as you just add 1 to the address. What about data that uses more space?
ОтветитьThank you!!! All your videos are great!
ОтветитьAnother video title would have been: Youngster meets Pointer, introduces her and doesn't even begin to point out how quickly the car can hit the wall.
ОтветитьC++ at it's best
ОтветитьI’d love to see how will this compare with the Parallel.ForEachAsync from .NET 6.0 henceforth…
ОтветитьReal quick - you are rarely writing code where this actually matters. For sure, don't burn cycles uselessly, but compute cycles are cheaper than brain cycles.
ОтветитьThis is just C code written as C#. Of course it's the fastest, all you need to do is add and deref. The same translates natively to assembly.
ОтветитьHow to write C++ without writing C++
ОтветитьI think it's good to put the knowledge out there and I appreciate the video. However, I feel like there needs to be a strong caveat because beginners and even intermediate devs will want to use these techniques because they are "cool".
My advice is that you should always write correct clear code first. That means, always use foreach loops and only after profiling do you then optimize. Because like the benchmark is showing, you don't really get any tangible benefits unless that code is called 100,000+ times which is true for Framework libraries but rarely true for everyday Line of Business applications. Not to mention the fact that compilers and the JIT are pretty sophisticated and in some cases can automatically do these optimizations like in the case of int[] so in those cases you have uglier code for no benefit whatsoever
I'm sure you already agree with this but you need to make it clear for beginners and intermediate devs who stumble across this
Thank you sir. This may actually prove useful in some of my current projects and I guess Unity game devs might find it useful in certain cases as games often need to be optimized to the millisecond (curious to know hoe it works with Unity gameobjects).
Ответитьseems like the compiler should've been improved, rather than writing hack code.
ОтветитьAll that work to get back to c style pointers...
ОтветитьWhy do you have to random 420 :)
Its 420 Time
Duhhh?
ОтветитьWhy do you try to achieve c++ in c#?
ОтветитьDid you just discover pointer arithmetic? Perhaps MS could invent a new language named D to take advantage of it?
Also, why are you adding the offset every time when you could just be incrementing by the constant sizeof()?
And where do you benchmark the conversions from whatever you have the data loaded into, to a span?
ОтветитьI love how we have to write all this code in these new languages now to do something which lower level languages do by default, Assembly, C, and Pascal have used direct memory pointer operations to iterate through arrays since the dawn of time, but to get c# to do it at the same level you have to write a bunch of code… why can t the language simply be smart enough to use pointers internally automatically… Great video though, its good to know this stuff…
Ответитьsounds like C pointers with extra steps
ОтветитьBut … converting the Array to the Span using the Marshal also takes time being a loop internally iterating the data in the array copying the data into the span. Surely the whole solution will only be more efficient if you have to iterate over the data multiple times 🤷♂️
ОтветитьMan I tried this span method to copy array to another array and it took 5 times longer than normal for loop. Hm?
Ответитьlol I’ll
ОтветитьProof that C# users will do anything at all to avoid C++
Ответитьuse C++ not C# for this!
ОтветитьI like how it starts with lets make fast loop without unsafe code and proceeds with API's that does unsafe operations. Nonetheless it is informative.
ОтветитьGreat video. I usually do fixed and use pointers if I need performance in C#. It performs as fast as C. UNSAFE is only really unsafe for NOOBS who don't know what they are doing, for us in the know unsafe is totally 100% safe :)
ОтветитьIMO all this stuff should be a matter of the compiler. For example the compiler, in many cases, knows which is the real underlying type so, if I use foreach, just because a "fashion" matter, with an array of value types, the compiler should "translates" it with a "for" if it is more efficient.
ОтветитьI like many of the comments are like "pointers seem like details". 😂
Ответить