Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce allocations #342

Merged
merged 3 commits into from
Oct 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/MySqlConnector/MySqlClient/Results/ResultSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,17 @@ private ValueTask<Row> ScanRowAsync(IOBehavior ioBehavior, Row row, Cancellation
{
// if we've already read past the end of this resultset, Read returns false
if (BufferState == ResultSetState.HasMoreData || BufferState == ResultSetState.NoMoreData || BufferState == ResultSetState.None)
return new ValueTask<Row>((Row)null);
return new ValueTask<Row>((Row) null);

using (Command.RegisterCancel(cancellationToken))
{
var payloadValueTask = Session.ReceiveReplyAsync(ioBehavior, CancellationToken.None);
return payloadValueTask.IsCompletedSuccessfully
? new ValueTask<Row>(ScanRowAsyncRemainder(payloadValueTask.Result))
: new ValueTask<Row>(ScanRowAsyncAwaited(payloadValueTask.AsTask(), cancellationToken));
? new ValueTask<Row>(ScanRowAsyncRemainder(payloadValueTask.Result, row))
: new ValueTask<Row>(ScanRowAsyncAwaited(payloadValueTask.AsTask(), row, cancellationToken));
}

async Task<Row> ScanRowAsyncAwaited(Task<PayloadData> payloadTask, CancellationToken token)
async Task<Row> ScanRowAsyncAwaited(Task<PayloadData> payloadTask, Row row_, CancellationToken token)
{
PayloadData payloadData;
try
Expand All @@ -191,10 +191,10 @@ async Task<Row> ScanRowAsyncAwaited(Task<PayloadData> payloadTask, CancellationT
token.ThrowIfCancellationRequested();
throw;
}
return ScanRowAsyncRemainder(payloadData);
return ScanRowAsyncRemainder(payloadData, row_);
}

Row ScanRowAsyncRemainder(PayloadData payload)
Row ScanRowAsyncRemainder(PayloadData payload, Row row_)
{
if (payload.HeaderByte == EofPayload.Signature)
{
Expand Down Expand Up @@ -223,12 +223,12 @@ Row ScanRowAsyncRemainder(PayloadData payload)
reader.Offset += m_dataLengths[column];
}

if (row == null)
row = new Row(this);
row.SetData(m_dataLengths, m_dataOffsets, payload.ArraySegment);
m_rowBuffered = row;
if (row_ == null)
row_ = new Row(this);
row_.SetData(m_dataLengths, m_dataOffsets, payload.ArraySegment);
m_rowBuffered = row_;
m_hasRows = true;
return row;
return row_;
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/MySqlConnector/Protocol/Serialization/Packet.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System;
using System;

namespace MySql.Data.Protocol.Serialization
{
internal class Packet
internal struct Packet
{
public Packet(int sequenceNumber, ArraySegment<byte> contents)
public Packet(ArraySegment<byte> contents)
{
SequenceNumber = sequenceNumber;
Contents = contents;
}

public int SequenceNumber { get; }
public ArraySegment<byte> Contents { get; }
}
}
23 changes: 8 additions & 15 deletions src/MySqlConnector/Protocol/Serialization/ProtocolUtility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Buffers;
using System.IO;
using System.Threading.Tasks;
Expand Down Expand Up @@ -45,16 +45,16 @@ private static ValueTask<Packet> ReadPacketAfterHeader(ArraySegment<byte> header

var payloadBytesTask = bufferedByteReader.ReadBytesAsync(byteHandler, payloadLength, ioBehavior);
if (payloadBytesTask.IsCompleted)
return CreatePacketFromPayload(payloadBytesTask.Result, payloadLength, packetSequenceNumber, protocolErrorBehavior);
return AddContinuation(payloadBytesTask, payloadLength, packetSequenceNumber, protocolErrorBehavior);
return CreatePacketFromPayload(payloadBytesTask.Result, payloadLength, protocolErrorBehavior);
return AddContinuation(payloadBytesTask, payloadLength, protocolErrorBehavior);

// NOTE: use a local function (with no captures) to defer creation of lambda objects
ValueTask<Packet> AddContinuation(ValueTask<ArraySegment<byte>> payloadBytesTask_, int payloadLength_, int packetSequenceNumber_, ProtocolErrorBehavior protocolErrorBehavior_)
=> payloadBytesTask_.ContinueWith(x => CreatePacketFromPayload(x, payloadLength_, packetSequenceNumber_, protocolErrorBehavior_));
ValueTask<Packet> AddContinuation(ValueTask<ArraySegment<byte>> payloadBytesTask_, int payloadLength_, ProtocolErrorBehavior protocolErrorBehavior_)
=> payloadBytesTask_.ContinueWith(x => CreatePacketFromPayload(x, payloadLength_, protocolErrorBehavior_));
}

private static ValueTask<Packet> CreatePacketFromPayload(ArraySegment<byte> payloadBytes, int payloadLength, int packetSequenceNumber, ProtocolErrorBehavior protocolErrorBehavior) =>
payloadBytes.Count >= payloadLength ? new ValueTask<Packet>(new Packet(packetSequenceNumber, payloadBytes)) :
private static ValueTask<Packet> CreatePacketFromPayload(ArraySegment<byte> payloadBytes, int payloadLength, ProtocolErrorBehavior protocolErrorBehavior) =>
payloadBytes.Count >= payloadLength ? new ValueTask<Packet>(new Packet(payloadBytes)) :
protocolErrorBehavior == ProtocolErrorBehavior.Throw ? ValueTaskExtensions.FromException<Packet>(new EndOfStreamException()) :
default(ValueTask<Packet>);

Expand All @@ -69,8 +69,7 @@ private static ValueTask<ArraySegment<byte>> DoReadPayloadAsync(BufferedByteRead
var readPacketTask = ReadPacketAsync(bufferedByteReader, byteHandler, getNextSequenceNumber, protocolErrorBehavior, ioBehavior);
while (readPacketTask.IsCompleted)
{
ValueTask<ArraySegment<byte>> result;
if (HasReadPayload(previousPayloads, readPacketTask.Result, protocolErrorBehavior, out result))
if (HasReadPayload(previousPayloads, readPacketTask.Result, protocolErrorBehavior, out var result))
return result;

readPacketTask = ReadPacketAsync(bufferedByteReader, byteHandler, getNextSequenceNumber, protocolErrorBehavior, ioBehavior);
Expand All @@ -89,12 +88,6 @@ ValueTask<ArraySegment<byte>> AddContinuation(ValueTask<Packet> readPacketTask_,

private static bool HasReadPayload(ArraySegmentHolder<byte> previousPayloads, Packet packet, ProtocolErrorBehavior protocolErrorBehavior, out ValueTask<ArraySegment<byte>> result)
{
if (packet == null && protocolErrorBehavior == ProtocolErrorBehavior.Ignore)
{
result = default(ValueTask<ArraySegment<byte>>);
return true;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packet will never be null now, because I made it a struct. OTOH, I'm not sure if it was ever null before, because it's dereferenced later in the method without a null check. (If it was possible that default(Packet) could come through, then packet.Contents.Array might throw a NullReferenceException later in the method.)


if (previousPayloads.Count == 0 && packet.Contents.Count < MaxPacketSize)
{
result = new ValueTask<ArraySegment<byte>>(packet.Contents);
Expand Down
2 changes: 1 addition & 1 deletion tests/Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp1.1;net462;netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>netcoreapp1.1;net47;netcoreapp2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.8" />
Expand Down
4 changes: 2 additions & 2 deletions tests/Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Data.Common;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
Expand All @@ -24,7 +24,7 @@ static void Main()
.With(JitOptimizationsValidator.FailOnError)
.With(MemoryDiagnoser.Default)
.With(StatisticColumn.AllStatistics)
.With(Job.Default.With(Runtime.Clr).With(Jit.RyuJit).With(Platform.X64).With(CsProjClassicNetToolchain.Net462).WithId("net462"))
.With(Job.Default.With(Runtime.Clr).With(Jit.RyuJit).With(Platform.X64).With(CsProjClassicNetToolchain.Net47).WithId("net47"))
.With(Job.Default.With(Runtime.Core).With(CsProjCoreToolchain.NetCoreApp11).WithId("netcore11"))
.With(Job.Default.With(Runtime.Core).With(CsProjCoreToolchain.NetCoreApp20).WithId("netcore20"))
.With(DefaultExporters.Csv);
Expand Down