Skip to content

Best way of copying (unmanaged) memory #41499

Answered by jkotas
ldematte asked this question in General
Discussion options

You must be logged in to vote

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.

.NET Core 3.1+ you could use a vectorized approach / intrinsics.

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…

Replies: 5 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@gfoidl
Comment options

@ldematte
Comment options

Answer selected by ldematte
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@ldematte
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants