From 666e05f5cb9aa9dc86874823c108586e7a5b38bd Mon Sep 17 00:00:00 2001 From: jhorv <38920027+jhorv@users.noreply.github.com> Date: Sun, 23 Apr 2023 22:06:23 -0400 Subject: Reducing Memory Allocations 202303 (#4624) * use ArrayPool, avoid 6000-7000 allocs/sec of runtime * use ArrayPool, avoid ~7k allocs/second during game execution * use ArrayPool, avoid ~3000 allocs/sec during game execution * use MemoryPool, reduce 0.5 MB/sec of new allocations during game execution * avoid over-allocation by setting List<> Capacity when known * remove LINQ in KTimeManager.UnscheduleFutureInvocation * KTimeManager - avoid spinning one more time when the time has arrived * KTimeManager - let SpinWait decide when to Thread.Yield(), and don't SpinOnce() immediately after Thread.Yield() * use MemoryPool, reduce ~175k bytes/sec allocation during game execution * IpcService - call commands via dynamic methods instead of reflection .Invoke(). Faster to call and with fewer allocations because parameters can be passed directly instead of as an array * Make ButtonMappingEntry a record struct to avoid allocations. Set the List capacity according to use. * add MemoryBuffer type for working with MemoryPool * update changes to use MemoryBuffer * make parameter ReadOnlySpan instead of Span * whitespace fix * Revert "IpcService - call commands via dynamic methods instead of reflection .Invoke(). Faster to call and with fewer allocations because parameters can be passed directly instead of as an array" This reverts commit f2c698bdf65f049e8481c9f2ec7138d9b9a8261d. * tweak KTimeManager spin behavior * replace MemoryBuffer with ByteMemoryPool modeled after System.Buffers.ArrayMemoryPool * make ByteMemoryPoolBuffer responsible for renting memory --- Ryujinx.HLE/HOS/Ipc/IpcMessage.cs | 123 ++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 59 deletions(-) (limited to 'Ryujinx.HLE/HOS/Ipc') diff --git a/Ryujinx.HLE/HOS/Ipc/IpcMessage.cs b/Ryujinx.HLE/HOS/Ipc/IpcMessage.cs index 394bf888..21630c42 100644 --- a/Ryujinx.HLE/HOS/Ipc/IpcMessage.cs +++ b/Ryujinx.HLE/HOS/Ipc/IpcMessage.cs @@ -27,98 +27,103 @@ namespace Ryujinx.HLE.HOS.Ipc public IpcMessage() { - PtrBuff = new List(); - SendBuff = new List(); - ReceiveBuff = new List(); - ExchangeBuff = new List(); - RecvListBuff = new List(); + PtrBuff = new List(0); + SendBuff = new List(0); + ReceiveBuff = new List(0); + ExchangeBuff = new List(0); + RecvListBuff = new List(0); - ObjectIds = new List(); + ObjectIds = new List(0); } - public IpcMessage(ReadOnlySpan data, long cmdPtr) : this() + public IpcMessage(ReadOnlySpan data, long cmdPtr) { using (RecyclableMemoryStream ms = MemoryStreamManager.Shared.GetStream(data)) { BinaryReader reader = new BinaryReader(ms); - Initialize(reader, cmdPtr); - } - } + int word0 = reader.ReadInt32(); + int word1 = reader.ReadInt32(); - private void Initialize(BinaryReader reader, long cmdPtr) - { - int word0 = reader.ReadInt32(); - int word1 = reader.ReadInt32(); + Type = (IpcMessageType)(word0 & 0xffff); - Type = (IpcMessageType)(word0 & 0xffff); + int ptrBuffCount = (word0 >> 16) & 0xf; + int sendBuffCount = (word0 >> 20) & 0xf; + int recvBuffCount = (word0 >> 24) & 0xf; + int xchgBuffCount = (word0 >> 28) & 0xf; - int ptrBuffCount = (word0 >> 16) & 0xf; - int sendBuffCount = (word0 >> 20) & 0xf; - int recvBuffCount = (word0 >> 24) & 0xf; - int xchgBuffCount = (word0 >> 28) & 0xf; + int rawDataSize = (word1 >> 0) & 0x3ff; + int recvListFlags = (word1 >> 10) & 0xf; + bool hndDescEnable = ((word1 >> 31) & 0x1) != 0; - int rawDataSize = (word1 >> 0) & 0x3ff; - int recvListFlags = (word1 >> 10) & 0xf; - bool hndDescEnable = ((word1 >> 31) & 0x1) != 0; + if (hndDescEnable) + { + HandleDesc = new IpcHandleDesc(reader); + } - if (hndDescEnable) - { - HandleDesc = new IpcHandleDesc(reader); - } + PtrBuff = new List(ptrBuffCount); - for (int index = 0; index < ptrBuffCount; index++) - { - PtrBuff.Add(new IpcPtrBuffDesc(reader)); - } + for (int index = 0; index < ptrBuffCount; index++) + { + PtrBuff.Add(new IpcPtrBuffDesc(reader)); + } - void ReadBuff(List buff, int count) - { - for (int index = 0; index < count; index++) + static List ReadBuff(BinaryReader reader, int count) { - buff.Add(new IpcBuffDesc(reader)); + List buff = new List(count); + + for (int index = 0; index < count; index++) + { + buff.Add(new IpcBuffDesc(reader)); + } + + return buff; } - } - ReadBuff(SendBuff, sendBuffCount); - ReadBuff(ReceiveBuff, recvBuffCount); - ReadBuff(ExchangeBuff, xchgBuffCount); + SendBuff = ReadBuff(reader, sendBuffCount); + ReceiveBuff = ReadBuff(reader, recvBuffCount); + ExchangeBuff = ReadBuff(reader, xchgBuffCount); - rawDataSize *= 4; + rawDataSize *= 4; - long recvListPos = reader.BaseStream.Position + rawDataSize; + long recvListPos = reader.BaseStream.Position + rawDataSize; // Only CMIF has the padding requirements. if (Type < IpcMessageType.TipcCloseSession) { long pad0 = GetPadSize16(reader.BaseStream.Position + cmdPtr); - if (rawDataSize != 0) - { - rawDataSize -= (int)pad0; + if (rawDataSize != 0) + { + rawDataSize -= (int)pad0; + } + + reader.BaseStream.Seek(pad0, SeekOrigin.Current); } - reader.BaseStream.Seek(pad0, SeekOrigin.Current); - } + int recvListCount = recvListFlags - 2; - int recvListCount = recvListFlags - 2; + if (recvListCount == 0) + { + recvListCount = 1; + } + else if (recvListCount < 0) + { + recvListCount = 0; + } - if (recvListCount == 0) - { - recvListCount = 1; - } - else if (recvListCount < 0) - { - recvListCount = 0; - } + RawData = reader.ReadBytes(rawDataSize); - RawData = reader.ReadBytes(rawDataSize); + reader.BaseStream.Seek(recvListPos, SeekOrigin.Begin); - reader.BaseStream.Seek(recvListPos, SeekOrigin.Begin); + RecvListBuff = new List(recvListCount); - for (int index = 0; index < recvListCount; index++) - { - RecvListBuff.Add(new IpcRecvListBuffDesc(reader.ReadUInt64())); + for (int index = 0; index < recvListCount; index++) + { + RecvListBuff.Add(new IpcRecvListBuffDesc(reader.ReadUInt64())); + } + + ObjectIds = new List(0); } } -- cgit v1.2.3