提交 8fc3f6ee 编写于 作者: C CyrusNajmabadi

Use a pooled array to create hashes from.

上级 0f8b7ff4
......@@ -14,31 +14,53 @@ namespace Microsoft.CodeAnalysis
// all these are just helper methods
internal partial class Checksum
{
private static readonly Stack<SHA1> s_sha1Stack = new Stack<SHA1>();
private static readonly Stack<IncrementalHash> s_hashStack = new Stack<IncrementalHash>();
private static readonly object s_gate = new object();
public static Checksum Create(Stream stream)
{
SHA1 sha1;
IncrementalHash hash;
lock (s_gate)
{
sha1 = s_sha1Stack.Count > 0
? s_sha1Stack.Pop()
: SHA1.Create();
hash = s_hashStack.Count > 0
? s_hashStack.Pop()
: IncrementalHash.CreateHash(HashAlgorithmName.SHA1);
}
stream.Seek(0, SeekOrigin.Begin);
var checksum = new Checksum(sha1.ComputeHash(stream));
var checksum = ComputeChecksum(stream, hash);
lock (s_gate)
{
s_sha1Stack.Push(sha1);
s_hashStack.Push(hash);
}
return checksum;
}
private static Checksum ComputeChecksum(Stream stream, IncrementalHash hash)
{
using (var pooledBuffer = SharedPools.ByteArray.GetPooledObject())
{
stream.Seek(0, SeekOrigin.Begin);
var buffer = pooledBuffer.Object;
var bufferLength = buffer.Length;
int bytesRead;
do
{
bytesRead = stream.Read(buffer, 0, bufferLength);
if (bytesRead > 0)
{
hash.AppendData(buffer, 0, bytesRead);
}
}
while (bytesRead > 0);
var bytes = hash.GetHashAndReset();
return new Checksum(bytes);
}
}
public static Checksum Create(string kind, IObjectWritable @object)
{
using (var stream = SerializableBytes.CreateWritableStream())
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册