Skip to content

Commit

Permalink
Reuse the existing buffer if the payload will fit in it.
Browse files Browse the repository at this point in the history
This avoids allocating a small (e.g., 8 byte) buffer when a MySQL packet crosses the buffer boundary.
  • Loading branch information
bgrainger committed Apr 22, 2016
1 parent 575a339 commit f59c6d8
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/MySql.Data/Serialization/PacketTransmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ private async Task<PayloadData> DoReceiveAsync(CancellationToken cancellationTok
}

// allocate a larger buffer if necessary
var readData = new byte[payloadLength];
var readData = m_buffer;
if (payloadLength > m_buffer.Length)
readData = new byte[payloadLength];
Array.Copy(m_buffer, m_offset, readData, 0, m_end - m_offset);
m_socketAwaitable.EventArgs.SetBuffer(readData, 0, 0);

Expand All @@ -157,7 +159,8 @@ private async Task<PayloadData> DoReceiveAsync(CancellationToken cancellationTok
}

// switch back to original buffer if a larger one was allocated
m_socketAwaitable.EventArgs.SetBuffer(m_buffer, 0, 0);
if (payloadLength > m_buffer.Length)
m_socketAwaitable.EventArgs.SetBuffer(m_buffer, 0, 0);

// check for error
if (readData[0] == 0xFF)
Expand Down

0 comments on commit f59c6d8

Please sign in to comment.