40% faster JSON serialization with Source Generators in .NET 6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody i'm in this video i want to talk about the.6 feature that quite honestly people are not talking enough about in my opinion and that is source generated json serialization and digitalization the reason why this is huge to me is because json is everywhere in our applications from apis to configuration to messaging to eventing everywhere so if we can save some time during this serialization and digitalization we can have huge like actual practical gains in our obligations traditionally json serialization or conversion has happened with either reflection or emitting il code so being able to do that at compile time can really make a difference that's what we're going to show you in this video if you like a lot of content and you want to see more make sure you subscribe reading this notification bell to get all edited when i upload a new video so let's go here and let's create the simplest thing i'm going to create a person class something like that and then let's see how we would traditionally serialize this so you'd have a person equals new person and that would be first name nick of course and then second name you have copilot is suggesting cage from nick cage sure let's go with nicolas cage so you have nicolas cage and if you want to serialize this you're gonna have nick cage text equals jason serializer dot serialize and then person and then let's print it so console.writeline nick cage text and let's just run this and you see the serialized version and if you wanted to customize this for example if you want to have camel case here you can say this but not the right intended and property naming policy is camel case and then just pass it down here i've been testing co-pilot by the way for some time now and it's so good okay so we can run this and now you can see this is countle case and let's just move this into its own file here we go and add the namespace file now let's say we want to do source generated json serialization here how would we do that well it's very easy surprisingly easy all you need to do is create a new class called in my case person json context here you go and this needs to be a partial class because the source generator will inspect that and write the rest of the class data to do the operation and this needs to extend the json serializer context now it complains that we need to implement a few members we don't because once we add the attributes we need the source generator will do that for us so what we need to say is json serializable and provide the type type of person in my case and then just save and this now has created the class behind the scenes and in fact as you can see here this is the code generated by the source generator this is all the serialization stuff needed now because we had camel case serialization we can even customize that we can say json source generation options and you can specify generation mode in our case we're going to go with default if you're only doing serialization not visualization you might want to go with the serialization version or metadata if you're doing both we're going to go with default you can try this in your own time and see what works for you and what doesn't but that's what we care about and then we care about the property naming policy which is camelcase in our case and now this is enough to actually have source generated serialization the way it works is i'm gonna comment this out and i'm gonna go down here and i'm gonna pass we don't need the options anymore what we need to say is person jsoncontext.default.person that's it and now this will hit the json serializer if i run this as you can see we're still getting the same thing but now we used the short generated version of this let's see if we can actually debug into that so i can show you what's happening i'm gonna do that i'm gonna do that yep here we go you can see that this is going into the json serializer i'm gonna go into the context here and i am here a lot of this is still in preview so some things might take some time or might break but you can see that this is where the code is actually running from now this is simple enough and this realization is the same you know you have the text and if you want to do a digitalization you say json serializer dot d serialize you have the text and then you have the this thing and this will do the opposite it will give you back the object let's just see that actually object object that's a keyword i can do that obj so here you go yep we have the person here so it all works now what i want to talk about is performance and i'm going to focus on serialization because digitalization in my test at least didn't actually yield any interesting results it's effectively the same performance i'm still going to have the benchmark included in this video and in the code but the differences will be observable in the serialization at least in my experience so let's go ahead and add benchmark.net in here here we go and then i'm going to just paste the benchmark classes because there's no point in me writing effectively this code 14 times so i've had to do classes the serialization digitalization here's what i'm doing with the serialization side of things this is using camel case options for the old way of doing serialization then i have a randomizer with a seed so the results we're getting are the same every time and then i'm making a list of that person class and i'm generating a thousand of those and then i'm serializing them using first a stream and then a string so the stream is using a utf-8 json writer and the string is just a stream call but with actually encoding this to an actual string that is readable there's different use cases you might want to go with a stream and you don't even have to interface with the actual text at all but if you want to see how the text itself would perform i'll have that here and i'm splitting this in two categories the stream category and the string category and let's see what's the difference for those and then it's effectively the same for digitalization but only from a string to the actual list i'm gonna comment this out and to run this i'm gonna say benchmark runner dot run the serializer no the serialization benchmarks here we go so i'm gonna run that change that to release mode and execute and let's give it a minute or two until all those benchmarks finish running and let's see what we get after that so results are back let's see what we have here now the formatting is a bit weird but as you can see the classic serializer is our baseline basically and that is 144 microseconds and now generated one is almost 40 faster at 89 and we basically did nothing and the classic one returning a string was 178 and now the generator is 122. it's more of a 30 i've seen this fluctuate but this is usually around the 40 mark and this is around the 35 mark so this is nothing to sneeze at especially if you think of how much realization you're doing in your application now like i said before the digitalization i did not manage to have similar experiences with it it's basically performing the same in my experience but if you have a different experience or you know how i can get this code to perform better please let me know down below well that's all i had for you for this video thank you very much for watching special thanks to my patreons for making videos possible if you want to support me as well you're going to find it in the description down below leave a like if you like this video subscribe and we're gonna like and sharing the bell as well and i'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 35,744
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 6, .net 6 api, source generators, source gen, .net 6 source gen, json, json performance, json serlizations, json.net, fast json
Id: HhyBaJ7uisU
Channel Id: undefined
Length: 8min 9sec (489 seconds)
Published: Thu Nov 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.