Memmove: handle overlapping forward copies in managed code - #132695
Draft
EgorBo wants to merge 7 commits into
Draft
Memmove: handle overlapping forward copies in managed code#132695EgorBo wants to merge 7 commits into
EgorBo wants to merge 7 commits into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 20467548-69a0-453c-af07-8259bf6415fb
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-memory |
This comment was marked as low quality.
This comment was marked as low quality.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR changes SpanHelpers.Memmove’s handling of overlapping forward copies (dest < src) to avoid always calling into native memmove, adding a managed, strictly-forward copy strategy (including a vectorized path) intended to improve throughput for common same-buffer left-shift patterns.
Changes:
- Adds a forward-overlap fast path that routes
dest < srcoverlapping copies to a new managed helper. - Introduces
MemmoveOverlappedForwardandCopyForwardVectorizedto copy in strictly ascending order and avoid the native overlap cliff. - Chunks native
memmovecalls for large overlapping forward copies when the managed vectorized path isn’t taken.
Member
Author
|
@EgorBot -windows_amd -windows_intel -windows_arm -macos_arm using System.Collections.Generic;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);
public class Bench {
private List<int> _list;
[Params(4, 10, 16, 100, 1000, 10000, 100000)]
public int Count;
[GlobalSetup]
public void Setup() {
_list = new List<int>(Count);
for (int i = 0; i < Count; i++)
_list.Add(i);
}
[Benchmark]
public void RemoveSecond() {
List<int> list = _list;
list.RemoveAt(1);
CollectionsMarshal.SetCount(list, Count);
}
} |
Aligning the destination closes the gap with rep movsb at every size, so the distance heuristic and the chunked memmove are no longer needed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 20467548-69a0-453c-af07-8259bf6415fb
Member
Author
|
@EgorBot -windows_amd -windows_intel -windows_arm -macos_arm using System.Collections.Generic;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Bench).Assembly).Run(args);
public class Bench {
private List<int> _list;
[Params(4, 10, 16, 100, 1000, 10000, 100000)]
public int Count;
[GlobalSetup]
public void Setup() {
_list = new List<int>(Count);
for (int i = 0; i < Count; i++)
_list.Add(i);
}
[Benchmark]
public void RemoveSecond() {
List<int> list = _list;
list.RemoveAt(1);
CollectionsMarshal.SetCount(list, Count);
}
} |
Factors the copy into shared block primitives so the forward and backward routines are mirror images of each other. Backward overlapping copies up to 256 bytes now skip the QCall as well; above that the platform memmove's backward loop is still about twice as fast as a managed descending one. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 20467548-69a0-453c-af07-8259bf6415fb
Replaces the hand-written vector blocks, which assumed the JIT would keep all of a block's loads ahead of its stores, with constant-length Memmove calls. The JIT unrolls those into exactly that shape by design, and picks the widest vector the target has. Blocks are capped at 64 bytes so a call the JIT did not unroll re-enters with a length the tails handle without calling Memmove again, which keeps these routines from recursing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 20467548-69a0-453c-af07-8259bf6415fb
Other targets keep using the CRT memmove exactly as before, which removes the 32-bit split in the tail, the Vector128 gate and the Intrinsics using. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 20467548-69a0-453c-af07-8259bf6415fb
Mono turns every SpanHelpers.Memmove call into OP_MEMMOVE rather than an unrolled block, so the block loops would degrade into one native call per 64 bytes there. Leave Mono on the CRT memmove. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 20467548-69a0-453c-af07-8259bf6415fb
Member
Author
|
@EgorBot -windows_amd -windows_intel -windows_arm -macos_arm using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Bench<>).Assembly).Run(args);
[GenericTypeArguments(typeof(byte))]
[GenericTypeArguments(typeof(int))]
[GenericTypeArguments(typeof(long))]
public class Bench<T> {
private T[] _array;
[Params(4, 10, 16, 100, 1000, 10000, 100000)]
public int Count;
[GlobalSetup]
public void Setup() => _array = new T[Count];
[Benchmark]
public void ShiftLeft() {
Span<T> span = _array;
span.Slice(1).CopyTo(span);
}
[Benchmark]
public void ShiftRight() {
Span<T> span = _array;
span.Slice(0, span.Length - 1).CopyTo(span.Slice(1));
}
} |
Memmove is big enough that inlining it burns a lot of the caller's budget, and when it was inlined into the overlapped copies the JIT no longer had a call to unroll - the inlined body re-dispatched on overlap and called straight back in, which cost ~8x at tier1. Keeping the call also makes the unroll reliable, so the block can be the widest the JIT still expands: 256 bytes with Vector512. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 20467548-69a0-453c-af07-8259bf6415fb
Member
Author
|
@EgorBot -windows_amd -windows_intel -windows_arm -macos_arm -linux_amd using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkSwitcher.FromAssembly(typeof(Bench<>).Assembly).Run(args);
[GenericTypeArguments(typeof(byte))]
[GenericTypeArguments(typeof(int))]
[GenericTypeArguments(typeof(long))]
public class Bench<T> {
private T[] _array;
[Params(4, 10, 16, 100, 1000, 10000, 100000)]
public int Count;
[GlobalSetup]
public void Setup() => _array = new T[Count];
[Benchmark]
public void ShiftLeft() {
Span<T> span = _array;
span.Slice(1).CopyTo(span);
}
[Benchmark]
public void ShiftRight() {
Span<T> span = _array;
span.Slice(0, span.Length - 1).CopyTo(span.Slice(1));
}
} |
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.
Experiment to test with EgorBot.
SpanHelpers.Memmovecurrently P/Invokes the CRTmemmovefor any overlap at any length. Its forward copy loop is tuned for disjoint buffers, so same-array left shifts (List<T>.RemoveAtand friends) hit two separate cliffs — see #132690:rep movsbcollapses to ~3 GB/s when the buffers are less than a cache line apart (Emerald Rapids, 40 KB copy)This copies the
dest < srccase in managed code, in strictly ascending order with the destination aligned. Right shifts are untouched — the CRT's backward loop has neither problem.