You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Prefer Span<T>.Clear() over Span<T>.Fill(default) #33813
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.
@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.
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.
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.
Activity
gfoidl commentedon Mar 20, 2020
Aside from the analyzer I can submit a PR changing
Fill(default)toClear.Joe4evr commentedon Mar 20, 2020
Is there a rule-of-thumb on when
Clear()could be more optimized thanFill(default)?GrabYourPitchforks commentedon Mar 20, 2020
@Joe4evr currently
Clearis always more optimized thanFill(default)for all T. Maybe there's an exception when the span contains only 1 or 2 elements, but those are rarities.jeffhandley commentedon Mar 21, 2020
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.
Replace Span<T>.Fill(0) with Span<T>.Clear()
Replace Span<T>.Fill(0) with Span<T>.Clear()
GrabYourPitchforks commentedon Apr 16, 2021
@jeffhandley Any issues with me marking this ready for review? Seems straightforward enough.
davidfowl commentedon Apr 16, 2021
I feel like
Span<T>.Fill(default)would only be written by someone coming from C\C++,memset(0, sizeof(...)).GrabYourPitchforks commentedon Apr 16, 2021
I count 9 references to
Span<T>.Fill(default)tracked by source.dot.net.jeffhandley commentedon Apr 16, 2021
No objections from me; thanks!
bartonjs commentedon Jun 3, 2021
Video
Level: Info
Category: Performance
Looks good as proposed.
carlossanlop commentedon 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 commentedon Sep 15, 2021
@carlossanlop You can assign me now, and you can tell the owners to review the PR.
NN--- commentedon Jan 10, 2022
@GrabYourPitchforks Probably also
Fillcould be optimized internally when default value is passed.huoyaoyuan commentedon Jan 10, 2022
I'm afraid a branch will cause performance regression on small spans.
huoyaoyuan commentedon Feb 2, 2022
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 commentedon 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.