Skip to content

Use span directly instead of reader / writer for methods & properties #936

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

Merged
merged 9 commits into from
Aug 31, 2020
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
6 changes: 6 additions & 0 deletions RabbitMQDotNetClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RabbitMQ.Client", "projects
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unit", "projects\Unit\Unit.csproj", "{B8FAC024-CC03-4067-9FFC-02846FB8AE48}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Benchmarks", "projects\Benchmarks\Benchmarks.csproj", "{38D72C9A-68E9-4653-B0CE-C7BA9FFD91D0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -26,6 +28,10 @@ Global
{B8FAC024-CC03-4067-9FFC-02846FB8AE48}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B8FAC024-CC03-4067-9FFC-02846FB8AE48}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B8FAC024-CC03-4067-9FFC-02846FB8AE48}.Release|Any CPU.Build.0 = Release|Any CPU
{38D72C9A-68E9-4653-B0CE-C7BA9FFD91D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{38D72C9A-68E9-4653-B0CE-C7BA9FFD91D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38D72C9A-68E9-4653-B0CE-C7BA9FFD91D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38D72C9A-68E9-4653-B0CE-C7BA9FFD91D0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
18 changes: 18 additions & 0 deletions projects/Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<AssemblyOriginatorKeyFile>../rabbit.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\RabbitMQ.Client\RabbitMQ.Client.csproj" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions projects/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using BenchmarkDotNet.Running;

namespace Benchmarks
{
public static class Program
{
public static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}
}
24 changes: 24 additions & 0 deletions projects/Benchmarks/WireFormatting/WireFormatting_Read_BasicAck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using BenchmarkDotNet.Attributes;
using RabbitMQ.Client.Framing.Impl;

namespace Benchmarks.WireFormatting
{
[ShortRunJob]
[MemoryDiagnoser]
public class WireFormatting_Read_BasicAck
{
private readonly byte[] _buffer = new byte[1024];

public WireFormatting_Read_BasicAck()
{
new BasicAck(ulong.MaxValue, true).WriteArgumentsTo(_buffer);
}

[Benchmark(Baseline = true)]
public object ReadFromSpan()
{
return new BasicAck(new ReadOnlySpan<byte>(_buffer));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using BenchmarkDotNet.Attributes;
using BasicDeliver = RabbitMQ.Client.Framing.Impl.BasicDeliver;

namespace Benchmarks.WireFormatting
{
[ShortRunJob]
[MemoryDiagnoser]
public class WireFormatting_Read_BasicDeliver
{
private readonly byte[] _buffer = new byte[1024];

public WireFormatting_Read_BasicDeliver()
{
new BasicDeliver(string.Empty, 0, false, string.Empty, string.Empty).WriteArgumentsTo(_buffer);
}

[Benchmark(Baseline = true)]
public object ReadFromSpan()
{
return new BasicDeliver(new ReadOnlySpan<byte>(_buffer));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using BenchmarkDotNet.Attributes;
using BasicProperties = RabbitMQ.Client.Framing.BasicProperties;

namespace Benchmarks.WireFormatting
{
[ShortRunJob]
[MemoryDiagnoser]
public class WireFormatting_Read_BasicProperties
{
private readonly byte[] _buffer = new byte[1024];

public WireFormatting_Read_BasicProperties()
{
new BasicProperties
{
Persistent = true,
AppId = "AppId",
ContentEncoding = "content"
}.WritePropertiesTo(_buffer);
}

[Benchmark(Baseline = true)]
public object ReadFromSpan()
{
return new BasicProperties(new ReadOnlySpan<byte>(_buffer));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using BenchmarkDotNet.Attributes;
using RabbitMQ.Client.Framing.Impl;

namespace Benchmarks.WireFormatting
{
[ShortRunJob]
[MemoryDiagnoser]
public class WireFormatting_Read_ChannelClose
{
private readonly byte[] _buffer = new byte[1024];

public WireFormatting_Read_ChannelClose()
{
new ChannelClose(333, string.Empty, 0099, 2999).WriteArgumentsTo(_buffer);
}

[Benchmark(Baseline = true)]
public object ReadFromSpan()
{
return new ChannelClose(new ReadOnlySpan<byte>(_buffer));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using BenchmarkDotNet.Attributes;
using RabbitMQ.Client.Framing.Impl;

namespace Benchmarks.WireFormatting
{
[ShortRunJob]
[MemoryDiagnoser]
public class WireFormatting_Write_BasicAck
{
private readonly byte[] _buffer = new byte[1024];
private readonly BasicAck _method = new BasicAck(ulong.MaxValue, true);

[Benchmark(Baseline = true)]
public int WriteArgumentsTo()
{
return _method.WriteArgumentsTo(_buffer);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using BenchmarkDotNet.Attributes;
using BasicDeliver = RabbitMQ.Client.Framing.Impl.BasicDeliver;

namespace Benchmarks.WireFormatting
{
[ShortRunJob]
[MemoryDiagnoser]
public class WireFormatting_Write_BasicDeliver
{
private readonly byte[] _buffer = new byte[1024];
private readonly BasicDeliver _method = new BasicDeliver(string.Empty, 0, false, string.Empty, string.Empty);

[Benchmark(Baseline = true)]
public int WriteArgumentsTo()
{
return _method.WriteArgumentsTo(_buffer);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using BenchmarkDotNet.Attributes;
using BasicProperties = RabbitMQ.Client.Framing.BasicProperties;

namespace Benchmarks.WireFormatting
{
[ShortRunJob]
[MemoryDiagnoser]
public class WireFormatting_Write_BasicProperties
{
private readonly byte[] _buffer = new byte[1024];
private readonly BasicProperties _properties = new BasicProperties
{
Persistent = true,
AppId = "AppId",
ContentEncoding = "content",
};

[Benchmark(Baseline = true)]
public void WritePropertiesToSpan()
{
_properties.WritePropertiesTo(new Span<byte>(_buffer));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using BenchmarkDotNet.Attributes;
using RabbitMQ.Client.Framing.Impl;

namespace Benchmarks.WireFormatting
{
[ShortRunJob]
[MemoryDiagnoser]
public class WireFormatting_Write_ChannelClose
{
private readonly byte[] _buffer = new byte[1024];
private readonly ChannelClose _method = new ChannelClose(333, string.Empty, 0099, 2999);

[Benchmark(Baseline = true)]
public int WriteArgumentsTo()
{
return _method.WriteArgumentsTo(_buffer);
}
}
}
3 changes: 3 additions & 0 deletions projects/RabbitMQ.Client/RabbitMQ.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Unit, PublicKey=00240000048000009400000006020000002400005253413100040000010001008d20ec856aeeb8c3153a77faa2d80e6e43b5db93224a20cc7ae384f65f142e89730e2ff0fcc5d578bbe96fa98a7196c77329efdee4579b3814c0789e5a39b51df6edd75b602a33ceabdfcf19a3feb832f31d8254168cd7ba5700dfbca301fbf8db614ba41ba18474de0a5f4c2d51c995bc3636c641c8cbe76f45717bfcb943b5</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Benchmarks, PublicKey=00240000048000009400000006020000002400005253413100040000010001008d20ec856aeeb8c3153a77faa2d80e6e43b5db93224a20cc7ae384f65f142e89730e2ff0fcc5d578bbe96fa98a7196c77329efdee4579b3814c0789e5a39b51df6edd75b602a33ceabdfcf19a3feb832f31d8254168cd7ba5700dfbca301fbf8db614ba41ba18474de0a5f4c2d51c995bc3636c641c8cbe76f45717bfcb943b5</_Parameter1>
</AssemblyAttribute>
</ItemGroup>

<ItemGroup>
Expand Down
21 changes: 11 additions & 10 deletions projects/RabbitMQ.Client/client/framing/BasicAck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
//---------------------------------------------------------------------------

using System;
using RabbitMQ.Client.client.framing;
using RabbitMQ.Client.Impl;

namespace RabbitMQ.Client.Framing.Impl
{
Expand All @@ -48,21 +50,20 @@ public BasicAck(ulong DeliveryTag, bool Multiple)
_multiple = Multiple;
}

public BasicAck(ReadOnlySpan<byte> span)
{
int offset = WireFormatting.ReadLonglong(span, out _deliveryTag);
WireFormatting.ReadBits(span.Slice(offset), out _multiple);
}

public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicAck;
public override string ProtocolMethodName => "basic.ack";
public override bool HasContent => false;

public override void ReadArgumentsFrom(ref Client.Impl.MethodArgumentReader reader)
{
_deliveryTag = reader.ReadLonglong();
_multiple = reader.ReadBit();
}

public override void WriteArgumentsTo(ref Client.Impl.MethodArgumentWriter writer)
public override int WriteArgumentsTo(Span<byte> span)
{
writer.WriteLonglong(_deliveryTag);
writer.WriteBit(_multiple);
writer.EndBits();
int offset = WireFormatting.WriteLonglong(span, _deliveryTag);
return offset + WireFormatting.WriteBits(span.Slice(offset), _multiple);
}

public override int GetRequiredBufferSize()
Expand Down
21 changes: 11 additions & 10 deletions projects/RabbitMQ.Client/client/framing/BasicCancel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
//---------------------------------------------------------------------------

using System;
using System.Text;
using RabbitMQ.Client.client.framing;
using RabbitMQ.Client.Impl;

namespace RabbitMQ.Client.Framing.Impl
{
Expand All @@ -49,21 +51,20 @@ public BasicCancel(string ConsumerTag, bool Nowait)
_nowait = Nowait;
}

public BasicCancel(ReadOnlySpan<byte> span)
{
int offset = WireFormatting.ReadShortstr(span, out _consumerTag);
WireFormatting.ReadBits(span.Slice(offset), out _nowait);
}

public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicCancel;
public override string ProtocolMethodName => "basic.cancel";
public override bool HasContent => false;

public override void ReadArgumentsFrom(ref Client.Impl.MethodArgumentReader reader)
{
_consumerTag = reader.ReadShortstr();
_nowait = reader.ReadBit();
}

public override void WriteArgumentsTo(ref Client.Impl.MethodArgumentWriter writer)
public override int WriteArgumentsTo(Span<byte> span)
{
writer.WriteShortstr(_consumerTag);
writer.WriteBit(_nowait);
writer.EndBits();
int offset = WireFormatting.WriteShortstr(span, _consumerTag);
return offset + WireFormatting.WriteBits(span.Slice(offset), _nowait);
}

public override int GetRequiredBufferSize()
Expand Down
16 changes: 9 additions & 7 deletions projects/RabbitMQ.Client/client/framing/BasicCancelOk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
//---------------------------------------------------------------------------

using System;
using System.Text;
using RabbitMQ.Client.client.framing;
using RabbitMQ.Client.Impl;

namespace RabbitMQ.Client.Framing.Impl
{
Expand All @@ -47,18 +49,18 @@ public BasicCancelOk(string ConsumerTag)
_consumerTag = ConsumerTag;
}

public BasicCancelOk(ReadOnlySpan<byte> span)
{
WireFormatting.ReadShortstr(span, out _consumerTag);
}

public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicCancelOk;
public override string ProtocolMethodName => "basic.cancel-ok";
public override bool HasContent => false;

public override void ReadArgumentsFrom(ref Client.Impl.MethodArgumentReader reader)
{
_consumerTag = reader.ReadShortstr();
}

public override void WriteArgumentsTo(ref Client.Impl.MethodArgumentWriter writer)
public override int WriteArgumentsTo(Span<byte> span)
{
writer.WriteShortstr(_consumerTag);
return WireFormatting.WriteShortstr(span, _consumerTag);
}

public override int GetRequiredBufferSize()
Expand Down
Loading