Skip to content

Prefer string.AsSpan() over string.Substring() when parsing #33784

Closed

Description

@terrajobst

The rule should flag instances of a pattern like int.Parse(str.Substring(...)) and instead switch to using the span-based int.Parse(str.AsSpan(...)). This would apply to all of the primitive types, and more generally potentially anything that has an overload taking a ReadOnlySpan<char> instead of a string.

Category: Performance

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added
    untriaged
    on Mar 19, 2020
  3. added this to the Future milestone on Mar 20, 2020
  4. added
    code-fixer
    on Mar 21, 2020
  5. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Small
    • Fixer: Small
  6. removed
    untriaged
    on Mar 21, 2020
  7. GrabYourPitchforks commented on Nov 10, 2020

    @GrabYourPitchforks
    Member

    Seems reasonable. We should figure out how much we want our analyzers (especially our on-by-default analyzers) to push people into using span. Using ROS<char> increases the concept count for scenarios which are currently very simple.

  8. bartonjs commented on Nov 10, 2020

    @bartonjs
    Member

    Video

    • Find argument expressions that are str.Substring(...) and there's an overload (or the same overload) that accepts ReadOnlySpan<char> in that position
    • Suggest changing Substring to AsSpan for the call

    Category: Performance
    Suggested severity: Info

  9. added
    api-ready-for-review
    api-approved
    and removed
    api-suggestion
    api-ready-for-review
    on Nov 10, 2020
  10. carlossanlop commented on Nov 10, 2020

    @carlossanlop
    Contributor

    @Mrnikbobjeff also approved in case you already started working on this. Let us know if you'd like to get this issue assigned to you.

  11. Mrnikbobjeff commented on Nov 11, 2020

    @Mrnikbobjeff

    @Mrnikbobjeff also approved in case you already started working on this. Let us know if you'd like to get this issue assigned to you.

    I am currently working on porting my analyzers to be IOperation analyzers to be inline with the pattern already established. While I have a working implementation for a c# analyzer and fixer I still need to port the analyzer at least for this to be viable.

  12. carlossanlop commented on Nov 20, 2020

    @carlossanlop
    Contributor

    Would you like me to assign this to you @Mrnikbobjeff or should i mark it up-for-grabs instead?

  13. added
    help wanted
    on Jan 22, 2021
  14. carlossanlop commented on Feb 5, 2021

    @carlossanlop
    Contributor

    I closed #33778 in favor of this one (it was an identical approved proposal).

    Note to whoever decides to work on this: Please take a look at this comment in the dup issue, since it contains a list of cases this analyzer should be able to address.

  15. NewellClark commented on Feb 6, 2021

    @NewellClark
    Contributor

    I would like to be assigned to this. I'll start work on it immediately.

  16. removed
    help wanted
    on Feb 6, 2021
  17. AraHaan commented on Apr 12, 2021

    @AraHaan
    Member

    How does one enable this AsSpan diagnostics for netstandard2.0+ for using it over Substring?

    Edit: It seems that the rule does not flag all uses of string.Substring, I would like all uses to get a diagnostic for replacing it with AsSpan as it will increase my program's performance indirectly, yes in some cases my code makes substrings and assigns them to variables before passing them to methods and so the AsSpan version could help me greatly in that case.

  18. ghost locked as resolved and limited conversation to collaborators on Sep 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Runtimecode-analyzercode-fixer

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions