-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Huge performance degradation using System.Text.Json to serialize a model with an Object type property #31103
Comments
Thanks for reporting this @mrlund, that looks like a severe regression which we should definitely try and fix. It isn't isolated to just collections (serializing the simple login view model test also regresses roughly 15x). Though collections (like Looks like @svick is working on a fix for this: dotnet/corefx#41753 Did you notice any similar issues with deserializing or was this perf issue unique to serialization (in my quick testing, I noticed a perf difference there but it wasn't this significant - 60%)? |
@ahsonkhan I didn't try deserializing since my application doesn't do that for this model, and generally doesn't deserialize anything beyond simple models. If it's helpful I'd be happy to setup a case where it does, and report back? |
Edit: Actually, never mind. I just remembered that for deserializing |
It's a bit of a contrived test, since what I did was serialize the model using Json.NET and then deserialize it again with System.Text. From my test it doesn't seem like there's any significant performance difference deserializing the model with As a point of interest it seems that Json.NET doesn't have any performance difference serializing the polymorphic type vs the concrete type. *Since it's a real application use case, with a lot of other things involved, and I'm running it as a local load test the resulting requests/sec do vary +/- 15% from each run. It was probably just a little slower deserializing the |
Great, thanks for checking.
Makes sense given the implementation. I believe |
Port dotnet/corefx#41753 to 3.1 |
I'll create the 3.1 PR. |
@steveharter was there PR porting it to 3.1? |
Yes, dotnet/corefx#42057 ports the fix to 3.1. |
I've been troubleshooting some performance issues that came up during a core 2.2 -> 3.0 upgrade, and was stumped why a particular ASP.NET Core API endpoint showed terrible performance compared to the others.
After countless hours I finally discovered the cause; the model being serialized contained a child class, with a collection property that was simply defined with type Object, i.e. what should have been:
was defined as:
The reasons for this odd coding choice are unfortunately lost in history, but it works fine, was insidiously hard to diagnose, and the perf impact was very severe, with the API going from ~300 requests/sec under local load tests to ~3300 requests/sec after fixing.
I assume that the poor performance is due to reflection being used to serialize when there's an unknown object type, and I guess it only gets worse because it's a list. It's not clear to me if there's a way of fixing this, or if it would be necessary to remove support for serializing
Object
altogether. Perhaps just log a warning to make it easier to catch?I'd be happy to try my hand with a PR, if someone could point me in the right direction?
The text was updated successfully, but these errors were encountered: