Skip to content

Commit d248e2e

Browse files
committed
deduplication
1 parent 18f7a20 commit d248e2e

File tree

3 files changed

+34
-44
lines changed

3 files changed

+34
-44
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -266,28 +266,11 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item)
266266
// If file exists, check if content is different using streaming hash comparison
267267
if (File.Exists(depsFilePath))
268268
{
269-
// Get hash length from a single instance to avoid unnecessary allocations
270-
var hasher = new XxHash64();
271-
var hashLength = hasher.HashLengthInBytes;
272-
273-
// Hash existing file content using streaming approach
274-
Span<byte> existingHashBuffer = stackalloc byte[hashLength];
275-
var existingHasher = new XxHash64();
276-
using (var existingStream = File.OpenRead(depsFilePath))
277-
{
278-
existingHasher.Append(existingStream);
279-
}
280-
existingHasher.GetCurrentHash(existingHashBuffer);
281-
282-
// Hash new content using streaming approach
283-
Span<byte> newHashBuffer = stackalloc byte[hashLength];
284-
var newHasher = new XxHash64();
285-
contentStream.Position = 0;
286-
newHasher.Append(contentStream);
287-
newHasher.GetCurrentHash(newHashBuffer);
288-
269+
// stream positions are reset as part of these utility calls
270+
var existingContentHash = HashingUtils.ComputeXXHash64(File.OpenRead(depsFilePath));
271+
var newContentHash = HashingUtils.ComputeXXHash64(contentStream);
289272
// If hashes are equal, content is the same - don't write
290-
if (existingHashBuffer.SequenceEqual(newHashBuffer))
273+
if (existingContentHash.SequenceEqual(newContentHash))
291274
{
292275
shouldWriteFile = false;
293276
}
@@ -298,7 +281,6 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item)
298281
// Write the new content to file using CopyTo
299282
using (var fileStream = File.Create(depsFilePath))
300283
{
301-
contentStream.Position = 0;
302284
contentStream.CopyTo(fileStream);
303285
}
304286
}

src/Tasks/Microsoft.NET.Build.Tasks/GenerateRuntimeConfigurationFiles.cs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -408,39 +408,22 @@ private static void WriteToJsonFile(string fileName, object value)
408408
// If file exists, check if content is different using streaming hash comparison
409409
if (File.Exists(fileName))
410410
{
411-
// Get hash length from a single instance to avoid unnecessary allocations
412-
var hasher = new XxHash64();
413-
var hashLength = hasher.HashLengthInBytes;
414-
415-
// Hash existing file content using streaming approach
416-
Span<byte> existingHashBuffer = stackalloc byte[hashLength];
417-
var existingHasher = new XxHash64();
418-
using (var existingStream = File.OpenRead(fileName))
419-
{
420-
existingHasher.Append(existingStream);
421-
}
422-
existingHasher.GetCurrentHash(existingHashBuffer);
423-
424-
// Hash new content using streaming approach
425-
Span<byte> newHashBuffer = stackalloc byte[hashLength];
426-
var newHasher = new XxHash64();
427-
contentStream.Position = 0;
428-
newHasher.Append(contentStream);
429-
newHasher.GetCurrentHash(newHashBuffer);
430-
411+
// stream positions are reset as part of these utility calls
412+
var existingContentHash = HashingUtils.ComputeXXHash64(File.OpenRead(fileName));
413+
var newContentHash = HashingUtils.ComputeXXHash64(contentStream);
431414
// If hashes are equal, content is the same - don't write
432-
if (existingHashBuffer.SequenceEqual(newHashBuffer))
415+
if (existingContentHash.SequenceEqual(newContentHash))
433416
{
434417
shouldWriteFile = false;
435418
}
419+
436420
}
437421

438422
if (shouldWriteFile)
439423
{
440424
// Write the new content to file using CopyTo
441425
using (var fileStream = File.Create(fileName))
442426
{
443-
contentStream.Position = 0;
444427
contentStream.CopyTo(fileStream);
445428
}
446429
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.IO.Hashing;
5+
6+
namespace Microsoft.NET.Build.Tasks;
7+
8+
public static class HashingUtils
9+
{
10+
/// <summary>
11+
/// Computes the XxHash64 hash of a file.
12+
/// </summary>
13+
/// <param name="content">A stream to read for the hash. If the stream is seekable it will be reset to its incoming position.</param>
14+
public static byte[] ComputeXXHash64(Stream content)
15+
{
16+
var initialPosition = content.CanSeek ? content.Position : 0;
17+
var hasher = new XxHash64();
18+
hasher.Append(content);
19+
if (content.CanSeek)
20+
{
21+
content.Position = initialPosition;
22+
}
23+
return hasher.GetCurrentHash();
24+
}
25+
}

0 commit comments

Comments
 (0)