-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Special case Mono in SpanHelpers.Fill/CleanWithoutReference #99059
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,6 +254,13 @@ public static unsafe void ClearWithoutReferences(ref byte dest, nuint len) | |
if (len == 0) | ||
return; | ||
|
||
// Mono works faster with Unsafe.InitBlockUnaligned | ||
#if MONO | ||
if (len > ZeroMemoryNativeThreshold) | ||
goto PInvoke; | ||
Unsafe.InitBlockUnaligned(ref dest, 0, (uint)len); | ||
return; | ||
#else | ||
ref byte destEnd = ref Unsafe.Add(ref dest, len); | ||
|
||
if (len <= 16) | ||
|
@@ -427,6 +434,7 @@ public static unsafe void ClearWithoutReferences(ref byte dest, nuint len) | |
Unsafe.WriteUnaligned<int>(ref Unsafe.Add(ref destEnd, -4), 0); | ||
#endif | ||
return; | ||
#endif // MONO | ||
|
||
PInvoke: | ||
// Implicit nullchecks | ||
|
@@ -498,24 +506,16 @@ internal static void Fill(ref byte dest, byte value, nuint len) | |
nuint stopLoopAtOffset = len & ~(nuint)7; | ||
do | ||
{ | ||
Unsafe.Add(ref dest, (nint)i + 0) = value; | ||
Unsafe.Add(ref dest, (nint)i + 1) = value; | ||
Unsafe.Add(ref dest, (nint)i + 2) = value; | ||
Unsafe.Add(ref dest, (nint)i + 3) = value; | ||
Unsafe.Add(ref dest, (nint)i + 4) = value; | ||
Unsafe.Add(ref dest, (nint)i + 5) = value; | ||
Unsafe.Add(ref dest, (nint)i + 6) = value; | ||
Unsafe.Add(ref dest, (nint)i + 7) = value; | ||
// broadcast the value to all 8 bytes of the ulong and write it to memory | ||
Unsafe.WriteUnaligned(ref Unsafe.Add(ref dest, i), value * 0x101010101010101ul); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's not just for Mono, but it's shorter (and faster) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is 64-bit multiplication. Is it faster on 32-bit platforms as well? |
||
} while ((i += 8) < stopLoopAtOffset); | ||
} | ||
|
||
// Write next 4 elements if needed | ||
if ((len & 4) != 0) | ||
{ | ||
Unsafe.Add(ref dest, (nint)i + 0) = value; | ||
Unsafe.Add(ref dest, (nint)i + 1) = value; | ||
Unsafe.Add(ref dest, (nint)i + 2) = value; | ||
Unsafe.Add(ref dest, (nint)i + 3) = value; | ||
// broadcast the value to all 4 bytes of the uint and write it to memory | ||
Unsafe.WriteUnaligned(ref Unsafe.Add(ref dest, i), value * 0x1010101u); | ||
i += 4; | ||
} | ||
|
||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth doing this over updating Mono to recognize/special-case the same scenarios as dotnet/runtime?
At the very least, should we ensure an issue tracking that same support is added so this
#if MONO
can be removed long term?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure whether Mono folks want to add more intrinsics in Mini and Interpreter considering this short path just works as is, so leaving that decision up to them, cc @BrzVlad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a big fan of having MONO ifdefs in SPC. Also I think the regression is limited to interpreter, so disabling for all mono might be uncalled for. Also this should be easy to intrinsify, I'll take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regression can be seen for mini (JIT) as well (https://pvscmdupload.blob.core.windows.net/reports/allTestHistory/refs/heads/main_x64_ubuntu%2022.04_LLVM=false_MonoAOT=false_MonoInterpreter=false_RunKind=micro_mono/System.Memory.Span(Char).Clear(Size:%2033).html)
Most likely for AOT as well since, the microbenchmark is written using generics, however, our AOT perf lines are currently broken.
Having Mono ifdefs in managed code doesn't seem to be a good long-term solution. If we go with this solution and the solution proves to remove the regression, I think we should add a tracking issue for adding these intrinsics to Mono. What do you think about this @fanyang-mono?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a look a quick look at the original PR (#98623). It seems to me that to intrinsify
SpanHelpers.Memmove
, the code path for intrinsifyingBuffer.Memmove
was used with some tweak. Mono intrinsifyBuffer.Memmove
as well. See the code below.runtime/src/mono/mono/mini/intrinsics.c
Lines 288 to 310 in fd48b6f
I think the proper fix should be updating the above code as CoreCLR did.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created a github issue for it #99161