Skip to content

Commit b3e7a14

Browse files
Merge pull request #936 from bollhals/useSpanDirectly
Use span directly instead of reader / writer for methods & properties
2 parents 9dd3878 + 8a4b3f7 commit b3e7a14

File tree

95 files changed

+1438
-1510
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1438
-1510
lines changed

RabbitMQDotNetClient.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RabbitMQ.Client", "projects
1212
EndProject
1313
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Unit", "projects\Unit\Unit.csproj", "{B8FAC024-CC03-4067-9FFC-02846FB8AE48}"
1414
EndProject
15+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Benchmarks", "projects\Benchmarks\Benchmarks.csproj", "{38D72C9A-68E9-4653-B0CE-C7BA9FFD91D0}"
16+
EndProject
1517
Global
1618
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1719
Debug|Any CPU = Debug|Any CPU
@@ -26,6 +28,10 @@ Global
2628
{B8FAC024-CC03-4067-9FFC-02846FB8AE48}.Debug|Any CPU.Build.0 = Debug|Any CPU
2729
{B8FAC024-CC03-4067-9FFC-02846FB8AE48}.Release|Any CPU.ActiveCfg = Release|Any CPU
2830
{B8FAC024-CC03-4067-9FFC-02846FB8AE48}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{38D72C9A-68E9-4653-B0CE-C7BA9FFD91D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{38D72C9A-68E9-4653-B0CE-C7BA9FFD91D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{38D72C9A-68E9-4653-B0CE-C7BA9FFD91D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{38D72C9A-68E9-4653-B0CE-C7BA9FFD91D0}.Release|Any CPU.Build.0 = Release|Any CPU
2935
EndGlobalSection
3036
GlobalSection(SolutionProperties) = preSolution
3137
HideSolutionNode = FALSE

projects/Benchmarks/Benchmarks.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<AssemblyOriginatorKeyFile>../rabbit.snk</AssemblyOriginatorKeyFile>
6+
<SignAssembly>true</SignAssembly>
7+
<TargetFramework>netcoreapp3.1</TargetFramework>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\RabbitMQ.Client\RabbitMQ.Client.csproj" />
16+
</ItemGroup>
17+
18+
</Project>

projects/Benchmarks/Program.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using BenchmarkDotNet.Running;
2+
3+
namespace Benchmarks
4+
{
5+
public static class Program
6+
{
7+
public static void Main(string[] args)
8+
{
9+
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
10+
}
11+
}
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using RabbitMQ.Client.Framing.Impl;
4+
5+
namespace Benchmarks.WireFormatting
6+
{
7+
[ShortRunJob]
8+
[MemoryDiagnoser]
9+
public class WireFormatting_Read_BasicAck
10+
{
11+
private readonly byte[] _buffer = new byte[1024];
12+
13+
public WireFormatting_Read_BasicAck()
14+
{
15+
new BasicAck(ulong.MaxValue, true).WriteArgumentsTo(_buffer);
16+
}
17+
18+
[Benchmark(Baseline = true)]
19+
public object ReadFromSpan()
20+
{
21+
return new BasicAck(new ReadOnlySpan<byte>(_buffer));
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using BasicDeliver = RabbitMQ.Client.Framing.Impl.BasicDeliver;
4+
5+
namespace Benchmarks.WireFormatting
6+
{
7+
[ShortRunJob]
8+
[MemoryDiagnoser]
9+
public class WireFormatting_Read_BasicDeliver
10+
{
11+
private readonly byte[] _buffer = new byte[1024];
12+
13+
public WireFormatting_Read_BasicDeliver()
14+
{
15+
new BasicDeliver(string.Empty, 0, false, string.Empty, string.Empty).WriteArgumentsTo(_buffer);
16+
}
17+
18+
[Benchmark(Baseline = true)]
19+
public object ReadFromSpan()
20+
{
21+
return new BasicDeliver(new ReadOnlySpan<byte>(_buffer));
22+
}
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using BasicProperties = RabbitMQ.Client.Framing.BasicProperties;
4+
5+
namespace Benchmarks.WireFormatting
6+
{
7+
[ShortRunJob]
8+
[MemoryDiagnoser]
9+
public class WireFormatting_Read_BasicProperties
10+
{
11+
private readonly byte[] _buffer = new byte[1024];
12+
13+
public WireFormatting_Read_BasicProperties()
14+
{
15+
new BasicProperties
16+
{
17+
Persistent = true,
18+
AppId = "AppId",
19+
ContentEncoding = "content"
20+
}.WritePropertiesTo(_buffer);
21+
}
22+
23+
[Benchmark(Baseline = true)]
24+
public object ReadFromSpan()
25+
{
26+
return new BasicProperties(new ReadOnlySpan<byte>(_buffer));
27+
}
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using RabbitMQ.Client.Framing.Impl;
4+
5+
namespace Benchmarks.WireFormatting
6+
{
7+
[ShortRunJob]
8+
[MemoryDiagnoser]
9+
public class WireFormatting_Read_ChannelClose
10+
{
11+
private readonly byte[] _buffer = new byte[1024];
12+
13+
public WireFormatting_Read_ChannelClose()
14+
{
15+
new ChannelClose(333, string.Empty, 0099, 2999).WriteArgumentsTo(_buffer);
16+
}
17+
18+
[Benchmark(Baseline = true)]
19+
public object ReadFromSpan()
20+
{
21+
return new ChannelClose(new ReadOnlySpan<byte>(_buffer));
22+
}
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using BenchmarkDotNet.Attributes;
2+
using RabbitMQ.Client.Framing.Impl;
3+
4+
namespace Benchmarks.WireFormatting
5+
{
6+
[ShortRunJob]
7+
[MemoryDiagnoser]
8+
public class WireFormatting_Write_BasicAck
9+
{
10+
private readonly byte[] _buffer = new byte[1024];
11+
private readonly BasicAck _method = new BasicAck(ulong.MaxValue, true);
12+
13+
[Benchmark(Baseline = true)]
14+
public int WriteArgumentsTo()
15+
{
16+
return _method.WriteArgumentsTo(_buffer);
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BasicDeliver = RabbitMQ.Client.Framing.Impl.BasicDeliver;
3+
4+
namespace Benchmarks.WireFormatting
5+
{
6+
[ShortRunJob]
7+
[MemoryDiagnoser]
8+
public class WireFormatting_Write_BasicDeliver
9+
{
10+
private readonly byte[] _buffer = new byte[1024];
11+
private readonly BasicDeliver _method = new BasicDeliver(string.Empty, 0, false, string.Empty, string.Empty);
12+
13+
[Benchmark(Baseline = true)]
14+
public int WriteArgumentsTo()
15+
{
16+
return _method.WriteArgumentsTo(_buffer);
17+
}
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using BasicProperties = RabbitMQ.Client.Framing.BasicProperties;
4+
5+
namespace Benchmarks.WireFormatting
6+
{
7+
[ShortRunJob]
8+
[MemoryDiagnoser]
9+
public class WireFormatting_Write_BasicProperties
10+
{
11+
private readonly byte[] _buffer = new byte[1024];
12+
private readonly BasicProperties _properties = new BasicProperties
13+
{
14+
Persistent = true,
15+
AppId = "AppId",
16+
ContentEncoding = "content",
17+
};
18+
19+
[Benchmark(Baseline = true)]
20+
public void WritePropertiesToSpan()
21+
{
22+
_properties.WritePropertiesTo(new Span<byte>(_buffer));
23+
}
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using BenchmarkDotNet.Attributes;
2+
using RabbitMQ.Client.Framing.Impl;
3+
4+
namespace Benchmarks.WireFormatting
5+
{
6+
[ShortRunJob]
7+
[MemoryDiagnoser]
8+
public class WireFormatting_Write_ChannelClose
9+
{
10+
private readonly byte[] _buffer = new byte[1024];
11+
private readonly ChannelClose _method = new ChannelClose(333, string.Empty, 0099, 2999);
12+
13+
[Benchmark(Baseline = true)]
14+
public int WriteArgumentsTo()
15+
{
16+
return _method.WriteArgumentsTo(_buffer);
17+
}
18+
}
19+
}

projects/RabbitMQ.Client/RabbitMQ.Client.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
5252
<_Parameter1>Unit, PublicKey=00240000048000009400000006020000002400005253413100040000010001008d20ec856aeeb8c3153a77faa2d80e6e43b5db93224a20cc7ae384f65f142e89730e2ff0fcc5d578bbe96fa98a7196c77329efdee4579b3814c0789e5a39b51df6edd75b602a33ceabdfcf19a3feb832f31d8254168cd7ba5700dfbca301fbf8db614ba41ba18474de0a5f4c2d51c995bc3636c641c8cbe76f45717bfcb943b5</_Parameter1>
5353
</AssemblyAttribute>
54+
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
55+
<_Parameter1>Benchmarks, PublicKey=00240000048000009400000006020000002400005253413100040000010001008d20ec856aeeb8c3153a77faa2d80e6e43b5db93224a20cc7ae384f65f142e89730e2ff0fcc5d578bbe96fa98a7196c77329efdee4579b3814c0789e5a39b51df6edd75b602a33ceabdfcf19a3feb832f31d8254168cd7ba5700dfbca301fbf8db614ba41ba18474de0a5f4c2d51c995bc3636c641c8cbe76f45717bfcb943b5</_Parameter1>
56+
</AssemblyAttribute>
5457
</ItemGroup>
5558

5659
<ItemGroup>

projects/RabbitMQ.Client/client/framing/BasicAck.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
3030
//---------------------------------------------------------------------------
3131

32+
using System;
3233
using RabbitMQ.Client.client.framing;
34+
using RabbitMQ.Client.Impl;
3335

3436
namespace RabbitMQ.Client.Framing.Impl
3537
{
@@ -48,21 +50,20 @@ public BasicAck(ulong DeliveryTag, bool Multiple)
4850
_multiple = Multiple;
4951
}
5052

53+
public BasicAck(ReadOnlySpan<byte> span)
54+
{
55+
int offset = WireFormatting.ReadLonglong(span, out _deliveryTag);
56+
WireFormatting.ReadBits(span.Slice(offset), out _multiple);
57+
}
58+
5159
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicAck;
5260
public override string ProtocolMethodName => "basic.ack";
5361
public override bool HasContent => false;
5462

55-
public override void ReadArgumentsFrom(ref Client.Impl.MethodArgumentReader reader)
56-
{
57-
_deliveryTag = reader.ReadLonglong();
58-
_multiple = reader.ReadBit();
59-
}
60-
61-
public override void WriteArgumentsTo(ref Client.Impl.MethodArgumentWriter writer)
63+
public override int WriteArgumentsTo(Span<byte> span)
6264
{
63-
writer.WriteLonglong(_deliveryTag);
64-
writer.WriteBit(_multiple);
65-
writer.EndBits();
65+
int offset = WireFormatting.WriteLonglong(span, _deliveryTag);
66+
return offset + WireFormatting.WriteBits(span.Slice(offset), _multiple);
6667
}
6768

6869
public override int GetRequiredBufferSize()

projects/RabbitMQ.Client/client/framing/BasicCancel.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
3030
//---------------------------------------------------------------------------
3131

32+
using System;
3233
using System.Text;
3334
using RabbitMQ.Client.client.framing;
35+
using RabbitMQ.Client.Impl;
3436

3537
namespace RabbitMQ.Client.Framing.Impl
3638
{
@@ -49,21 +51,20 @@ public BasicCancel(string ConsumerTag, bool Nowait)
4951
_nowait = Nowait;
5052
}
5153

54+
public BasicCancel(ReadOnlySpan<byte> span)
55+
{
56+
int offset = WireFormatting.ReadShortstr(span, out _consumerTag);
57+
WireFormatting.ReadBits(span.Slice(offset), out _nowait);
58+
}
59+
5260
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicCancel;
5361
public override string ProtocolMethodName => "basic.cancel";
5462
public override bool HasContent => false;
5563

56-
public override void ReadArgumentsFrom(ref Client.Impl.MethodArgumentReader reader)
57-
{
58-
_consumerTag = reader.ReadShortstr();
59-
_nowait = reader.ReadBit();
60-
}
61-
62-
public override void WriteArgumentsTo(ref Client.Impl.MethodArgumentWriter writer)
64+
public override int WriteArgumentsTo(Span<byte> span)
6365
{
64-
writer.WriteShortstr(_consumerTag);
65-
writer.WriteBit(_nowait);
66-
writer.EndBits();
66+
int offset = WireFormatting.WriteShortstr(span, _consumerTag);
67+
return offset + WireFormatting.WriteBits(span.Slice(offset), _nowait);
6768
}
6869

6970
public override int GetRequiredBufferSize()

projects/RabbitMQ.Client/client/framing/BasicCancelOk.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
// Copyright (c) 2007-2020 VMware, Inc. All rights reserved.
3030
//---------------------------------------------------------------------------
3131

32+
using System;
3233
using System.Text;
3334
using RabbitMQ.Client.client.framing;
35+
using RabbitMQ.Client.Impl;
3436

3537
namespace RabbitMQ.Client.Framing.Impl
3638
{
@@ -47,18 +49,18 @@ public BasicCancelOk(string ConsumerTag)
4749
_consumerTag = ConsumerTag;
4850
}
4951

52+
public BasicCancelOk(ReadOnlySpan<byte> span)
53+
{
54+
WireFormatting.ReadShortstr(span, out _consumerTag);
55+
}
56+
5057
public override ProtocolCommandId ProtocolCommandId => ProtocolCommandId.BasicCancelOk;
5158
public override string ProtocolMethodName => "basic.cancel-ok";
5259
public override bool HasContent => false;
5360

54-
public override void ReadArgumentsFrom(ref Client.Impl.MethodArgumentReader reader)
55-
{
56-
_consumerTag = reader.ReadShortstr();
57-
}
58-
59-
public override void WriteArgumentsTo(ref Client.Impl.MethodArgumentWriter writer)
61+
public override int WriteArgumentsTo(Span<byte> span)
6062
{
61-
writer.WriteShortstr(_consumerTag);
63+
return WireFormatting.WriteShortstr(span, _consumerTag);
6264
}
6365

6466
public override int GetRequiredBufferSize()

0 commit comments

Comments
 (0)