-
Hi clr team!
To add a layer of complication, the code will run both con Core (3.1, eventually also 5) and Framework 4.x. Any suggestion or pointers in the right direction is really welcome! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
Measure, then you'll know (for sure and for your workload). |
Beta Was this translation helpful? Give feedback.
-
For ~1MB block of memory, all memory copying methods in recent runtimes (Span.ToArray, Span.CopyTo, Array.Copy, Buffer.BlockCopy, Buffer.MemoryCopy, ...) end up calling memcpy. We depend on memcpy to provide efficient memory copying for large memory blocks. For .NET Framework, you can call
Implementing memory copying that is as efficient as possible on variety of machines, for all combinations of block sizes, source and target alignment, is very complex. The typical memcpy implementation has number of strategies, and picks one based on variety of factors. Unless you know the exact machine and conditions that this will run on, it is probably not worth for you to spend time on this. |
Beta Was this translation helpful? Give feedback.
-
Yes, |
Beta Was this translation helpful? Give feedback.
-
First of all, thank you for your answers, very informative!
Yeah, I know, I was just being lazy :) First, .NET Full 32bit
CopyTo is the clear winner. ToArray is like allocation + CopyTo. Looping is not so good, as expected.
Suprinsingly (maybe) CopyTo is slightly worse. I added Buffer.MemoryCopy, and it seems to be the best alternative.
CopyTo is like MemoryCopy (expeted) and lopping over the ReadOnlySpan is better (expected) Finally, copy and convert. Of corse Core here is better than Full because indexed access on ReadOnlySpan is better:
The clear winner here is using Intrinsics:
It costs as much as just copying. Quite impressed! Too bad we are not fully on Core yet :/ I will try to see if 5.0 has any differences and if I can get Avx2 to work too. |
Beta Was this translation helpful? Give feedback.
-
.... you haven't posted your benchmark code, but my first question would be "are you also including the cost of allocating the array used in just- |
Beta Was this translation helpful? Give feedback.
For ~1MB block of memory, all memory copying methods in recent runtimes (Span.ToArray, Span.CopyTo, Array.Copy, Buffer.BlockCopy, Buffer.MemoryCopy, ...) end up calling memcpy. We depend on memcpy to provide efficient memory copying for large memory blocks.
For .NET Framework, you can call
Buffer.MemoryCopy
to avoid PInvoking.Buffer.MemoryCopy
always calls memcpy in .NET Framework. The other methods such as Span.CopyTo may not.Implementing memory copying that is as efficient as possible on variety of machines, for all combinations of block sizes, source and target alignment, is very complex. The typical memcpy implementa…