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

Enable rule CA1869 - Cache and reuse 'JsonSerializerOptions' #90895

Merged
merged 3 commits into from
Sep 11, 2023

Conversation

jozkee
Copy link
Member

@jozkee jozkee commented Aug 21, 2023

Follow up to dotnet/roslyn-analyzers#6850.

Found 102 hits on test projects, 1 in FunctionalTests\System.Text.RegularExpressions.Tests. The rest were in System.Text.Json.Tests, which is unsurprising.

Also found 5 occurrences in src/tasks projects which should be fixed.

@jozkee jozkee added this to the 9.0.0 milestone Aug 21, 2023
@jozkee jozkee self-assigned this Aug 21, 2023
@ghost
Copy link

ghost commented Aug 21, 2023

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

Issue Details

Follow up to dotnet/roslyn-analyzers#6850.

Found 102 hits total on test projects, 1 in FunctionalTests\System.Text.RegularExpressions.Tests. The rest were in System.Text.Json.Tests, which is unsurprising.

Author: Jozkee
Assignees: Jozkee
Labels:

area-Meta

Milestone: 9.0.0

@jozkee jozkee requested a review from radical as a code owner August 21, 2023 23:21
@buyaa-n
Copy link
Member

buyaa-n commented Aug 22, 2023

Also found 5 occurrences in src/tasks projects which should be fixed.

FYI you have more flagged that failed the build

@@ -65,7 +67,7 @@ public static void Create(MetadataReader metadataReader, string clsidMapPath)

using (StreamWriter writer = File.CreateText(clsidMapPath))
{
writer.Write(JsonSerializer.Serialize(clsidMap, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull }));
writer.Write(JsonSerializer.Serialize(clsidMap, s_jsonOptions));
Copy link

@Neme12 Neme12 Aug 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to serialize directly into the FileStream instead of creating a string?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably is but those optimizations are beyond the scope of this PR and this isn't product code so the performance gains probably are probably not that significant.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if there was an analyzer for this as well.

@@ -34,7 +35,7 @@ public FileCache(string? cacheFilePath, TaskLoggingHelper log)
{
_oldCache = (CompilerCache?)JsonSerializer.Deserialize(File.ReadAllText(cacheFilePath),
typeof(CompilerCache),
new JsonSerializerOptions());
s_jsonOptions);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to pass a JSO at all? Couldn't the argument just be removed?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better for consistency to keep it here. In case another option is added to the options in the future apart from WriteIndented, someone might not realize that now they have to pass in the options here as well.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The options just happen to not affect reading right now, but that could change.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this way, the same cache is reused for both reading & writing.

@jozkee jozkee closed this Sep 11, 2023
@jozkee jozkee reopened this Sep 11, 2023
@jozkee jozkee merged commit 6504cdb into dotnet:main Sep 11, 2023
181 of 183 checks passed
@jozkee jozkee deleted the enablerule_jsonoptions_ca1869 branch September 11, 2023 17:53
@ghost ghost locked as resolved and limited conversation to collaborators Oct 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants