In some situations the MemoryMappedViewAccessor class just doesn’t cut it for reading bytes efficiently; the best we get is the generic ReadArray<byte> which it the route for all structs and involves several unnecessary steps when you just need bytes.
It’s possible to use a MemoryMappedViewStream, but because it’s based on a Stream you need to seek to the correct position first, and then the read operation itself has many more unnecessary steps.
Is there a quick, high-performance way to read an array of bytes from a memory-mapped file in .NET, given that it should just be a particular area of the address space to read from?
This solution requires unsafe code (compile with
/unsafeswitch), but grabs a pointer to the memory directly; thenMarshal.Copycan be used. This is much, much faster than the methods provided by the .NET framework.