Skip to content

Commit ba29265

Browse files
Copilotbaronfel
andcommitted
Use MemoryStream.CopyTo instead of double serialization for incremental file writing
Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com>
1 parent a2b5900 commit ba29265

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

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

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

259259
bool shouldWriteFile = true;
260+
MemoryStream contentStream = null;
261+
262+
// Generate new content
263+
contentStream = new MemoryStream();
264+
writer.Write(dependencyContext, contentStream);
265+
260266
// If file exists, check if content is different using streaming hash comparison
261267
if (File.Exists(depsFilePath))
262268
{
@@ -272,12 +278,8 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item)
272278
// Hash new content using streaming approach
273279
Span<byte> newHashBuffer = stackalloc byte[XxHash64.HashSizeInBytes];
274280
var newHasher = new XxHash64();
275-
using (var memoryStream = new MemoryStream())
276-
{
277-
writer.Write(dependencyContext, memoryStream);
278-
memoryStream.Position = 0;
279-
newHasher.Append(memoryStream);
280-
}
281+
contentStream.Position = 0;
282+
newHasher.Append(contentStream);
281283
newHasher.GetCurrentHash(newHashBuffer);
282284

283285
// If hashes are equal, content is the same - don't write
@@ -289,12 +291,15 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item)
289291

290292
if (shouldWriteFile)
291293
{
292-
// Write the new content to file
294+
// Write the new content to file using CopyTo
293295
using (var fileStream = File.Create(depsFilePath))
294296
{
295-
writer.Write(dependencyContext, fileStream);
297+
contentStream.Position = 0;
298+
contentStream.CopyTo(fileStream);
296299
}
297300
}
301+
302+
contentStream?.Dispose();
298303
_filesWritten.Add(new TaskItem(depsFilePath));
299304

300305
if (ValidRuntimeIdentifierPlatformsForAssets != null)

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

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

395395
bool shouldWriteFile = true;
396+
MemoryStream contentStream = null;
397+
398+
// Generate new content
399+
contentStream = new MemoryStream();
400+
using (var streamWriter = new StreamWriter(contentStream, Encoding.UTF8, leaveOpen: true))
401+
using (var jsonWriter = new JsonTextWriter(streamWriter))
402+
{
403+
serializer.Serialize(jsonWriter, value);
404+
jsonWriter.Flush();
405+
streamWriter.Flush();
406+
}
407+
396408
// If file exists, check if content is different using streaming hash comparison
397409
if (File.Exists(fileName))
398410
{
@@ -408,31 +420,26 @@ private static void WriteToJsonFile(string fileName, object value)
408420
// Hash new content using streaming approach
409421
Span<byte> newHashBuffer = stackalloc byte[XxHash64.HashSizeInBytes];
410422
var newHasher = new XxHash64();
411-
using (var memoryStream = new MemoryStream())
412-
using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8, leaveOpen: true))
413-
using (var jsonWriter = new JsonTextWriter(streamWriter))
414-
{
415-
serializer.Serialize(jsonWriter, value);
416-
jsonWriter.Flush();
417-
streamWriter.Flush();
418-
memoryStream.Position = 0;
419-
newHasher.Append(memoryStream);
420-
}
423+
contentStream.Position = 0;
424+
newHasher.Append(contentStream);
421425
newHasher.GetCurrentHash(newHashBuffer);
422426

423427
// If hashes are equal, content is the same - don't write
424428
if (existingHashBuffer.SequenceEqual(newHashBuffer))
425429
{
430+
contentStream?.Dispose();
426431
return;
427432
}
428433
}
429434

430-
// Write the new content to file
431-
using (var fileWriter = new StreamWriter(fileName, false, Encoding.UTF8))
432-
using (var jsonWriter = new JsonTextWriter(fileWriter))
435+
// Write the new content to file using CopyTo
436+
using (var fileStream = File.Create(fileName))
433437
{
434-
serializer.Serialize(jsonWriter, value);
438+
contentStream.Position = 0;
439+
contentStream.CopyTo(fileStream);
435440
}
441+
442+
contentStream?.Dispose();
436443
}
437444
}
438445
}

0 commit comments

Comments
 (0)