Skip to content

Commit

Permalink
minimal parallelism
Browse files Browse the repository at this point in the history
  • Loading branch information
NobodysNightmare committed Dec 27, 2013
1 parent 89683e7 commit f935488
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions CompareFS/PathComparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Threading.Tasks;

namespace CompareFS
{
Expand Down Expand Up @@ -60,7 +61,7 @@ private void CompareFiles(DirectoryInfo referenceDirectory, DirectoryInfo target
{
foreach (Pair<FileInfo> pair in EnumerateCommonFilesLeftAndRight(targetDirectory, referenceDirectory))
{
if (!CompareFiles(pair.Left, pair.Right))
if (!CompareFilesAsync(pair.Left, pair.Right).Result)
{
Listener.OnModification(new FileModification(pair.Left, ModificationType.Changed));
}
Expand All @@ -78,16 +79,24 @@ private IEnumerable<Pair<FileInfo>> EnumerateCommonFilesLeftAndRight(DirectoryIn
return leftFiles.EnumerateFiles().Where(f => !ExcludedFileExtensions.Contains(f.Extension)).Where(f => rightFiles.HasFile(f)).Select(left => new Pair<FileInfo>(left, rightFiles.GetCorrespondingFile(left)));
}

private bool CompareFiles(FileInfo fileInfo1, FileInfo fileInfo2)
private async Task<bool> CompareFilesAsync(FileInfo fileInfo1, FileInfo fileInfo2)
{
SHA1 hashAlgorithm = SHA1.Create();

using (var stream1 = fileInfo1.OpenRead())
using (var stream2 = fileInfo2.OpenRead())
{
byte[] hash1 = hashAlgorithm.ComputeHash(stream1);
byte[] hash2 = hashAlgorithm.ComputeHash(stream2);
Task<byte[]> hash1 = ComputeHashAsync(stream1);
Task<byte[]> hash2 = ComputeHashAsync(stream2);

return ByteArrayCompare(hash1, hash2);
return ByteArrayCompare(await hash1, await hash2);
}
}

private async Task<byte[]> ComputeHashAsync(Stream source)
{
using (SHA1 hashAlgorithm = SHA1.Create())
{
return await Task.Factory.StartNew<byte[]>(() => hashAlgorithm.ComputeHash(source));
}
}

Expand Down

0 comments on commit f935488

Please sign in to comment.