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 Span<T>.Clear() over Span<T>.Fill(default) #33813

Closed
Tracked by #57797
GrabYourPitchforks opened this issue Mar 19, 2020 · 15 comments · Fixed by dotnet/roslyn-analyzers#5181
Closed
Tracked by #57797

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

GrabYourPitchforks opened this issue Mar 19, 2020 · 15 comments · Fixed by dotnet/roslyn-analyzers#5181
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Memory code-analyzer Marks an issue that suggests a Roslyn analyzer
Milestone

Comments

@GrabYourPitchforks
Copy link
Member

(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

@GrabYourPitchforks GrabYourPitchforks added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory untriaged New issue has not been triaged by the area owner code-analyzer Marks an issue that suggests a Roslyn analyzer labels Mar 19, 2020
@gfoidl
Copy link
Member

gfoidl commented Mar 20, 2020

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

@Joe4evr
Copy link
Contributor

Joe4evr commented Mar 20, 2020

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

@GrabYourPitchforks
Copy link
Member Author

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

@jeffhandley jeffhandley added this to the Future milestone Mar 20, 2020
@jeffhandley
Copy link
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.

@terrajobst terrajobst removed the untriaged New issue has not been triaged by the area owner label Mar 21, 2020
turbedi added a commit to turbedi/sharpcompress that referenced this issue Apr 6, 2020
turbedi added a commit to turbedi/sharpcompress that referenced this issue Apr 6, 2020
@GrabYourPitchforks
Copy link
Member Author

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

@davidfowl
Copy link
Member

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

@GrabYourPitchforks
Copy link
Member Author

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

@jeffhandley
Copy link
Member

Any issues with me marking this ready for review?

No objections from me; thanks!

@GrabYourPitchforks GrabYourPitchforks 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 Apr 17, 2021
@bartonjs
Copy link
Member

bartonjs commented Jun 3, 2021

Video

Level: Info
Category: Performance

Looks good as proposed.

@bartonjs bartonjs 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 Jun 3, 2021
@carlossanlop
Copy link
Member

carlossanlop commented Sep 14, 2021

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

@huoyaoyuan
Copy link
Member

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

@NN---
Copy link
Contributor

NN--- commented Jan 10, 2022

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

@huoyaoyuan
Copy link
Member

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

@huoyaoyuan
Copy link
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.

@stephentoub
Copy link
Member

stephentoub commented Feb 2, 2022

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.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Memory code-analyzer Marks an issue that suggests a Roslyn analyzer
Projects
None yet
Development

Successfully merging a pull request may close this issue.