Skip to content

Prefer Span<T>.Clear() over Span<T>.Fill(default) #33813

Closed
dotnet/roslyn-analyzers
#5181

Description

@GrabYourPitchforks

(Copied from #30740 (comment).)

Consider replacing this:

Span<T> theSpan = GetSpan();
theSpan.Fill(default(T));

With this:

Span<T> theSpan = GetSpan();
theSpan.Clear();

I see a handful of uses of Fill(0) or Fill(default) throughout the codebase (see callers). The Clear() method is potentially much more optimized than the Fill method depending on the T in use.

Category: Performance

Activity

  1. added
    api-suggestion
    untriaged
    code-analyzer
    on Mar 19, 2020
  2. gfoidl commented on Mar 20, 2020

    @gfoidl
    Member

    Aside from the analyzer I can submit a PR changing Fill(default) to Clear.

  3. Joe4evr commented on Mar 20, 2020

    @Joe4evr
    Contributor

    Is there a rule-of-thumb on when Clear() could be more optimized than Fill(default)?

  4. GrabYourPitchforks commented on Mar 20, 2020

    @GrabYourPitchforks
    MemberAuthor

    @Joe4evr currently Clear is always more optimized than Fill(default) for all T. Maybe there's an exception when the span contains only 1 or 2 elements, but those are rarities.

  5. added this to the Future milestone on Mar 20, 2020
  6. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    I'm going to remove this from the Roslyn Analyzers project for the time-being; we will add it back to the project once we get our triage process in place for prioritizing newly filed code-analyzer issues.

  7. removed
    untriaged
    on Mar 21, 2020
  8. GrabYourPitchforks commented on Apr 16, 2021

    @GrabYourPitchforks
    MemberAuthor

    @jeffhandley Any issues with me marking this ready for review? Seems straightforward enough.

  9. davidfowl commented on Apr 16, 2021

    @davidfowl
    Member

    I feel like Span<T>.Fill(default) would only be written by someone coming from C\C++, memset(0, sizeof(...)).

  10. GrabYourPitchforks commented on Apr 16, 2021

    @GrabYourPitchforks
    MemberAuthor

    I count 9 references to Span<T>.Fill(default) tracked by source.dot.net.

  11. jeffhandley commented on Apr 16, 2021

    @jeffhandley
    Member

    Any issues with me marking this ready for review?

    No objections from me; thanks!

  12. added
    api-ready-for-review
    and removed
    api-suggestion
    on Apr 17, 2021
  13. bartonjs commented on Jun 3, 2021

    @bartonjs
    Member

    Video

    Level: Info
    Category: Performance

    Looks good as proposed.

  14. added
    api-approved
    and removed
    api-ready-for-review
    on Jun 3, 2021
  15. carlossanlop commented on Sep 14, 2021

    @carlossanlop
    Contributor

    @huoyaoyuan Consider this issue assigned to you.

    Edit: Can't assign it to you because you need to participate in this discussion. But consider it yours.

  16. huoyaoyuan commented on Sep 15, 2021

    @huoyaoyuan
    Member

    @carlossanlop You can assign me now, and you can tell the owners to review the PR.

  17. NN--- commented on Jan 10, 2022

    @NN---
    Contributor

    @GrabYourPitchforks Probably also Fill could be optimized internally when default value is passed.

  18. huoyaoyuan commented on Jan 10, 2022

    @huoyaoyuan
    Member

    I'm afraid a branch will cause performance regression on small spans.

  19. huoyaoyuan commented on Feb 2, 2022

    @huoyaoyuan
    Member

    For VB, please confirm on the dotnet/runtime issue whether VB support is desired or not? If desired, and you wish to postpone it to a separate PR, please file a tracking issue in this repo for VB part.

    Originally posted by @mavasani in dotnet/roslyn-analyzers#5181 (review)

    @bartonjs
    I think support for VB shouldn't be desired, since using span in VB will cause error anyway.

  20. stephentoub commented on Feb 2, 2022

    @stephentoub
    Member

    Thanks. We don't need VB support for analyzers about the use of span until such a time that VB has more than minimal support for spans.

  21. ghost locked as resolved and limited conversation to collaborators on Sep 18, 2022
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.Memorycode-analyzer

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions