Avoid this hidden memory allocation in C#

preview_player
Показать описание
Learn about a pretty neatly hidden allocation in c# and how to explore the code so you can find these yourself.

#csharp #dotnet #aspnetcore
Рекомендации по теме
Комментарии
Автор

Boxing (I had to look this up after 20+ years of C#) Boxing is an implicit conversion of a value type to the type object or to any interface type implemented by this value type

DataJuggler
Автор

Thanks for the vídeo 😊. I was the one who asked the question on Twitter.

I came across it while reading the Rider DPA "Fix Memory Issue"

ivandrofly
Автор

I would definitely not have thought about that without accidentally finding it in a confusing benchmark!

thethreeheadedmonkey
Автор

another hidden allocation scenario that i found out about is when you listen to an event.
In the case of "MyEvent += TriggerMethod", the lowered code can produce 2 different results :
- it creates a cached static "Action<T>" field IF "TriggerMethod" is static
- it creates a new "Action<T>" everytime you subscribe to the event if "TriggedMethod" is an instance method, the lowered code give "MyEvent += new Action(TriggedMethod);
So if you wanna avoid the creation of the delegate everytime, just cache it in a field manually and use the field to listen to the event, as in "MyEvent += cachedTriggedMethodField"

Bloodthirst
Автор

Like the video. Love advance coding videos and about performance. Even though it's difficult for me to understand. And that's what I like, so much hello world example on the web. We need advance content. Thanks for taking time to produce your videos.

bicicamuy
Автор

I think this would be a good fit to Advance c# playlist that you have

ivandrofly
Автор

Thank you for all your effort
You advises are much appreciated
Just a question outside the context: do you have a follow up on the packages delivery of your store 🙂

mohammadzakareatfaili
Автор

which extension are you using to view IL code. The one that I use gives me Error: command 'vscode.previewHtml' not found

DeepakSharma-wiwu
Автор

So, if we want generic, but efficient code, we better write implementations for every type (we want to optimize)? Suddenly, C++ templates look actually interesting

guiorgy
Автор

So should I avoid using IEnumerable<T> for the methods that are most likely to be used with lists?

Wherrimy
Автор

I would like to add arrays + unsafe and fixed for extra speed:
int sum = 0;
fixed(int* ptr = &arr[0])
{
int* end = &arr[arr.Length];
for(; ptr != end; ptr++)
{ sum += *ptr; }
}

had to use loops like this everywhere in per-frame loops while making a game engine, and sometimes some AVX math ops for those SIMD gains

Bloodthirst
Автор

Very difficult to follow - the code is too small to read on a laptop screen, and you scroll too fast. You clearly know your stuff, but you might build a larger following if you brush up on your presentation skills?

tullochgorum