How reflection changes will make your apps faster in .NET 7

  Рет қаралды 36,775

Nick Chapsas

Nick Chapsas

Жыл бұрын

Check out my courses: dometrain.com
Become a Patreon and get source code access: / nickchapsas
Hello everybody I'm Nick and in this video I will show you the updates that reflection got in .NET 7 that make it perform up to 4 times faster in some scenarios.
The pull request: github.com/dotnet/runtime/pul...
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet #reflection

Пікірлер: 76
@conbag5736
@conbag5736 Жыл бұрын
Interesting. I believe that Blazor uses Reflection pretty heavily. I'm guilty of making my interfaces very modular - creating lots of small reusable components. However, they say that you should try limit your use of Components because each time they are rendered, reflection is used under the hood to set the values of component parameters. In theory, this should be very good news for me, and Blazor in general.
@figloalds
@figloalds Жыл бұрын
Nice to see this getting a boost, it will definitely improve my micro-orm construction time, so I assume it will also improve all ORMS and mappers in general across the board too, which is NEAT
@willinton06
@willinton06 Жыл бұрын
It’s NET
@diadetediotedio6918
@diadetediotedio6918 Жыл бұрын
Why not using source generators for your ORM and remove the reflection dependency?
@figloalds
@figloalds Жыл бұрын
@@diadetediotedio6918 I don't like it, it's one of the core reasons I moved away from Entity Framework and decided to make my own micro-orm
@jradplowman
@jradplowman Жыл бұрын
Your easter eggs are so good and so subtle! Love it 😀
@kaiserbergin
@kaiserbergin Жыл бұрын
Wow this is super helpful for one of my projects. It uses a lot of reflection with ctors and this is very exciting. Thanks for sharing!
@MicroLosi
@MicroLosi Жыл бұрын
Usually I stop videos from other authors when patreons list appear. But in this case - I wait all way throught the list to hear this magic "keep coding" ))))
@nothingisreal6345
@nothingisreal6345 Жыл бұрын
Thanks for the update and video. Very important improvement indeed. Many libraries use creating instances by type names. IMHO calling methods via reflection is maybe less frequently used.
@krccmsitp2884
@krccmsitp2884 Жыл бұрын
Very nice, thanks for showing!
@williamliu8985
@williamliu8985 Жыл бұрын
cool, really like the performance improvement bring in reflection and generic area!
@Anequit
@Anequit Жыл бұрын
Very impressed! Good job .net team
@angelgabrielvaldessarduy7566
@angelgabrielvaldessarduy7566 Жыл бұрын
This is amazing. Thanks!👽
@nitrous1001
@nitrous1001 Жыл бұрын
Working on a project that makes use of LINQ Expression. Although I do need to use reflection initially for caching purposes. This is quite a benefit.
@gunnarliljas8459
@gunnarliljas8459 Жыл бұрын
Indeed. I expect that to be a good way, even after .NET 7 arrives, whenever the methods are reused a lot. The performance benefits can be massive. In the example given, the improvement over plain reflection was 25-200x. Yes, times. Quite often it can also be further improved by using FastExpressionCompiler (Nuget)
@TonoNamnum
@TonoNamnum Жыл бұрын
thanks for the great video! it's rare to change a private variable. there are a lot of cases when the dynamic keyword could substitute reflection. I wonder how will this compare with performance.
@pqsk
@pqsk Жыл бұрын
Amazing stuff.
@lalibi
@lalibi Жыл бұрын
Hey Nick, did you have the chance to benchmark the built-in DI, with the latest preview? Does it benefit from those improvements?
@davidwilliss5555
@davidwilliss5555 Жыл бұрын
I'd like to see benchmarks on PropertyInfo reflection. I use that a LOT and it would be nice to speed that up
@gunnarliljas8459
@gunnarliljas8459 Жыл бұрын
It should be similar, since properties are just methods. If you set the same properties often, you should probably build a Func or an Action, using LINQ expressions.
@antonmartyniuk
@antonmartyniuk Жыл бұрын
Great news!
@protox4
@protox4 Жыл бұрын
Interesting, but does that performance improvement also exist in AOT? IL emitting typically doesn't work in AOT. I'm still waiting for the fast reflection (new APIs) to make it into the runtime (the issue was put on future roadmap, unfortunately).
@shingok
@shingok Жыл бұрын
That will help to clean a lot of code that is using lambda expressions and il emit for performance reasons
@mjuniorjr5942
@mjuniorjr5942 Жыл бұрын
Hey Nick! I appreciate all your youtube work. Can I ask something? Can you please make a video or video series about all about authentication with database. With and with your own authentication handler, cookies and jwt, with middleware and with your own middleware, what's claims, identity, principal, security context, roles, how we store these in database, how we receive principal from frontend, why they need call AddAuthentication and then call AddCookies for example. Why we need Identity, what's oauth, what's identity server, what's mfa, external providers and why all of this stuffs are different and what's is different in web api, blazor, mvc, and in all .net template. And how this get evolved. It's very confused for me, and I think there's so many people that thinks confused about authentication as well. Thanks!!!
@ahmedmedo7642
@ahmedmedo7642 Жыл бұрын
Hi, i want to begin learning about .net and i want an advice about how to begin from which version and what book do you recommend to begin with as a beginner
@Brodeon
@Brodeon Жыл бұрын
Can you benchmark yarp and compare it to for example nginx? I am quite curious how YARP is fast. Since nginx is written in C we can of course assume nginx will be faster but how much faster than yarp
@danielschmid8530
@danielschmid8530 Жыл бұрын
Neeto, I just installed the Visual Studio preview to try out Maui and if this monster benefits of something it's reflection. Same for XAML in WPF oh lord I might just pick that one up again for funsies as well.
@baracek8797
@baracek8797 Жыл бұрын
I once had a 3rd party library of proprietary closed source code that had a memory corruption issue. The company refused to believe it a problem even when I sent them the decompiled code with the problem in it and pointed out the line that needed changing. I used reflection to get the field that had the problem and pin it so that it wasn't moved by the GC when unmanaged code had a pointer to it. That is the only case where I thought reflection of private members was necessary and warranted.
@alexisfibonacci
@alexisfibonacci Жыл бұрын
Hmmm...
@meydanozeri4322
@meydanozeri4322 Жыл бұрын
I'm curious on what would be faster, manually emitting IL myself or using the improved reflection of .NET 7. Maybe you should have tested that as well
@sealsharp
@sealsharp Жыл бұрын
Sweet.
@MrRobin4444
@MrRobin4444 Жыл бұрын
nice, almost missed the improvement. it would be interesting how that improves enums. (since they are almost entirely reflection based) *Wouldn't null instead of an empty array be even faster?
@vm123
@vm123 Жыл бұрын
I did some testing by my own and found out that getting property value by reflection in loop is way faster than compiling expression on the fly (to lambda func). At about a million loop size expression compiling solution catches up the reflection based solution times. Is is possible or my test method could be flawed?
@alexgriffith5161
@alexgriffith5161 Жыл бұрын
my irc bot library uses a lot of reflection this is good news for me
@dadcraft9949
@dadcraft9949 Жыл бұрын
I happened to be working on something in this area just now and I'm curious how the performance of the emitted IL compares to cached delegate performance. The delegate approach is certainly much faster than calling the get method directly via reflection, if you're making enough invocations to account for the overhead of constructing the delegate. To illustrate what I mean, calling these delegates achieves the same performance as calling a virtual method, which is significantly faster than calling Invoke directly on the MethodInfo (at least before .NET 7...) var getterDelegateType = typeof(Func).MakeGenericType(propertyOwnerType, propertyInfo.PropertyType); var setterDelegateType = typeof(Action).MakeGenericType(propertyOwnerType, propertyInfo.PropertyType); var getterDelegate = propertyInfo.GetGetMethod().CreateDelegate(getterDelegateType); var setterDelegate = propertyInfo.GetSetMethod().CreateDelegate(setterDelegateType);
@DJosh-cs5vx
@DJosh-cs5vx Жыл бұрын
Thanks
@_Aarius_
@_Aarius_ Жыл бұрын
I'm working on a personal project that relies on reflection a lot, so any performance improvements are always welcome to me.
@TheMonk72
@TheMonk72 Жыл бұрын
Now do a Linq Expression version. Create an action to call the set method compile it and see how fast the call is. It surprised me.
@adamponka6404
@adamponka6404 Жыл бұрын
I'm dissapointed you didn't compare it with CreateDelegate method cause that's the one most heavily used for reflection optimization. Do we still need it or is Invoke optimized enough to forget about it
@nickchapsas
@nickchapsas Жыл бұрын
If I remember correctly CreateDelegate cant replace all reflection calls. It can replace the PropertyInfo ones and I have a video on that but I’m not so sure on MethodInfo and ConstructorInfo
@tarsala1995
@tarsala1995 Жыл бұрын
Hi Nick, just want to inform you that new StackOverflow survey results came out. You might want to have some commentary on that :)
@marcelofernandes8005
@marcelofernandes8005 Жыл бұрын
Hey Nick. Really enjoy your content. Especially the benchmark stuff. I was curious if the Activator.CreateInstance also has any performance improvements in .Net7? Since it is usually the fastest way to create an instance with an empty constructor, but trails being the GetConstructorInfo method when the constructor has parameters....
@nickchapsas
@nickchapsas Жыл бұрын
I actually haven’t investigated this one. Will need to take a look
@onyxgc2568
@onyxgc2568 Жыл бұрын
I used to cached the object properties to make reflection run faster.
@brianm1864
@brianm1864 Жыл бұрын
All of these great new features/enhancements in .NET 7 are making me sad, as we will probably skip 7 and wait for 8
@sergeybenzenko6629
@sergeybenzenko6629 Жыл бұрын
Which is the right choise for production code, because .NET 7 is not LTS version.
@BillyBraga
@BillyBraga Жыл бұрын
Well, if you can afford the slight man power to upgrade from 7 and 8, it's alright. Non-LTS are not less stable or reliable, only supported for a shorter time.
@sanampakuwal
@sanampakuwal Жыл бұрын
​@@BillyBraga agree with you
@brianm1864
@brianm1864 Жыл бұрын
@@BillyBraga Unfortunately it is a manpower issue. Too much to do, too little time.
@sanampakuwal
@sanampakuwal Жыл бұрын
@@brianm1864 update from dotnet 6 to 7 is not a big task (you could only argue about manpower if the case was to migrate dotnet framework 4.8 to dotnet 7 btw) at least if the solution is less than ~50 projects
@PetrVejchoda
@PetrVejchoda Жыл бұрын
Is the GetAge allocating because of the Array.Empty method? I would hope it is internally cached.
@nickchapsas
@nickchapsas Жыл бұрын
No it is internally cached
@PetrVejchoda
@PetrVejchoda Жыл бұрын
@@nickchapsas umm, I still can't get over why GetAge is allocating whilst the SetAge is not. Is that like the first time use allocation and then it is cached? I don't see why those two method infos should behave differently. Maybe I am just missing something. I am sorry, I am too lazy to recreate it and investigate that myself, I just wanted to ask if somebody knows out of the box.
@nickchapsas
@nickchapsas Жыл бұрын
@@PetrVejchoda Set age is allocating because it is boxing the value type in the object array
@julienc.8562
@julienc.8562 Жыл бұрын
I wonder how it compares to Fastmember
@raingelfer
@raingelfer Жыл бұрын
Nick, I 'm pretty sure, you may pass a target dotnet version directly into benchmarks.
@nickchapsas
@nickchapsas Жыл бұрын
You can but I couldn't find the .NET 7 support yet
@possessedllama
@possessedllama Жыл бұрын
If someone is using reflection to access a private class or member in another library, I'd suggest they write a wrapper around it and unit test the wrapper extensively.
@liontalha7383
@liontalha7383 Жыл бұрын
Anyone have any idea how to add watermark to each page of doc or pdf file and how to add a text file in zipped file and zip again. Any reference will be useful
@corinnarust
@corinnarust Жыл бұрын
I'm a Java developer. *Proceeds to the next video*
@IgnoreSolutions
@IgnoreSolutions Жыл бұрын
Unity: *sweats excitedly*
@enfy1337
@enfy1337 Жыл бұрын
unfortunately it'll be at least 2 more years until unity supports .NET 7
@mariocamspam72
@mariocamspam72 Жыл бұрын
Unity uses Mono + il2cpp for C#, not .NET
@IgnoreSolutions
@IgnoreSolutions Жыл бұрын
@@mariocamspam72 Don’t you think the Mono folks will see this and follow suit? Bit pedantic
@mariocamspam72
@mariocamspam72 Жыл бұрын
@@IgnoreSolutions Can Mono just copy the implementation from .NET runtime though? Wouldn't it be a licensing nightmare?
@IgnoreSolutions
@IgnoreSolutions Жыл бұрын
@@mariocamspam72 No…..but they can implement in the runtime like Microsoft is for .NET. If .NET 7 spec states that these changes should be made to reflection, then Mono will have to implement them to maintain compatibility.
@MrKyriotetes1
@MrKyriotetes1 Жыл бұрын
Do become Nick patreon can get more video?
@igorthelight
@igorthelight Жыл бұрын
You can see all unavailable videos (blurred) BEFORE subscribing to a someone's Patreon ;-)
@aj.arunkumar
@aj.arunkumar Жыл бұрын
mic drop*
@justwatching6118
@justwatching6118 Жыл бұрын
I knew there will be 69 with age :D :D
Microsoft fixed my biggest complaint with Minimal APIs in .NET 7
8:12
You are mocking the HttpClient the wrong way
13:56
Nick Chapsas
Рет қаралды 48 М.
ДЕНЬ РОЖДЕНИЯ БАБУШКИ #shorts
00:19
Паша Осадчий
Рет қаралды 6 МЛН
100😭🎉 #thankyou
00:28
はじめしゃちょー(hajime)
Рет қаралды 56 МЛН
КАРМАНЧИК 2 СЕЗОН 6 СЕРИЯ
21:57
Inter Production
Рет қаралды 505 М.
How IEnumerable can kill your performance in C#
11:02
Nick Chapsas
Рет қаралды 112 М.
Why .NET's memory cache is kinda flawed
14:13
Nick Chapsas
Рет қаралды 54 М.
I Created A Mobile App Using These Simple Tools!
10:48
Simon Grimm
Рет қаралды 599 М.
What are record types in C# and how they ACTUALLY work
15:36
Nick Chapsas
Рет қаралды 116 М.
The New .NET 9 HybridCache That You Must Upgrade To!
14:34
Nick Chapsas
Рет қаралды 41 М.
The NEW caching you should be using in .NET 7
18:17
Nick Chapsas
Рет қаралды 75 М.
STOP Learning These Programming Languages (for Beginners)
5:25
Andy Sterkowitz
Рет қаралды 640 М.
The CORRECT way to implement Retries in .NET
17:01
Nick Chapsas
Рет қаралды 85 М.
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 247 М.
The .NET dependency injection methods you are not using
11:49
Nick Chapsas
Рет қаралды 91 М.
ДЕНЬ РОЖДЕНИЯ БАБУШКИ #shorts
00:19
Паша Осадчий
Рет қаралды 6 МЛН