@@ -393,58 +393,58 @@ private static void WriteToJsonFile(string fileName, object value)
393
393
} ;
394
394
395
395
bool shouldWriteFile = true ;
396
- MemoryStream contentStream = null ;
397
396
398
397
// 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 ( ) )
402
399
{
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 ) )
418
402
{
419
- existingHasher . Append ( existingStream ) ;
403
+ serializer . Serialize ( jsonWriter , value ) ;
404
+ jsonWriter . Flush ( ) ;
405
+ streamWriter . Flush ( ) ;
420
406
}
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 ) ;
429
407
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 ) )
432
410
{
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
+ }
434
436
}
435
- }
436
437
437
- if ( shouldWriteFile )
438
- {
439
- // Write the new content to file using CopyTo
440
- using ( var fileStream = File . Create ( fileName ) )
438
+ if ( shouldWriteFile )
441
439
{
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
+ }
444
446
}
445
447
}
446
-
447
- contentStream ? . Dispose ( ) ;
448
448
}
449
449
}
450
450
}
0 commit comments