var file = MemoryMappedFile.CreateFromFile(...);
var accessor = file.CreateViewAccessor();
var bytes = new byte[yourLength];
// assuming the string is at the start of the file
// aka position: 0
// https://msdn.microsoft.com/en-us/library/dd267761(v=vs.110).aspx
accessor.ReadArray<byte>(
position: 0, // The number of bytes in the accessor at which to begin reading
array: bytes, // The array to contain the structures read from the accessor
offset: 0, // The index in `array` in which to place the first copied structure
count: yourLength // The number of structures of type T to read from the accessor.
);
var myString = Encoding.UTF8.GetString(bytes);
5条答案
按热度按时间k10s72fa1#
这个解决方案需要unsafe代码(用
/unsafe
开关编译),但是直接抓取指向内存的指针;则可以使用Marshal.Copy
。这比.NET框架提供的方法快得多。r6vfmomb2#
请参阅此错误报告:无法确定MemoryMappedViewAccessor使用的内部偏移量-使SafeMemoryMappedViewHandle属性不可用。
从报告中可以看出:
MemoryMappedViewAccessor具有SafeMemoryMappedViewHandle属性,该属性返回由MemoryMappedView在内部使用的ViewHandle,但没有任何属性返回由MemoryMappedView使用的偏移量。
由于MemoryMappedView正在对MemoryMappedFile.CreateViewAccessor(offset,size)中请求的偏移量进行页面对齐,因此在不知道偏移量的情况下,无法使用SafeMemoryMappedViewHandle进行任何有用的操作。
注意,我们实际上要做的是使用AcquirePointer(ref byte* pointer)方法来允许运行一些基于快速指针的(可能是非托管的)代码。我们可以将指针页面对齐,但必须能够找出从最初请求的地址的偏移量。
1cosmwyk3#
我知道这是一个已经回答过的老问题,但我想补充我的两分钱。
我使用接受的答案(使用不安全代码)和MemoryMappedViewStream方法运行了一个测试,以阅读200MB字节数组。
内存Map视图流
我用每种方法运行了3次测试,并收到了以下时间。
内存Map视图流:
不安全的方法
从少量的测试来看,
MemoryMappedViewStream
似乎有一个 * 非常轻微 * 的优势。考虑到这一点,任何人阅读这篇文章的道路上,我会去与MemoryMappedViewStream
。euoag5mw4#
此解决方案的安全版本是:
我已经测试过了,它确实有效。我不能评论它的性能,或者它是否是最好的整体解决方案,只是它的工作。
ercv8c1e5#
只想共享一个longl_offset的版本(这样就可以读取\写入大于int32最大大小的文件):