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

Prefer string.AsSpan over string.Substring #33778

Closed
terrajobst opened this issue Mar 19, 2020 · 6 comments
Closed

Prefer string.AsSpan over string.Substring #33778

terrajobst opened this issue Mar 19, 2020 · 6 comments
Labels
area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer
Milestone

Comments

@terrajobst
Copy link
Member

Somewhat more generally, any time string.Substring is used as an argument to something where there's an equivalent overload that takes a ReadOnlySpan<char> (e.g. StringBuilder.Append(string) vs StringBuilder.Append(ReadOnlySpan<char>)), the case can be flagged to be changed to use AsSpan instead.

Category: Performance

@terrajobst terrajobst added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer labels Mar 19, 2020
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Mar 19, 2020
@jeffhandley jeffhandley added this to the Future milestone Mar 20, 2020
@jeffhandley jeffhandley added the code-fixer Marks an issue that suggests a Roslyn code fixer label Mar 21, 2020
@jeffhandley
Copy link
Member

Estimates:

  • Analyzer: Small
  • Fixer: Small

@terrajobst terrajobst removed the untriaged New issue has not been triaged by the area owner label Mar 21, 2020
@carlossanlop
Copy link
Member

carlossanlop commented Jan 15, 2021

Suggested severity: Info

We implemented a very similar analyzer for Stream.ReadAsync and Stream.WriteAsync that suggested preferring the memory-based overloads. The logic can be very similar here so whoever works on this can use that analyzer for reference: dotnet/roslyn-analyzers#3592

static void Main()
{
    int n = 0;
    string s = "My string";

    // Substring overload with 1 argument
    // Before
    MyMethod(s.Substring(1));
    // After
    MyMethod(s.AsSpan(1));


   // Substring overload with 2 arguments
    // Before
    MyMethod(s.Substring(1, 3));
    // After
    MyMethod(s.AsSpan(1, 3));


    // Extra arguments, find overload with same types except string->ROS
    // Before
    MyMethod(n, s.Substring(1));
    // After
    MyMethod(n, s.AsSpan(1));


    // Named arguments, match overload with string->ROS in same position
    // Before
    MyMethod(s: s.Substring(1), n: n);
    // After
    MyMethod(ros: s.AsSpan(1), n: n);


    // Named arguments, multiple strings, match overload with strings->ROS in same positions
    // Before
    MyMethod(s2: s.Substring(1), n: n, s1: s.Substring(2));
    // After
    MyMethod(ros2: s.AsSpan(1), n: n, ros1: s.Substring(2));
}

static void MyMethod(string s);
static void MyMethod(ReadOnlySpan<char> ros);
static void MyMethod(int n, string s);
static void MyMethod(int n, ReadOnlySpan<char> ros);
static void MyMethod(int n, string s1, string s2);
static void MyMethod(int n, ReadOnlySpan<char> ros1, ReadOnlySpan<char> ros2);

@carlossanlop carlossanlop added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jan 15, 2021
@stephentoub
Copy link
Member

Suggested severity: Warning

I don't think any purely perf-focused rules like this should break a build (which warning can do if warning-as-error is enabled). It should likely be 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 Feb 2, 2021
@terrajobst
Copy link
Member Author

terrajobst commented Feb 2, 2021

Video

  • On by default as info seems fine

@carlossanlop carlossanlop added the help wanted [up-for-grabs] Good issue for external contributors label Feb 2, 2021
@carlossanlop
Copy link
Member

@terrajobst I just noticed this issue is almost identical to #33784.

The only difference is that the other issue is specifically targeted to substrings passed as arguments to Parse methods of primitive types, while this issue is specifically targeted to substrings passed as arguments to any methods that take a string.

Is there a reason I'm missing for which there are two separate issues? Couldn't we have one analyzer target all methods?

Note: The other issue was approved back in November, and is already assigned to someone working on it. So if we have to close one, it should be this one.

@buyaa-n buyaa-n added help wanted [up-for-grabs] Good issue for external contributors and removed help wanted [up-for-grabs] Good issue for external contributors labels Feb 5, 2021
@carlossanlop
Copy link
Member

Talked to @terrajobst about this. This is a duplicate approval of #33784. Since the other one was approved first, I'll keep that one open.

@ghost ghost locked as resolved and limited conversation to collaborators Mar 7, 2021
@carlossanlop carlossanlop removed api-approved API was approved in API review, it can be implemented help wanted [up-for-grabs] Good issue for external contributors labels Mar 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime code-analyzer Marks an issue that suggests a Roslyn analyzer code-fixer Marks an issue that suggests a Roslyn code fixer
Projects
None yet
Development

No branches or pull requests

6 participants