Enums in .NET 8 Are FAST, but Mine Are Faster!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick in this video I'm going to show you some massive performance improvements that net 8 brings over Net 7 and especially over net 6 so if you're back in the LTS version you really really want to upgrade and actually show you some performance problems that enams have in general and how you can fix them even if you don't move into net 8 now I've already talked about the inm performance problems and in this video I will do a bit of a recap to understand exactly what those problems are but I will leave a couple of videos in the description if you want to check late what that problem is in depth and why it really happens behind the scenes now net8 does fix most of that so in this video we're going to take a look at all that like of content and you want to see more make sure you subscribe for more training check out my courses on do tr.com all right let shoot a hi here and I'm going to start by just showing you what methods were affected by performance and are being fixed now in do net 8 now I have an enum here representing the day of the week I'm not using the day of the week enam that net has built in intentionally I'm going to explain why later in the video but for now let's first get a Monday so we're going to say day dot Monday we're going to start with that now the simplest thing you might want to do with an inam is actually get its string value there's many cases where this might be relevant and this happens by default in things like logs or anything that automatically converts an inam to some text representation you won't get the int value but you're going to get the string so if I say here to string you're going to get it by saying to string and this will Monday another thing you might have to do when you're working with enams especially in a dynamic context is to check if that enam is defined so using the enum do is defined method getting the name of the enam using the enam dogit name method is another one then you can actually get all the names and all the values in an inam by using the get names and get values methods over here and then last but not least is the triy pass method to try to pause an inum based on it string value and then get out the date and check if it was passed or not so all of these methods are things you will probably be using if you're dealing with enams in any way now here's the problem with these methods up until net 6 they were really really slow and not very memory efficient as well and do 7 made a pretty significant Improvement compared to what we had but the problem was that it was still not good and this led to people like Andrew L for example who has an excellent blog talking about many net things to make new kit packages like the following one over here called net escapades enam generator which actually allows you to do the following it allows you to go to your enum and add an enum extensions attribute and what that does behind the scenes is it actually makes a source generator kick in which will build a bunch of different methods that are basically faster versions of all of these and more M efficient versions because it will just take that enum and try to optimize it however before I show you that what I want to show you is the performance Improvement we have from Net 7 to net 8 in all of those methods now to do that I'm going to go to this Benchmark and this Benchmark will run for both Net 7 and net 8 it's going to use a memory diagnoser and it's going to check things like is defined get named get names get values two string and then tripod so all the methods we saw before we're going to see how performance has improved between seven and 8 now to make sure that this runs correctly I'm going to Target both net 7 and net 8 so I'm going to do this over here and then I'm go to the program.cs say return and then using one NC then similar improvements in the get name one then get names is not that big of a jump from 14 to 9 but get values goes from 319 nond to just 22 and the memory significantly improved then small Improvement in two string and tripods now here's the thing about all those numbers yes they are great yes they show Improvement but they are not perfect and this is where I'm going to save a snapshot of all these results over here and then go back to my benchmarks now what I want to show you is how much this performance could be improved if we were to use and loocks uh net escap page andam generator now to do that I'm going to have to add the enam extensions attribute over here and once I do you're going to see that I get all of these new usages over here which are really The Source generated code for the partial classes and all the extensions generated that will analyze my code at compile time and write this code so e is it this day of the week yes yes yes yes yes and whatever because that's not really what's going to happen behind the scenes with any of these methods now if you want the technical details of how these used to be I do have that link in the description if you want to check my other video we won't focus on that here what we're going to focus is once I actually add this inam extensions what I can do now is have fast versions of all of these methods so I'm going to say two string fast for example and I can say Monday to string fast if I want to do the same for is defined I can say is defined fast and I can say the name of my inam so day and then extensions and that's where the code is generated so I can say is defined Monday and so on then I can get the names so I can say names fast day extensions and then get names I don't need to specify the parameter because it is based already on that type of the inam and then same with the values so day extensions get values delete that and last but not least I have the past fast over here basically the same signature but I'm going to say day one and day two here just to simplify things and that is it now I use the source generator again that n package if you want to use it is called net escapades do inam generators over here and what I'm going to do is add benchmarks for that so benchmarks have been added so all I'm doing here is using the fast versions and I'm going to compare them to the snapshot I have from Net 7 and net 8 let's go ahead and change this to return again and run those benchmarks okay so results are back so let's see what we have here the way I'm going to do this is take a screenshot of this bit and then paste it on that very scientific tool over here called um Ms paint okay let's take a look at this so the is defining Net 7 9.5 NS 1.2 NCS over here but on the fast version 0.73 so even faster and you don't have to upgrade in net 8 now interestingly enough get Nam was 14 NS it went down to 9 but the source generator is 12 so something happened there and I think Andrew can actually optimize this even further because it's no way the source generated version is not faster than the runtime version so careful because this one is actually a bit slower now in terms of memory I should point out they're both the same both 80 bytes now get values from 39 all the way down to 22 and actually all the way down to 4 NCS if you use the fast version same memory efficiency as this one over here the do net 8 so pretty fast but not as good as the source generated version and then the two string which is probably the one you're going to be using the most anyway from 11 to 7 but all the way down to 1.4 and no memory allocated over here so significantly faster over here now in terms of tripods I accidentally cut it off over here but it was 36 NS down to something like 20 whatever and this is now 7.4 again no Ming all of them so as you can see for four out of five you can actually use a s generated version not lose any functionality and gain significant performance without even upgrading to net 8 now why is Microsoft not allowing or not providing an enam Source generator to deal with this well I guess they don't want us to have two ways of doing the exact same thing which is ironic to say because cop has plenty of that but I would love to have a source generator by Microsoft that actually provides this functionality which if they make allows them to tap into more potential features of net that could be internal to fix all that so yes net 8 and enam in net 8 are significantly faster and more memory efficient however I will still link the S generated video down in the description because that's the one I would be recommended yes the performance improvements are great but they're not as good but now I want to know from you did you know about all these performance issues and have you used any of those libraries that do exist to solve them leave a comment down below let me know well that's all thank you very much for watching and as always keep coding
Info
Channel: Nick Chapsas
Views: 36,675
Rating: undefined out of 5
Keywords: Elfocrash, elfo, coding, .netcore, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft, microsoft mvp, .net core, nick chapsas, chapsas, dotnet, .net, .net 8, enums in .net 8, .net 8 performance, enums in .net, enums bad performance, enum bad performance, source generator enum, Enums in .NET 8 Are FAST, but Mine Are Faster!
Id: UBY4Y6AykdM
Channel Id: undefined
Length: 9min 3sec (543 seconds)
Published: Fri Oct 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.