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

CA1846: Prefer AsSpan over Substring #10299

Open
Tracked by #10270
himgoyalmicro opened this issue Jan 16, 2025 · 0 comments · May be fixed by #10144
Open
Tracked by #10270

CA1846: Prefer AsSpan over Substring #10299

himgoyalmicro opened this issue Jan 16, 2025 · 0 comments · May be fixed by #10144
Assignees
Labels
area-Styling Indicates if an issue or a PR is for style changes 🚧 work in progress

Comments

@himgoyalmicro
Copy link
Contributor

himgoyalmicro commented Jan 16, 2025

Link to issue description: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1846

Rule Description:

Substring allocates a new string object on the heap and performs a full copy of the extracted text. String manipulation is a performance bottleneck for many programs. Allocating many small, short-lived strings on a hot path can create enough collection pressure to impact performance. The O(n) copies created by Substring become relevant when the substrings get large. The Span and ReadOnlySpan types were created to solve these performance problems.

Many APIs that accept strings also have overloads that accept a ReadOnlySpan<System.Char> argument. When such overloads are available, you can improve performance by calling AsSpan instead of Substring.

@himgoyalmicro himgoyalmicro changed the title CA1846 CA1846: Prefer AsSpan over Substring Jan 16, 2025
@himgoyalmicro himgoyalmicro self-assigned this Jan 16, 2025
@himgoyalmicro himgoyalmicro added the area-Styling Indicates if an issue or a PR is for style changes label Jan 16, 2025
@himgoyalmicro himgoyalmicro linked a pull request Jan 16, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Styling Indicates if an issue or a PR is for style changes 🚧 work in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant