-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add an analyzer that warns on single-use JsonSerializerOptions instances #65396
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsForked from #40072. While #64646 mitigates most of the performance issues related to creating single-use
|
@jeffhandley @eiriktsarpalis would be great if we could get something like this. We have an issue in our codebase where a dev passed a new options object to the Serialize method every time and it caused a lot of CPU due to jitting and other things. This could have been caught with an analyzer. |
Related to #31094 (comment). Ultimately this and other issues stem from ergonomic concerns with the existing static |
According to dotnet/runtime#65396, using a new JsonSerializerOptions every time JsonSerializer is invoked is a suboptimal pattern. Fix this by caching JsonSerializerOptions instances.
Looks good as proposed. Category: Performance |
Forked from #40072.
While #64646 mitigates most of the performance issues related to creating single-use
JsonSerializerOptions
instances, this is still a suboptimal pattern and users are better off working with singletonJsonSerializerOptions
instances. We should add an analyzer that warns about any JsonSerializerOptions construction that flows only to a call to a JsonSerializer method argument.The analyzer should warn if any of the following patterns are detected:
or if we're passing a local that has been initialized in the same method
In both cases the analyzer should recommend lifting the options instance to a static variable or use DI, but would most likely not provide a fixer.
The text was updated successfully, but these errors were encountered: