我正在为了速度而重写我的应用程序。它基本上使用生产者/消费者模式来处理文件数据,以并行计算例如多个哈希。
其中一项更改是将字节[]块切换为ReadOnlySpan。这导致了一些不错的加速和代码简化。
只有一个市长抓住:我必须删除框架支持的所有HashAlgorithms,因为到目前为止它们还不支持处理部分Span数据。删除它们或将数据复制到字节数组会受到惩罚。支持Span的唯一方法是TryComputeHash,它需要完整的数据。由于要处理的文件可能很大,所以对我来说这没有用。
所以这是我的实际问题:是否可以使用任何(可能不安全的)“ hack”将Span数据传递给HashAlgorithm对象?]
protected override void DoWork(CancellationToken ct) {
HashAlgorithm.Initialize();
ReadOnlySpan<byte> block;
int bytesProcessed;
do {
ct.ThrowIfCancellationRequested();
block = Reader.GetBlock(ReadLength);
//Make this work
bytesProcessed = HashAlgorithm.TransformBlock((byte[])block, 0, block.Length, null, 0);
} while(Reader.Advance(bytesProcessed) && bytesProcessed != 0);
var lastBytes = block.Length - bytesProcessed;
//Make this work
HashValue = HashAlgorithm.TransformFinalBlock((byte[])block.Slice(bytesProcessed, lastBytes), 0, lastBytes).ToArray();
Reader.Advance(lastBytes);
}
我正在为了速度而重写我的应用程序。它基本上使用生产者/消费者模式来处理文件数据,以并行计算例如多个哈希。更改之一是从...
incrementalhash建议用于您的情况。它可以处理Span<byte>
类型的连续数据。