Skip to content

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

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

Merged
merged 3 commits into from
Apr 7, 2025

Conversation

h3xds1nz
Copy link
Member

@h3xds1nz h3xds1nz commented Jan 11, 2025

Fixes #10286

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

Copy link
Member

@harshit7962 harshit7962 left a comment

Choose a reason for hiding this comment

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

Changes LGTM

@harshit7962 harshit7962 merged commit 7f6d4aa into dotnet:main Apr 7, 2025
8 checks passed
@harshit7962
Copy link
Member

Thank you @h3xds1nz for these contributions. We will be taking the next batch of changes for the test pass soon.

@harshit7962 harshit7962 self-assigned this Apr 7, 2025
@h3xds1nz
Copy link
Member Author

h3xds1nz commented Apr 7, 2025

@harshit7962 Thank you, happy to hear :)

@github-actions github-actions bot locked and limited conversation to collaborators May 7, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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.

CA1825: Avoid zero-length array allocations
3 participants