Use safe Span.Slice loop pattern in Enumerable.FillIncrementing#127427
Merged
EgorBo merged 1 commit intodotnet:mainfrom Apr 26, 2026
Merged
Use safe Span.Slice loop pattern in Enumerable.FillIncrementing#127427EgorBo merged 1 commit intodotnet:mainfrom
EgorBo merged 1 commit intodotnet:mainfrom
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Member
Author
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-linq |
Contributor
There was a problem hiding this comment.
Pull request overview
Refactors Enumerable.FillIncrementing<T> to use a safe Span<T>.Slice-driven loop shape for the vectorized fill, eliminating Unsafe/MemoryMarshal usage while keeping JIT-friendly bounds-check elimination.
Changes:
- Removed
Unsafe.*/MemoryMarshal.GetReferencepointer-walking inFillIncrementing<T>. - Replaced vector stores with
Vector<T>.CopyTo(destination)inside awhile (destination.Length >= Vector<T>.Count)loop and advanced viadestination = destination.Slice(...). - Simplified the scalar tail fill to iterate directly over the remaining span.
Member
Author
|
@EgorBot -intel -arm using System.Linq;
using BenchmarkDotNet.Attributes;
public class RangeToArrayBench
{
[Params(0, 1, 4, 8, 16, 64, 256, 1024, 16384, 1_000_000)]
public int Count;
[Benchmark]
public int[] RangeToArray() => Enumerable.Range(0, Count).ToArray();
} |
MihaZupan
approved these changes
Apr 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
This PR was authored with assistance from GitHub Copilot (AI-generated content).
Rewrites the vectorized fill in
Enumerable.FillIncrementing<T>to use the safewhile (span.Length >= CONST) { ...; span = span.Slice(CONST); }loop pattern, removing allUnsafe.*/MemoryMarshal.GetReferenceplumbing. JIT recognizes this shape and elides the bounds checks.Codegen
Small diffs, no new checks. No perf impact.