Skip to content

Commit

Permalink
perf: Removed global send lock
Browse files Browse the repository at this point in the history
  • Loading branch information
TwoTenPvP committed Dec 11, 2019
1 parent 5a0657b commit fafe3a6
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions Ruffles/Messaging/PacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ internal static void HandleIncomingMessage(ArraySegment<byte> payload, Connectio
}
}

// Lock when sending messages to prevent a pointer from being overwritten.
private static readonly object _memoryPointerLock = new object();

internal static void SendMessage(ArraySegment<byte> payload, Connection connection, byte channelId, bool noDelay, MemoryManager memoryManager)
{
if (channelId < 0 || channelId >= connection.Channels.Length)
Expand All @@ -91,29 +88,26 @@ internal static void SendMessage(ArraySegment<byte> payload, Connection connecti

IChannel channel = connection.Channels[channelId];

lock (_memoryPointerLock)
HeapPointers memoryPointers = channel.CreateOutgoingMessage(payload, out byte headerSize, out bool dealloc);

if (memoryPointers != null)
{
HeapPointers memoryPointers = channel.CreateOutgoingMessage(payload, out byte headerSize, out bool dealloc);
for (int i = 0; i < memoryPointers.VirtualCount; i++)
{
connection.SendRaw(new ArraySegment<byte>(((HeapMemory)memoryPointers.Pointers[i]).Buffer, (int)((HeapMemory)memoryPointers.Pointers[i]).VirtualOffset, (int)((HeapMemory)memoryPointers.Pointers[i]).VirtualCount), noDelay, headerSize);
}

if (memoryPointers != null)
if (dealloc)
{
// DeAlloc the memory again. This is done for unreliable channels that dont need the message after the initial send.
for (int i = 0; i < memoryPointers.VirtualCount; i++)
{
connection.SendRaw(new ArraySegment<byte>(((HeapMemory)memoryPointers.Pointers[i]).Buffer, (int)((HeapMemory)memoryPointers.Pointers[i]).VirtualOffset, (int)((HeapMemory)memoryPointers.Pointers[i]).VirtualCount), noDelay, headerSize);
}

if (dealloc)
{
// DeAlloc the memory again. This is done for unreliable channels that dont need the message after the initial send.
for (int i = 0; i < memoryPointers.VirtualCount; i++)
{
memoryManager.DeAlloc(((HeapMemory)memoryPointers.Pointers[i]));
}
memoryManager.DeAlloc(((HeapMemory)memoryPointers.Pointers[i]));
}

// Dealloc the array always.
memoryManager.DeAlloc(memoryPointers);
}

// Dealloc the array always.
memoryManager.DeAlloc(memoryPointers);
}
}
}
Expand Down

0 comments on commit fafe3a6

Please sign in to comment.