From f62dfa8f73f8244aca2067477b6bf658512f2eb1 Mon Sep 17 00:00:00 2001 From: Kein Date: Tue, 13 Apr 2021 03:31:55 +0300 Subject: [PATCH 1/2] Fix evil allocations, marshaling overhead StrToPtr() had at least 4 extra allocations of byte[] and char[] arrays that will make Unity's GC and frametime stability sad, taking into account you can updated 5 times per 20 seconds, which is few frames apart. Also, there was 0 reason to have that marshaling invoke overhead where it zeroes byte by byte in a loop. There is no point, we already overwrite whole memory via .Copy() and we only allocate as much as needed, no extra. No security concerns here. --- examples/button-clicker/Assets/DiscordRpc.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/button-clicker/Assets/DiscordRpc.cs b/examples/button-clicker/Assets/DiscordRpc.cs index f3b1ee5..1f709be 100644 --- a/examples/button-clicker/Assets/DiscordRpc.cs +++ b/examples/button-clicker/Assets/DiscordRpc.cs @@ -132,6 +132,8 @@ public class DiscordRpc { private RichPresenceStruct _presence; private readonly List _buffers = new List(10); + private char[] _chrBuffer = new char[128]; + private byte[] _byteBuffer = new byte[385]; // utf8 char max 4 bytes in worst case public string state; /* max 128 bytes */ public string details; /* max 128 bytes */ @@ -189,14 +191,12 @@ public class DiscordRpc private IntPtr StrToPtr(string input) { if (string.IsNullOrEmpty(input)) return IntPtr.Zero; - var convbytecnt = Encoding.UTF8.GetByteCount(input); - var buffer = Marshal.AllocHGlobal(convbytecnt + 1); - for (int i = 0; i < convbytecnt + 1; i++) - { - Marshal.WriteByte(buffer, i, 0); - } + var len = input.Length > 128 ? 128 : input.Length; + input.CopyTo(0, _chrBuffer, 0, len); + var convbytecnt = Encoding.UTF8.GetBytes(_chrBuffer, 0, len, _byteBuffer, 0); + IntPtr buffer = Marshal.AllocHGlobal(convbytecnt + 1); _buffers.Add(buffer); - Marshal.Copy(Encoding.UTF8.GetBytes(input), 0, buffer, convbytecnt); + Marshal.Copy(_byteBuffer, 0, buffer, convbytecnt); return buffer; } @@ -228,4 +228,4 @@ public class DiscordRpc } } } -} \ No newline at end of file +} From a88cabd41747ab13e61f33b463f570bd931b4983 Mon Sep 17 00:00:00 2001 From: Kein Date: Tue, 13 Apr 2021 16:55:34 +0300 Subject: [PATCH 2/2] Bring back null termination that slipped away --- examples/button-clicker/Assets/DiscordRpc.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/button-clicker/Assets/DiscordRpc.cs b/examples/button-clicker/Assets/DiscordRpc.cs index 1f709be..71e9449 100644 --- a/examples/button-clicker/Assets/DiscordRpc.cs +++ b/examples/button-clicker/Assets/DiscordRpc.cs @@ -195,6 +195,7 @@ public class DiscordRpc input.CopyTo(0, _chrBuffer, 0, len); var convbytecnt = Encoding.UTF8.GetBytes(_chrBuffer, 0, len, _byteBuffer, 0); IntPtr buffer = Marshal.AllocHGlobal(convbytecnt + 1); + Marshal.WriteByte(buffer, convbytecnt, 0); _buffers.Add(buffer); Marshal.Copy(_byteBuffer, 0, buffer, convbytecnt); return buffer;