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

Avoid zero-length array allocations and storing them in static fields (CA1825) #10267

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

h3xds1nz
Copy link
Contributor

@h3xds1nz h3xds1nz commented Jan 11, 2025

Description

This PR mainly resolves CA1825 but in some cases goes a step further over standard analyzer fix.

  • In addition to using singleton over allocating zero-length array everytime, we want to avoid storing those in readonly fields (whether they're marked or not), as the pointer to such array is already accessible via Array.Empty<Type>().
  • XamlTypeMapper was going step further and cloning this zero-length array even. Happy days.
  • RowSpanVector was performing unnecessary assigment if the branch was not taken.

Customer Impact

Increased performance, decreased allocations.

Regression

No.

Testing

Local build.

Risk

Should be low.

Microsoft Reviewers: Open in CodeFlow

@h3xds1nz h3xds1nz requested review from a team as code owners January 11, 2025 21:36
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Jan 11, 2025
@omariom
Copy link
Contributor

omariom commented Jan 13, 2025

Could [] be used insted of Array.Empty?

@h3xds1nz
Copy link
Contributor Author

Could [] be used insted of Array.Empty?

I'm not a fan. In cases like assigning to already defined field or parameter you lose the visible type information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants