Compute map UIDs without copying all data to a MemoryStream.

We can use MergedStream to create a single combined stream with all the input and pass this to the hash function. This saves copying all the data into a MemoryStream to achieve the same goal, which requires more memory and allocations.
This commit is contained in:
RoosterDragon
2017-12-07 21:30:51 +00:00
committed by reaperrr
parent 49f0e4ebcf
commit be761de768

View File

@@ -19,6 +19,7 @@ using System.Reflection;
using System.Text; using System.Text;
using OpenRA.FileSystem; using OpenRA.FileSystem;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Primitives;
using OpenRA.Support; using OpenRA.Support;
using OpenRA.Traits; using OpenRA.Traits;
@@ -253,16 +254,27 @@ namespace OpenRA
if (!contents.Contains(required)) if (!contents.Contains(required))
throw new FileNotFoundException("Required file {0} not present in this map".F(required)); throw new FileNotFoundException("Required file {0} not present in this map".F(required));
using (var ms = new MemoryStream()) var streams = new List<Stream>();
try
{ {
foreach (var filename in contents) foreach (var filename in contents)
if (filename.EndsWith(".yaml") || filename.EndsWith(".bin") || filename.EndsWith(".lua")) if (filename.EndsWith(".yaml") || filename.EndsWith(".bin") || filename.EndsWith(".lua"))
using (var s = package.GetStream(filename)) streams.Add(package.GetStream(filename));
s.CopyTo(ms);
// Take the SHA1 // Take the SHA1
ms.Seek(0, SeekOrigin.Begin); if (streams.Count == 0)
return CryptoUtil.SHA1Hash(ms); return CryptoUtil.SHA1Hash(new byte[0]);
var merged = streams[0];
for (var i = 1; i < streams.Count; i++)
merged = new MergedStream(merged, streams[i]);
return CryptoUtil.SHA1Hash(merged);
}
finally
{
foreach (var stream in streams)
stream.Dispose();
} }
} }