Skip to content

Commit 42d55e3

Browse files
Copilotbaronfel
andcommitted
Improve resource management and optimize hash length access in incremental tasks
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
1 parent 830621a commit 42d55e3

File tree

2 files changed

+76
-76
lines changed

2 files changed

+76
-76
lines changed

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -257,52 +257,52 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item)
257257
var writer = new DependencyContextWriter();
258258

259259
bool shouldWriteFile = true;
260-
MemoryStream contentStream = null;
261260

262261
// Generate new content
263-
contentStream = new MemoryStream();
264-
writer.Write(dependencyContext, contentStream);
265-
266-
// If file exists, check if content is different using streaming hash comparison
267-
if (File.Exists(depsFilePath))
262+
using (var contentStream = new MemoryStream())
268263
{
269-
// Get hash length from instance
270-
var hashLength = new XxHash64().HashLengthInBytes;
271-
272-
// Hash existing file content using streaming approach
273-
Span<byte> existingHashBuffer = stackalloc byte[hashLength];
274-
var existingHasher = new XxHash64();
275-
using (var existingStream = File.OpenRead(depsFilePath))
276-
{
277-
existingHasher.Append(existingStream);
278-
}
279-
existingHasher.GetCurrentHash(existingHashBuffer);
280-
281-
// Hash new content using streaming approach
282-
Span<byte> newHashBuffer = stackalloc byte[hashLength];
283-
var newHasher = new XxHash64();
284-
contentStream.Position = 0;
285-
newHasher.Append(contentStream);
286-
newHasher.GetCurrentHash(newHashBuffer);
264+
writer.Write(dependencyContext, contentStream);
287265

288-
// If hashes are equal, content is the same - don't write
289-
if (existingHashBuffer.SequenceEqual(newHashBuffer))
266+
// If file exists, check if content is different using streaming hash comparison
267+
if (File.Exists(depsFilePath))
290268
{
291-
shouldWriteFile = false;
269+
// Get hash length from a single instance to avoid unnecessary allocations
270+
using 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+
289+
// If hashes are equal, content is the same - don't write
290+
if (existingHashBuffer.SequenceEqual(newHashBuffer))
291+
{
292+
shouldWriteFile = false;
293+
}
292294
}
293-
}
294295

295-
if (shouldWriteFile)
296-
{
297-
// Write the new content to file using CopyTo
298-
using (var fileStream = File.Create(depsFilePath))
296+
if (shouldWriteFile)
299297
{
300-
contentStream.Position = 0;
301-
contentStream.CopyTo(fileStream);
298+
// Write the new content to file using CopyTo
299+
using (var fileStream = File.Create(depsFilePath))
300+
{
301+
contentStream.Position = 0;
302+
contentStream.CopyTo(fileStream);
303+
}
302304
}
303305
}
304-
305-
contentStream?.Dispose();
306306
_filesWritten.Add(new TaskItem(depsFilePath));
307307

308308
if (ValidRuntimeIdentifierPlatformsForAssets != null)

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

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -393,58 +393,58 @@ private static void WriteToJsonFile(string fileName, object value)
393393
};
394394

395395
bool shouldWriteFile = true;
396-
MemoryStream contentStream = null;
397396

398397
// Generate new content
399-
contentStream = new MemoryStream();
400-
using (var streamWriter = new StreamWriter(contentStream, Encoding.UTF8, 1024, true))
401-
using (var jsonWriter = new JsonTextWriter(streamWriter))
398+
using (var contentStream = new MemoryStream())
402399
{
403-
serializer.Serialize(jsonWriter, value);
404-
jsonWriter.Flush();
405-
streamWriter.Flush();
406-
}
407-
408-
// If file exists, check if content is different using streaming hash comparison
409-
if (File.Exists(fileName))
410-
{
411-
// Get hash length from instance
412-
var hashLength = new XxHash64().HashLengthInBytes;
413-
414-
// Hash existing file content using streaming approach
415-
Span<byte> existingHashBuffer = stackalloc byte[hashLength];
416-
var existingHasher = new XxHash64();
417-
using (var existingStream = File.OpenRead(fileName))
400+
using (var streamWriter = new StreamWriter(contentStream, Encoding.UTF8, 1024, true))
401+
using (var jsonWriter = new JsonTextWriter(streamWriter))
418402
{
419-
existingHasher.Append(existingStream);
403+
serializer.Serialize(jsonWriter, value);
404+
jsonWriter.Flush();
405+
streamWriter.Flush();
420406
}
421-
existingHasher.GetCurrentHash(existingHashBuffer);
422-
423-
// Hash new content using streaming approach
424-
Span<byte> newHashBuffer = stackalloc byte[hashLength];
425-
var newHasher = new XxHash64();
426-
contentStream.Position = 0;
427-
newHasher.Append(contentStream);
428-
newHasher.GetCurrentHash(newHashBuffer);
429407

430-
// If hashes are equal, content is the same - don't write
431-
if (existingHashBuffer.SequenceEqual(newHashBuffer))
408+
// If file exists, check if content is different using streaming hash comparison
409+
if (File.Exists(fileName))
432410
{
433-
shouldWriteFile = false;
411+
// Get hash length from a single instance to avoid unnecessary allocations
412+
using 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+
431+
// If hashes are equal, content is the same - don't write
432+
if (existingHashBuffer.SequenceEqual(newHashBuffer))
433+
{
434+
shouldWriteFile = false;
435+
}
434436
}
435-
}
436437

437-
if (shouldWriteFile)
438-
{
439-
// Write the new content to file using CopyTo
440-
using (var fileStream = File.Create(fileName))
438+
if (shouldWriteFile)
441439
{
442-
contentStream.Position = 0;
443-
contentStream.CopyTo(fileStream);
440+
// Write the new content to file using CopyTo
441+
using (var fileStream = File.Create(fileName))
442+
{
443+
contentStream.Position = 0;
444+
contentStream.CopyTo(fileStream);
445+
}
444446
}
445447
}
446-
447-
contentStream?.Dispose();
448448
}
449449
}
450450
}

0 commit comments

Comments
 (0)