Skip to content
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

Closed
Tracked by #77020
eiriktsarpalis opened this issue Feb 15, 2022 · 4 comments · Fixed by dotnet/roslyn-analyzers#6850
Closed
Tracked by #77020
Assignees
Labels
api-approved API was approved in API review, it can be implemented api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json blocking-release code-analyzer Marks an issue that suggests a Roslyn analyzer in-pr There is an active PR which will close this issue when it is merged tenet-performance Performance related issue
Milestone

Comments

@eiriktsarpalis
Copy link
Member

eiriktsarpalis commented Feb 15, 2022

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 singleton JsonSerializerOptions 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:

JsonSerializer.Serialize(value, new JsonSerializerOptions { /* some custom config */ });

or if we're passing a local that has been initialized in the same method

var options = useDefault ? JsonSerializerOptions.Default : new JsonSerializerOptions { /* some custom config */ };

foreach(object value in values)
{
    JsonSerializer.Serialize(value, options);
}

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.

@eiriktsarpalis eiriktsarpalis added area-System.Text.Json tenet-performance Performance related issue code-analyzer Marks an issue that suggests a Roslyn analyzer labels Feb 15, 2022
@eiriktsarpalis eiriktsarpalis added this to the Future milestone Feb 15, 2022
@ghost
Copy link

ghost commented Feb 15, 2022

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

Issue Details

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 singleton JsonSerializerOptions instances. We should add an analyzer that warns about any JsonSerializerOptions construction that flows only to a call to a JsonSerializer method argument.

Author: eiriktsarpalis
Assignees: -
Labels:

area-System.Text.Json, tenet-performance, code-analyzer

Milestone: Future

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Feb 15, 2022
@eiriktsarpalis eiriktsarpalis removed the untriaged New issue has not been triaged by the area owner label Feb 15, 2022
@timmydo
Copy link

timmydo commented Jun 9, 2022

@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.

@eiriktsarpalis
Copy link
Member Author

Related to #31094 (comment). Ultimately this and other issues stem from ergonomic concerns with the existing static JsonSerializer methods. One possible alternative might be to use a DI approach and obsolete static methods accepting JsonSerializerOptions.

@eiriktsarpalis eiriktsarpalis modified the milestones: Future, 8.0.0 Sep 2, 2022
@buyaa-n buyaa-n added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 10, 2022
eerhardt added a commit to eerhardt/eShopOnContainers that referenced this issue Jun 13, 2023
According to dotnet/runtime#65396, using a new JsonSerializerOptions every time JsonSerializer is invoked is a suboptimal pattern.

Fix this by caching JsonSerializerOptions instances.
@eiriktsarpalis eiriktsarpalis self-assigned this Jun 16, 2023
@eiriktsarpalis eiriktsarpalis added the api-ready-for-review API is ready for review, it is NOT ready for implementation label Jun 16, 2023
@terrajobst
Copy link
Member

terrajobst commented Jul 18, 2023

Video

Looks good as proposed.

Category: Performance
Severity: Info

@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jul 18, 2023
@jozkee jozkee self-assigned this Jul 24, 2023
@eiriktsarpalis eiriktsarpalis added the in-pr There is an active PR which will close this issue when it is merged label Aug 11, 2023
@eiriktsarpalis eiriktsarpalis removed their assignment Aug 11, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json blocking-release code-analyzer Marks an issue that suggests a Roslyn analyzer in-pr There is an active PR which will close this issue when it is merged tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants