Skip to content

Commit f013917

Browse files
committed
convert BasicProperties as well to use span directly
1 parent 4c9e59f commit f013917

13 files changed

+284
-429
lines changed
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: 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+
}

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

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

32+
using System;
3233
using System.Collections.Generic;
3334
using System.Text;
3435
using RabbitMQ.Client.Impl;
@@ -197,73 +198,63 @@ public BasicProperties()
197198
{
198199
}
199200

200-
public override ushort ProtocolClassId => 60;
201-
public override string ProtocolClassName => "basic";
202-
203-
internal override void ReadPropertiesFrom(ref ContentHeaderPropertyReader reader)
201+
public BasicProperties(ReadOnlySpan<byte> span)
204202
{
205-
bool contentType_present = reader.ReadPresence();
206-
bool contentEncoding_present = reader.ReadPresence();
207-
bool headers_present = reader.ReadPresence();
208-
bool deliveryMode_present = reader.ReadPresence();
209-
bool priority_present = reader.ReadPresence();
210-
bool correlationId_present = reader.ReadPresence();
211-
bool replyTo_present = reader.ReadPresence();
212-
bool expiration_present = reader.ReadPresence();
213-
bool messageId_present = reader.ReadPresence();
214-
bool timestamp_present = reader.ReadPresence();
215-
bool type_present = reader.ReadPresence();
216-
bool userId_present = reader.ReadPresence();
217-
bool appId_present = reader.ReadPresence();
218-
bool clusterId_present = reader.ReadPresence();
219-
reader.FinishPresence();
220-
if (contentType_present) { _contentType = reader.ReadShortstr(); }
221-
if (contentEncoding_present) { _contentEncoding = reader.ReadShortstr(); }
222-
if (headers_present) { _headers = reader.ReadTable(); }
223-
if (deliveryMode_present) { _deliveryMode = reader.ReadOctet(); }
224-
if (priority_present) { _priority = reader.ReadOctet(); }
225-
if (correlationId_present) { _correlationId = reader.ReadShortstr(); }
226-
if (replyTo_present) { _replyTo = reader.ReadShortstr(); }
227-
if (expiration_present) { _expiration = reader.ReadShortstr(); }
228-
if (messageId_present) { _messageId = reader.ReadShortstr(); }
229-
if (timestamp_present) { _timestamp = reader.ReadTimestamp(); }
230-
if (type_present) { _type = reader.ReadShortstr(); }
231-
if (userId_present) { _userId = reader.ReadShortstr(); }
232-
if (appId_present) { _appId = reader.ReadShortstr(); }
233-
if (clusterId_present) { _clusterId = reader.ReadShortstr(); }
203+
int offset = WireFormatting.ReadBits(span,
204+
out bool contentType_present,
205+
out bool contentEncoding_present,
206+
out bool headers_present,
207+
out bool deliveryMode_present,
208+
out bool priority_present,
209+
out bool correlationId_present,
210+
out bool replyTo_present,
211+
out bool expiration_present,
212+
out bool messageId_present,
213+
out bool timestamp_present,
214+
out bool type_present,
215+
out bool userId_present,
216+
out bool appId_present,
217+
out bool clusterId_present);
218+
if (contentType_present) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _contentType); }
219+
if (contentEncoding_present) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _contentEncoding); }
220+
if (headers_present) { offset += WireFormatting.ReadDictionary(span.Slice(offset), out var tmpDirectory); _headers = tmpDirectory; }
221+
if (deliveryMode_present) { _deliveryMode = span[offset++]; }
222+
if (priority_present) { _priority = span[offset++]; }
223+
if (correlationId_present) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _correlationId); }
224+
if (replyTo_present) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _replyTo); }
225+
if (expiration_present) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _expiration); }
226+
if (messageId_present) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _messageId); }
227+
if (timestamp_present) { offset += WireFormatting.ReadTimestamp(span.Slice(offset), out _timestamp); }
228+
if (type_present) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _type); }
229+
if (userId_present) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _userId); }
230+
if (appId_present) { offset += WireFormatting.ReadShortstr(span.Slice(offset), out _appId); }
231+
if (clusterId_present) { WireFormatting.ReadShortstr(span.Slice(offset), out _clusterId); }
234232
}
235233

236-
internal override void WritePropertiesTo(ref ContentHeaderPropertyWriter writer)
234+
public override ushort ProtocolClassId => 60;
235+
public override string ProtocolClassName => "basic";
236+
237+
internal override int WritePropertiesTo(Span<byte> span)
237238
{
238-
writer.WritePresence(IsContentTypePresent());
239-
writer.WritePresence(IsContentEncodingPresent());
240-
writer.WritePresence(IsHeadersPresent());
241-
writer.WritePresence(IsDeliveryModePresent());
242-
writer.WritePresence(IsPriorityPresent());
243-
writer.WritePresence(IsCorrelationIdPresent());
244-
writer.WritePresence(IsReplyToPresent());
245-
writer.WritePresence(IsExpirationPresent());
246-
writer.WritePresence(IsMessageIdPresent());
247-
writer.WritePresence(IsTimestampPresent());
248-
writer.WritePresence(IsTypePresent());
249-
writer.WritePresence(IsUserIdPresent());
250-
writer.WritePresence(IsAppIdPresent());
251-
writer.WritePresence(IsClusterIdPresent());
252-
writer.FinishPresence();
253-
if (IsContentTypePresent()) { writer.WriteShortstr(_contentType); }
254-
if (IsContentEncodingPresent()) { writer.WriteShortstr(_contentEncoding); }
255-
if (IsHeadersPresent()) { writer.WriteTable(_headers); }
256-
if (IsDeliveryModePresent()) { writer.WriteOctet(_deliveryMode); }
257-
if (IsPriorityPresent()) { writer.WriteOctet(_priority); }
258-
if (IsCorrelationIdPresent()) { writer.WriteShortstr(_correlationId); }
259-
if (IsReplyToPresent()) { writer.WriteShortstr(_replyTo); }
260-
if (IsExpirationPresent()) { writer.WriteShortstr(_expiration); }
261-
if (IsMessageIdPresent()) { writer.WriteShortstr(_messageId); }
262-
if (IsTimestampPresent()) { writer.WriteTimestamp(_timestamp); }
263-
if (IsTypePresent()) { writer.WriteShortstr(_type); }
264-
if (IsUserIdPresent()) { writer.WriteShortstr(_userId); }
265-
if (IsAppIdPresent()) { writer.WriteShortstr(_appId); }
266-
if (IsClusterIdPresent()) { writer.WriteShortstr(_clusterId); }
239+
int offset = WireFormatting.WriteBits(span,
240+
IsContentTypePresent(), IsContentEncodingPresent(), IsHeadersPresent(), IsDeliveryModePresent(), IsPriorityPresent(),
241+
IsCorrelationIdPresent(), IsReplyToPresent(), IsExpirationPresent(), IsMessageIdPresent(), IsTimestampPresent(),
242+
IsTypePresent(), IsUserIdPresent(), IsAppIdPresent(), IsClusterIdPresent());
243+
if (IsContentTypePresent()) { offset += WireFormatting.WriteShortstr(span.Slice(offset), _contentType); }
244+
if (IsContentEncodingPresent()) { offset += WireFormatting.WriteShortstr(span.Slice(offset), _contentEncoding); }
245+
if (IsHeadersPresent()) { offset += WireFormatting.WriteTable(span.Slice(offset), _headers); }
246+
if (IsDeliveryModePresent()) { span[offset++] = _deliveryMode; }
247+
if (IsPriorityPresent()) { span[offset++] = _priority; }
248+
if (IsCorrelationIdPresent()) { offset += WireFormatting.WriteShortstr(span.Slice(offset), _correlationId); }
249+
if (IsReplyToPresent()) { offset += WireFormatting.WriteShortstr(span.Slice(offset), _replyTo); }
250+
if (IsExpirationPresent()) { offset += WireFormatting.WriteShortstr(span.Slice(offset), _expiration); }
251+
if (IsMessageIdPresent()) { offset += WireFormatting.WriteShortstr(span.Slice(offset), _messageId); }
252+
if (IsTimestampPresent()) { offset += WireFormatting.WriteTimestamp(span.Slice(offset), _timestamp); }
253+
if (IsTypePresent()) { offset += WireFormatting.WriteShortstr(span.Slice(offset), _type); }
254+
if (IsUserIdPresent()) { offset += WireFormatting.WriteShortstr(span.Slice(offset), _userId); }
255+
if (IsAppIdPresent()) { offset += WireFormatting.WriteShortstr(span.Slice(offset), _appId); }
256+
if (IsClusterIdPresent()) { offset += WireFormatting.WriteShortstr(span.Slice(offset), _clusterId); }
257+
return offset;
267258
}
268259

269260
public override int GetRequiredPayloadBufferSize()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,11 @@ private static Client.Impl.MethodBase DecodeMethodFrom(ProtocolCommandId command
131131
}
132132
}
133133

134-
internal override Client.Impl.ContentHeaderBase DecodeContentHeaderFrom(ushort classId)
134+
internal override Client.Impl.ContentHeaderBase DecodeContentHeaderFrom(ushort classId, ReadOnlySpan<byte> span)
135135
{
136136
switch (classId)
137137
{
138-
case 60: return new BasicProperties();
138+
case 60: return new BasicProperties(span);
139139
default: throw new Exceptions.UnknownClassOrMethodException(classId, 0);
140140
}
141141
}

projects/RabbitMQ.Client/client/impl/CommandAssembler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,7 @@ private void ParseHeaderFrame(in InboundFrame frame)
114114
}
115115

116116
ReadOnlySpan<byte> span = frame.Payload.Span;
117-
_header = _protocol.DecodeContentHeaderFrom(NetworkOrderDeserializer.ReadUInt16(span));
118-
_header.ReadFrom(span.Slice(12));
117+
_header = _protocol.DecodeContentHeaderFrom(NetworkOrderDeserializer.ReadUInt16(span), span.Slice(12));
119118
ulong totalBodyBytes = NetworkOrderDeserializer.ReadUInt64(span.Slice(4));
120119
if (totalBodyBytes > MaxArrayOfBytesSize)
121120
{

projects/RabbitMQ.Client/client/impl/ContentHeaderBase.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,7 @@ public virtual object Clone()
5050
throw new NotImplementedException();
5151
}
5252

53-
///<summary>
54-
/// Fill this instance from the given byte buffer stream.
55-
///</summary>
56-
internal void ReadFrom(ReadOnlySpan<byte> span)
57-
{
58-
ContentHeaderPropertyReader reader = new ContentHeaderPropertyReader(span);
59-
ReadPropertiesFrom(ref reader);
60-
}
61-
62-
internal abstract void ReadPropertiesFrom(ref ContentHeaderPropertyReader reader);
63-
internal abstract void WritePropertiesTo(ref ContentHeaderPropertyWriter writer);
53+
internal abstract int WritePropertiesTo(Span<byte> span);
6454

6555
public abstract int GetRequiredPayloadBufferSize();
6656
}

projects/RabbitMQ.Client/client/impl/ContentHeaderPropertyReader.cs

Lines changed: 0 additions & 152 deletions
This file was deleted.

0 commit comments

Comments
 (0)