From 0657b551964986fc5504a202eaa5e699d1c72f00 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 14 Sep 2023 13:33:25 +0900 Subject: [PATCH] Avoid `MemoryStream.ToArray` overhead in `LegacyByteArrayReader` --- osu.Game/Database/ImportTask.cs | 2 +- .../IO/Archives/MemoryStreamArchiveReader.cs | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 osu.Game/IO/Archives/MemoryStreamArchiveReader.cs diff --git a/osu.Game/Database/ImportTask.cs b/osu.Game/Database/ImportTask.cs index bf5b377a07..962eab9fa5 100644 --- a/osu.Game/Database/ImportTask.cs +++ b/osu.Game/Database/ImportTask.cs @@ -54,7 +54,7 @@ namespace osu.Game.Database if (ZipUtils.IsZipArchive(memoryStream)) return new ZipArchiveReader(memoryStream, Path); - return new LegacyByteArrayReader(memoryStream.ToArray(), Path); + return new MemoryStreamArchiveReader(memoryStream, Path); } // This isn't used in any current path. May need to reconsider for performance reasons (ie. if we don't expect the incoming stream to be copied out). diff --git a/osu.Game/IO/Archives/MemoryStreamArchiveReader.cs b/osu.Game/IO/Archives/MemoryStreamArchiveReader.cs new file mode 100644 index 0000000000..37ce1e508e --- /dev/null +++ b/osu.Game/IO/Archives/MemoryStreamArchiveReader.cs @@ -0,0 +1,30 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Collections.Generic; +using System.IO; + +namespace osu.Game.IO.Archives +{ + /// + /// Allows reading a single file from the provided memory stream. + /// + public class MemoryStreamArchiveReader : ArchiveReader + { + private readonly MemoryStream stream; + + public MemoryStreamArchiveReader(MemoryStream stream, string filename) + : base(filename) + { + this.stream = stream; + } + + public override Stream GetStream(string name) => new MemoryStream(stream.GetBuffer(), 0, (int)stream.Length); + + public override void Dispose() + { + } + + public override IEnumerable Filenames => new[] { Name }; + } +}