Тёмный

C# 12 Spread Operator Performance Pitfalls 

Coding Tutorials
Подписаться 15 тыс.
Просмотров 1,2 тыс.
50% 1

Visual Studio gives us lots of hints on how to improve our code. Mostly it's right. Sometimes it's dead wrong.
Source code available at: github.com/Jas...
Topics include:
The various ways of creating an empty array
The performance benefits of using a predefined empty array
How all ways of creating an empty string work the same way
Benchmarking of spread operator performance against alternatives
Bad advice from Visual Studio

Опубликовано:

 

4 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 20   
@catalinenache-stoian1733
@catalinenache-stoian1733 Месяц назад
Thank you! I was pretty close to adopt the change that VS suggested and make use of spread operator.
@kurumi690
@kurumi690 10 месяцев назад
Thx for sharing your experience. I think C# developers made spread not just for copy full collection items - it is really powerful and cool feature especially if you work with collections. But as I told, not for copying every single element. VS developers should fix this suggestion or we should have some if condition to do ToArray() behind of scene in spread 😄
@muhammadfathi168
@muhammadfathi168 9 месяцев назад
Awesome channel, very informative videos, thank you for all your effort, you helped me a lot, keep it up please..
@IncomingLegend
@IncomingLegend 9 месяцев назад
thanks for the video! keep up the good work!
@CodingTutorialsAreGo
@CodingTutorialsAreGo 10 месяцев назад
How much does performance matter to you? Source code available at: github.com/JasperKent/Spread-Benchmark Remember to subscribe at ru-vid.com/show-UCqWQzlUDdllnLmtgfSgYTCA And if you liked the video, click the 👍.
@anm3037
@anm3037 10 месяцев назад
It matters to me very well.
@codingbloke
@codingbloke 10 месяцев назад
Thanks for highlighting the performance concerns. Definitely worth knowing if this sort of thing if tight performance is a constraint for your code. I disagree that VS offering a slower but clearer version of the code is a bad thing. Clarity trumps performance unless performance is genuinely needed. Developers working with such constraints should have a good understanding of this stuff. IMO in a code file where the VS suggestion is undesirable a pragma should be added the file to turn that suggestion off with a comment from the developer why they have made the choice. The overwhelming majority of developers will never really need this optimisation. Also later versions of the compiler could identify and optimise this pattern.
@anm3037
@anm3037 10 месяцев назад
I get your point, but I disagree with this populist chant that 'Clarity trumps performance'. They are very equal from my own perspective and it's a good habit to always remember that.
@codingbloke
@codingbloke 10 месяцев назад
@@anm3037 Well putting aside the "chant" and "populist" nonsense. I can understand that different developers will have their own perspective. In some cases micro performance factors such as highlighted in this video make a genuine difference to your bottom line or your user's experience. However, in the vast majority of code being written today it doesn't. This leads to the VS suggestion to use the clearer syntax to be a sensible one. It certainly isn't a "wrong" suggestion as the video intimates.
@anm3037
@anm3037 10 месяцев назад
@@codingbloke You have absolutely no statistical data to the claim that vast majority of code written today are not performance dependent. You are obviously repeating the same rhetoric you have heard over the years. Meanwhile that spread operator is NOT more readable than traditional ways of doing it.
@codingbloke
@codingbloke 10 месяцев назад
@@anm3037^rhetoric" more nonsense. Here is another assertion that will upset you. Most performance sensitive code isn't being written in C#.
@christianj.2581
@christianj.2581 9 месяцев назад
Tbh, I kinda disagree with the spread operator syntax being "clearer" in the first place. If someone who has not kept up with the latest C# language features reads "something.ToArray()", then it is _very_ clear what this piece of code does. If that same person sees "[.. something]", then they - unless they by chance know that syntax from other languages - will have absolutely no clue what is going on. One is intuitive, because all it requires you to know is English. The other is yet another piece of "clever" syntax that will make code more concise for specific people in specific situations, but is overall a net minus in code expressiveness when you take the average C# developer into account. Of course if you write code that is only ever read by yourself or better-than-average skilled people then it doesn't matter.
@natepepin09
@natepepin09 10 месяцев назад
I have a feeling that this is going to get fixed in the next .NET version. Maybe the problem is a bit more complicated than I'm giving it credit for, but I'm pretty sure the solution would involve the compiler checking all the types within spread statement, and if they are all arrays, lowering it to the more optimized version.
@CodingTutorialsAreGo
@CodingTutorialsAreGo 10 месяцев назад
I'd have thought so.
@obinnaokafor6252
@obinnaokafor6252 10 месяцев назад
They said optimisation and performance for collection expressions are coming in C# 13
@foudilbenouci482
@foudilbenouci482 Месяц назад
CopyTo() and ToArray() have also to loop through the array , no ??!! i don t understand sorry
@tak8242
@tak8242 14 дней назад
it won't iterate. It works at the memory address level, copying blocks of memory rather than individual elements.
@foudilbenouci482
@foudilbenouci482 14 дней назад
@@tak8242 how can you see that ? what is the code for that ? thank you
@pzgameforkid9259
@pzgameforkid9259 9 месяцев назад
you has read all books :/ :/ 🥲
@CodingTutorialsAreGo
@CodingTutorialsAreGo 9 месяцев назад
About 60%.
Далее
C# 12 Collection Expressions and the Spread Operator
25:27
.NET 8 Keyed Dependency Injection
16:34
Просмотров 1,4 тыс.
Being Competent With Coding Is More Fun
11:13
Просмотров 89 тыс.
Use Arc Instead of Vec
15:21
Просмотров 147 тыс.
.NET 8 Blazor Rendering
16:03
Просмотров 1,6 тыс.
The Fastest Way to Modify a List in C# | Coding Demo
10:30
C# 12 Primary Constructors
19:44
Просмотров 1,5 тыс.
Blazor RenderFragment
23:11
Просмотров 2 тыс.
Writing C# without allocating ANY memory
19:36
Просмотров 149 тыс.
Just enough C to have fun
39:29
Просмотров 50 тыс.
ARRAYLIST VS LINKEDLIST
21:20
Просмотров 64 тыс.
5 (Extreme) Performance Tips in C#
12:26
Просмотров 75 тыс.