Presize MemoryStream when possible.

Also use GetBuffer when we know we have presized the stream to the exact required size to prevent a needless copy.
This commit is contained in:
RoosterDragon
2017-12-07 21:28:43 +00:00
committed by abcdefg30
parent 5d8c9a560a
commit ca01a1f186
7 changed files with 19 additions and 13 deletions

View File

@@ -44,7 +44,7 @@ namespace OpenRA.FileSystem
using (var z = pkg.GetInputStream(entry))
{
var ms = new MemoryStream();
var ms = new MemoryStream((int)entry.Size);
z.CopyTo(ms);
ms.Seek(0, SeekOrigin.Begin);
return ms;
@@ -104,7 +104,13 @@ namespace OpenRA.FileSystem
// SharpZipLib breaks when asked to update archives loaded from outside streams or files
// We can work around this by creating a clean in-memory-only file, cutting all outside references
if (!create)
new MemoryStream(File.ReadAllBytes(filename)).CopyTo(pkgStream);
{
using (var copy = new MemoryStream(File.ReadAllBytes(filename)))
{
pkgStream.Capacity = (int)copy.Length;
copy.CopyTo(pkgStream);
}
}
pkgStream.Position = 0;
pkg = ZipFileHelper.Create(pkgStream);