-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
perf: soft revert of the performance regressions introduced in #1469 #2885
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,50 @@ namespace Stride.Core; | |
| /// </summary> | ||
| public static class Utilities | ||
| { | ||
| // MUST BE A METHOD AND AGGRESSIVELY INLINED, OTHERWISE THE JIT WILL NOT ELIMINATE THE BRANCH | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| private static bool IsUnalignedSafe() => | ||
| RuntimeInformation.ProcessArchitecture is Architecture.X64 or Architecture.X86 or Architecture.Arm64; | ||
|
|
||
| /// <inheritdoc cref="AlignmentFallbackDoc"/> | ||
| /// <inheritdoc cref="Unsafe.CopyBlock(ref byte, ref readonly byte, uint)"/> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static unsafe void CopyWithAlignmentFallback(ref byte destination, ref readonly byte source, uint byteCount) | ||
| { | ||
| if (IsUnalignedSafe()) | ||
| fixed (void* src = &source, dst = &destination) | ||
| Buffer.MemoryCopy(src, dst, byteCount, byteCount); | ||
|
Comment on lines
+48
to
+49
Contributor
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.
Contributor
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. Also, for cases where we are sure the memory is aligned (like those where we control the type and ensure proper layouts), you also have the option of
Collaborator
Author
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. I chose Buffer.MemoryCopy because it's more straightforward than creating two spans and using
Collaborator
Author
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. While
Contributor
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. For these utility methods, it's enough to use
Collaborator
Author
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. Yep |
||
| else | ||
| Unsafe.CopyBlockUnaligned(ref destination, in source, byteCount); | ||
| } | ||
|
|
||
| /// <inheritdoc cref="AlignmentFallbackDoc"/> | ||
| /// <inheritdoc cref="Unsafe.CopyBlock(ref byte, ref readonly byte, uint)"/> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static unsafe void CopyWithAlignmentFallback(void* destination, void* source, uint byteCount) | ||
| { | ||
| if (IsUnalignedSafe()) | ||
| Buffer.MemoryCopy(source, destination, byteCount, byteCount); | ||
| else | ||
| Unsafe.CopyBlockUnaligned(destination, source, byteCount); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Zero out memory at the address provided | ||
| /// </summary> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static unsafe void Clear(void* startAddress, uint byteCount) | ||
| { | ||
| // Span swaps between InitBlockUnaligned and _ZeroMemory depending on the size | ||
| new Span<byte>(startAddress, (int)byteCount).Clear(); | ||
| } | ||
|
|
||
| /// <remarks> | ||
| /// Some of the architecture dotnet runs on do not support arbitrary unaligned reads or writes, | ||
| /// use this instead of other memcopy if you aren't sure whether the pointers you passed in are aligned. | ||
| /// </remarks> | ||
| static void AlignmentFallbackDoc() { } | ||
|
|
||
| /// <summary> | ||
| /// Allocate an aligned memory buffer. | ||
| /// </summary> | ||
|
|
||
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.
What if it is a
static readonlyfield? Theoretically, doesn't the JIT evaluate those only once and uses them like aconstlater on?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.
You would think, but nope, here's disasm:
While this one is:
Godbolt shows similar overhead, so this is not a config issue.
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.
Maybe one of those things that it does when doing PGO and tiering?
Anyway, not that important.