Perf: Use Span.CopyTo<Byte>() on .NET 7 and above.#2170
Perf: Use Span.CopyTo<Byte>() on .NET 7 and above.#2170jkrejcha wants to merge 2 commits intoMessagePack-CSharp:masterMessagePack-CSharp/MessagePack-CSharp:masterfrom jkrejcha:20250301-use-safe-span-methods-if-availablejkrejcha/MessagePack-CSharp:20250301-use-safe-span-methods-if-availableCopy head branch name to clipboard
Span.CopyTo<Byte>() on .NET 7 and above.#2170Conversation
This changes `UnsafeMemory` to use the safe equivalents in .NET 7 and above as they are more performant than the equivalent code and can take advantage of vectorization on hardware that supports it
Span.CopyTo<Byte>() on .NET 7 and above.Span.CopyTo<Byte>() on .NET 7 and above.
|
Thanks for this. But we don't need the Can you just replace the old code with the new code? And then we should definitely get @neuecc's review (and maybe @pCYSl5EDgo) because they've done a lot of micro-optimization work in this library. |
|
Alright. It looks like .NET 7 and below doesn't have the optimization anyway potentially (it replaces with a call to Output on .NET 8 vs. .NET 7 |
|
Thanks. Two more thoughts. So long as you're looking at native assembly emitted, can you look at .NET Framework? That's important too. If this really is going to be the way to go, I guess we can get rid of all the many methods that you're changing too, as they were named and implemented specially for the length of the span, and that evidently isn't necessary any more since their implementations are now identical. But feel free to wait for @neuecc's take before doing more work, since he may know something about why this isn't doable in the first place. Unity is another target runtime and it may be important there that the code remains as-is, for example. |
|
This seems like a remnant from an era before Span existed. |
|
In the end, it goes to SpanHelpers.memmove? |
|
Minimal benchmarking shows that Span is better than the original. |
This changes
UnsafeMemoryto use the safe equivalents in .NET 7 and above as they are more performant than the equivalent code and can take advantage of vectorization on hardware that supports itThis optimization exists on at least .NET 7.