diff options
author | jhorv <38920027+jhorv@users.noreply.github.com> | 2023-03-17 08:14:50 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-17 13:14:50 +0100 |
commit | 5131b71437b36f9b876046a2fddd5465652e0f10 (patch) | |
tree | a1b8647b1f948e3f88708d9ba41b2d1338f34de9 /Ryujinx.HLE/HOS/Ipc/IpcHandleDesc.cs | |
parent | 7870423671cba17c8aad9bb93a67c84bda441366 (diff) |
Reducing memory allocations (#4537)1.1.668
* add RecyclableMemoryStream dependency and MemoryStreamManager
* organize BinaryReader/BinaryWriter extensions
* add StreamExtensions to reduce need for BinaryWriter
* simple replacments of MemoryStream with RecyclableMemoryStream
* add write ReadOnlySequence<byte> support to IVirtualMemoryManager
* avoid 0-length array creation
* rework IpcMessage and related types to greatly reduce memory allocation by using RecylableMemoryStream, keeping streams around longer, avoiding their creation when possible, and avoiding creation of BinaryReader and BinaryWriter when possible
* reduce LINQ-induced memory allocations with custom methods to query KPriorityQueue
* use RecyclableMemoryStream in StreamUtils, and use StreamUtils in EmbeddedResources
* add constants for nanosecond/millisecond conversions
* code formatting
* XML doc adjustments
* fix: StreamExtension.WriteByte not writing non-zero values for lengths <= 16
* XML Doc improvements. Implement StreamExtensions.WriteByte() block writes for large-enough count values.
* add copyless path for StreamExtension.Write(ReadOnlySpan<int>)
* add default implementation of IVirtualMemoryManager.Write(ulong, ReadOnlySequence<byte>); remove previous explicit implementations
* code style fixes
* remove LINQ completely from KScheduler/KPriorityQueue by implementing a custom struct-based enumerator
Diffstat (limited to 'Ryujinx.HLE/HOS/Ipc/IpcHandleDesc.cs')
-rw-r--r-- | Ryujinx.HLE/HOS/Ipc/IpcHandleDesc.cs | 61 |
1 files changed, 31 insertions, 30 deletions
diff --git a/Ryujinx.HLE/HOS/Ipc/IpcHandleDesc.cs b/Ryujinx.HLE/HOS/Ipc/IpcHandleDesc.cs index e6ed4613..c7ef7e9c 100644 --- a/Ryujinx.HLE/HOS/Ipc/IpcHandleDesc.cs +++ b/Ryujinx.HLE/HOS/Ipc/IpcHandleDesc.cs @@ -1,3 +1,6 @@ +using Microsoft.IO; +using Ryujinx.Common; +using Ryujinx.Common.Memory; using System; using System.IO; @@ -18,20 +21,27 @@ namespace Ryujinx.HLE.HOS.Ipc HasPId = (word & 1) != 0; - ToCopy = new int[(word >> 1) & 0xf]; - ToMove = new int[(word >> 5) & 0xf]; - PId = HasPId ? reader.ReadUInt64() : 0; - for (int index = 0; index < ToCopy.Length; index++) + int toCopySize = (word >> 1) & 0xf; + int[] toCopy = toCopySize == 0 ? Array.Empty<int>() : new int[toCopySize]; + + for (int index = 0; index < toCopy.Length; index++) { - ToCopy[index] = reader.ReadInt32(); + toCopy[index] = reader.ReadInt32(); } - for (int index = 0; index < ToMove.Length; index++) + ToCopy = toCopy; + + int toMoveSize = (word >> 5) & 0xf; + int[] toMove = toMoveSize == 0 ? Array.Empty<int>() : new int[toMoveSize]; + + for (int index = 0; index < toMove.Length; index++) { - ToMove[index] = reader.ReadInt32(); + toMove[index] = reader.ReadInt32(); } + + ToMove = toMove; } public IpcHandleDesc(int[] copy, int[] move) @@ -57,36 +67,27 @@ namespace Ryujinx.HLE.HOS.Ipc return new IpcHandleDesc(Array.Empty<int>(), handles); } - public byte[] GetBytes() + public RecyclableMemoryStream GetStream() { - using (MemoryStream ms = new MemoryStream()) - { - BinaryWriter writer = new BinaryWriter(ms); - - int word = HasPId ? 1 : 0; + RecyclableMemoryStream ms = MemoryStreamManager.Shared.GetStream(); - word |= (ToCopy.Length & 0xf) << 1; - word |= (ToMove.Length & 0xf) << 5; + int word = HasPId ? 1 : 0; - writer.Write(word); + word |= (ToCopy.Length & 0xf) << 1; + word |= (ToMove.Length & 0xf) << 5; - if (HasPId) - { - writer.Write(PId); - } + ms.Write(word); - foreach (int handle in ToCopy) - { - writer.Write(handle); - } + if (HasPId) + { + ms.Write(PId); + } - foreach (int handle in ToMove) - { - writer.Write(handle); - } + ms.Write(ToCopy); + ms.Write(ToMove); - return ms.ToArray(); - } + ms.Position = 0; + return ms; } } }
\ No newline at end of file |