Skip to content

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

@GrabYourPitchforks

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

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Memorycode-analyzerMarks an issue that suggests a Roslyn analyzer

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions