Skip to content

Commit

Permalink
Replace span slices with range operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebakken committed Mar 12, 2023
1 parent bb99fdd commit 025a12a
Show file tree
Hide file tree
Showing 23 changed files with 92 additions and 92 deletions.
8 changes: 4 additions & 4 deletions RabbitMQ.Stream.Client/CloseRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteUInt16(span.Slice(offset), 1); //ok code
offset += WireFormatting.WriteString(span.Slice(offset), reason);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
offset += WireFormatting.WriteUInt16(span[offset..], 1); //ok code
offset += WireFormatting.WriteString(span[offset..], reason);
writer.Advance(offset);
return offset;
}
Expand Down
8 changes: 4 additions & 4 deletions RabbitMQ.Stream.Client/Compression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public int Write(Span<byte> span)
var offset = 0;
foreach (var msg in messages)
{
offset += WireFormatting.WriteUInt32(span.Slice(offset), (uint)msg.Size);
offset += msg.Write(span.Slice(offset));
offset += WireFormatting.WriteUInt32(span[offset..], (uint)msg.Size);
offset += msg.Write(span[offset..]);
}

return offset;
Expand Down Expand Up @@ -89,8 +89,8 @@ public void Compress(List<Message> messages)
var offset = 0;
foreach (var msg in messages)
{
offset += WireFormatting.WriteUInt32(span.Slice(offset), (uint)msg.Size);
offset += msg.Write(span.Slice(offset));
offset += WireFormatting.WriteUInt32(span[offset..], (uint)msg.Size);
offset += msg.Write(span[offset..]);
}

using var compressedMemory = new MemoryStream();
Expand Down
8 changes: 4 additions & 4 deletions RabbitMQ.Stream.Client/ConsumerUpdateRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), _correlationId);
offset += WireFormatting.WriteUInt16(span.Slice(offset), (ushort)ResponseCode.Ok);
offset += OffsetSpecification.Write(span.Slice(offset));
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], _correlationId);
offset += WireFormatting.WriteUInt16(span[offset..], (ushort)ResponseCode.Ok);
offset += OffsetSpecification.Write(span[offset..]);
writer.Advance(offset);
return offset;
}
Expand Down
12 changes: 6 additions & 6 deletions RabbitMQ.Stream.Client/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public int Write(IBufferWriter<byte> writer)
var span = writer.GetSpan(SizeNeeded);

var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteString(span.Slice(offset), stream);
offset += WireFormatting.WriteInt32(span.Slice(offset), arguments.Count);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
offset += WireFormatting.WriteString(span[offset..], stream);
offset += WireFormatting.WriteInt32(span[offset..], arguments.Count);

foreach (var (key, value) in arguments)
{
offset += WireFormatting.WriteString(span.Slice(offset), key);
offset += WireFormatting.WriteString(span.Slice(offset), value);
offset += WireFormatting.WriteString(span[offset..], key);
offset += WireFormatting.WriteString(span[offset..], value);
}

writer.Advance(offset);
Expand Down
6 changes: 3 additions & 3 deletions RabbitMQ.Stream.Client/Credit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public int Write(IBufferWriter<byte> writer)
var span = writer.GetSpan(SizeNeeded);
var command = (ICommand)this;
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), command.Version);
offset += WireFormatting.WriteByte(span.Slice(offset), subscriptionId);
offset += WireFormatting.WriteUInt16(span.Slice(offset), credit);
offset += WireFormatting.WriteUInt16(span[offset..], command.Version);
offset += WireFormatting.WriteByte(span[offset..], subscriptionId);
offset += WireFormatting.WriteUInt16(span[offset..], credit);
writer.Advance(offset);
return offset;
}
Expand Down
10 changes: 5 additions & 5 deletions RabbitMQ.Stream.Client/DeclarePublisherRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteByte(span.Slice(offset), publisherId);
offset += WireFormatting.WriteString(span.Slice(offset), publisherRef);
offset += WireFormatting.WriteString(span.Slice(offset), stream);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
offset += WireFormatting.WriteByte(span[offset..], publisherId);
offset += WireFormatting.WriteString(span[offset..], publisherRef);
offset += WireFormatting.WriteString(span[offset..], stream);
writer.Advance(offset);
return offset;
}
Expand Down
6 changes: 3 additions & 3 deletions RabbitMQ.Stream.Client/Delete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteString(span.Slice(offset), stream);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
offset += WireFormatting.WriteString(span[offset..], stream);
writer.Advance(offset);
return offset;
}
Expand Down
6 changes: 3 additions & 3 deletions RabbitMQ.Stream.Client/DeletePublisherRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteByte(span.Slice(offset), publisherId);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
offset += WireFormatting.WriteByte(span[offset..], publisherId);
writer.Advance(offset);
return offset;
}
Expand Down
2 changes: 1 addition & 1 deletion RabbitMQ.Stream.Client/HeartBeatRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
writer.Advance(offset);
return offset;
}
Expand Down
6 changes: 3 additions & 3 deletions RabbitMQ.Stream.Client/OpenRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteString(span.Slice(offset), vhost);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
offset += WireFormatting.WriteString(span[offset..], vhost);
writer.Advance(offset);
return offset;
}
Expand Down
6 changes: 3 additions & 3 deletions RabbitMQ.Stream.Client/PartitionsQueryRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public int Write(IBufferWriter<byte> writer)
var span = writer.GetSpan(SizeNeeded);
var command = (ICommand)this;
var offset = WireFormatting.WriteUInt16(span, key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), command.Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), _correlationId);
offset += WireFormatting.WriteString(span.Slice(offset), _superStream);
offset += WireFormatting.WriteUInt16(span[offset..], command.Version);
offset += WireFormatting.WriteUInt32(span[offset..], _correlationId);
offset += WireFormatting.WriteString(span[offset..], _superStream);
writer.Advance(offset);
return offset;
}
Expand Down
10 changes: 5 additions & 5 deletions RabbitMQ.Stream.Client/PeerProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public int Write(IBufferWriter<byte> writer)
var span = writer.GetSpan(SizeNeeded);
var command = (ICommand)this;
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), command.Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteUInt16(span[offset..], command.Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
// map
offset += WireFormatting.WriteInt32(span.Slice(offset), properties.Count);
offset += WireFormatting.WriteInt32(span[offset..], properties.Count);
foreach (var (k, v) in properties)
{
offset += WireFormatting.WriteString(span.Slice(offset), k);
offset += WireFormatting.WriteString(span.Slice(offset), v);
offset += WireFormatting.WriteString(span[offset..], k);
offset += WireFormatting.WriteString(span[offset..], v);
}

writer.Advance(offset);
Expand Down
8 changes: 4 additions & 4 deletions RabbitMQ.Stream.Client/QueryOffsetRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public int Write(IBufferWriter<byte> writer)
var span = writer.GetSpan(SizeNeeded);
var command = (ICommand)this;
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), command.Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), corrId);
offset += WireFormatting.WriteString(span.Slice(offset), reference);
offset += WireFormatting.WriteString(span.Slice(offset), stream);
offset += WireFormatting.WriteUInt16(span[offset..], command.Version);
offset += WireFormatting.WriteUInt32(span[offset..], corrId);
offset += WireFormatting.WriteString(span[offset..], reference);
offset += WireFormatting.WriteString(span[offset..], stream);
writer.Advance(offset);
return offset;
}
Expand Down
8 changes: 4 additions & 4 deletions RabbitMQ.Stream.Client/QueryPublisherRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteString(span.Slice(offset), publisherRef);
offset += WireFormatting.WriteString(span.Slice(offset), stream);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
offset += WireFormatting.WriteString(span[offset..], publisherRef);
offset += WireFormatting.WriteString(span[offset..], stream);
writer.Advance(offset);
return offset;
}
Expand Down
8 changes: 4 additions & 4 deletions RabbitMQ.Stream.Client/SaslAuthenticateRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteString(span.Slice(offset), mechanism);
offset += WireFormatting.WriteBytes(span.Slice(offset), new ReadOnlySequence<byte>(data));
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
offset += WireFormatting.WriteString(span[offset..], mechanism);
offset += WireFormatting.WriteBytes(span[offset..], new ReadOnlySequence<byte>(data));
writer.Advance(offset);
return offset;
}
Expand Down
4 changes: 2 additions & 2 deletions RabbitMQ.Stream.Client/SaslHandshakeRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
writer.Advance(offset);
return offset;
}
Expand Down
8 changes: 4 additions & 4 deletions RabbitMQ.Stream.Client/StoreOffsetRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public int Write(IBufferWriter<byte> writer)
var span = writer.GetSpan(SizeNeeded);
var command = (ICommand)this;
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), command.Version);
offset += WireFormatting.WriteString(span.Slice(offset), reference);
offset += WireFormatting.WriteString(span.Slice(offset), stream);
offset += WireFormatting.WriteUInt64(span.Slice(offset), offsetValue);
offset += WireFormatting.WriteUInt16(span[offset..], command.Version);
offset += WireFormatting.WriteString(span[offset..], reference);
offset += WireFormatting.WriteString(span[offset..], stream);
offset += WireFormatting.WriteUInt64(span[offset..], offsetValue);
writer.Advance(offset);
return offset;
}
Expand Down
6 changes: 3 additions & 3 deletions RabbitMQ.Stream.Client/StreamStatsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteString(span.Slice(offset), stream);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
offset += WireFormatting.WriteString(span[offset..], stream);
writer.Advance(offset);
return offset;
}
Expand Down
18 changes: 9 additions & 9 deletions RabbitMQ.Stream.Client/SubEntryPublish.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,30 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), Version);
offset += WireFormatting.WriteByte(span.Slice(offset), publisherId);
offset += WireFormatting.WriteUInt16(span[offset..], Version);
offset += WireFormatting.WriteByte(span[offset..], publisherId);
// number of root messages. In this case will be always 1.
offset += WireFormatting.WriteInt32(span.Slice(offset), 1);
offset += WireFormatting.WriteInt32(span[offset..], 1);
// publishingId for all the messages
// so there is publishingId --> []messages
offset += WireFormatting.WriteUInt64(span.Slice(offset), publishingId);
offset += WireFormatting.WriteUInt64(span[offset..], publishingId);
// compress mode see CompressMode
var agg = (byte)compressionCodec.CompressionType << 4;
offset += WireFormatting.WriteByte(
span.Slice(offset), (byte)(0x80 | agg));
span[offset..], (byte)(0x80 | agg));

// sub Messages number
offset += WireFormatting.WriteUInt16(span.Slice(offset), (ushort)compressionCodec.MessagesCount);
offset += WireFormatting.WriteUInt16(span[offset..], (ushort)compressionCodec.MessagesCount);

// uncompressed byte size value
offset += WireFormatting.WriteUInt32(span.Slice(offset),
offset += WireFormatting.WriteUInt32(span[offset..],
(uint)compressionCodec.UnCompressedSize);

// compressed byte size value
offset += WireFormatting.WriteUInt32(span.Slice(offset),
offset += WireFormatting.WriteUInt32(span[offset..],
(uint)compressionCodec.CompressedSize);

offset += compressionCodec.Write(span.Slice(offset));
offset += compressionCodec.Write(span[offset..]);
writer.Advance(offset);
return offset;
}
Expand Down
18 changes: 9 additions & 9 deletions RabbitMQ.Stream.Client/Subscribe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,19 @@ public int Write(IBufferWriter<byte> writer)
{
var span = writer.GetSpan(SizeNeeded);
var offset = WireFormatting.WriteUInt16(span, Key);
offset += WireFormatting.WriteUInt16(span.Slice(offset), ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span.Slice(offset), correlationId);
offset += WireFormatting.WriteByte(span.Slice(offset), subscriptionId);
offset += WireFormatting.WriteString(span.Slice(offset), stream);
offset += offsetType.Write(span.Slice(offset));
offset += WireFormatting.WriteUInt16(span.Slice(offset), credit);
offset += WireFormatting.WriteUInt16(span[offset..], ((ICommand)this).Version);
offset += WireFormatting.WriteUInt32(span[offset..], correlationId);
offset += WireFormatting.WriteByte(span[offset..], subscriptionId);
offset += WireFormatting.WriteString(span[offset..], stream);
offset += offsetType.Write(span[offset..]);
offset += WireFormatting.WriteUInt16(span[offset..], credit);
if (properties.Count > 0)
{
offset += WireFormatting.WriteInt32(span.Slice(offset), properties.Count);
offset += WireFormatting.WriteInt32(span[offset..], properties.Count);
foreach (var (k, v) in properties)
{
offset += WireFormatting.WriteString(span.Slice(offset), k);
offset += WireFormatting.WriteString(span.Slice(offset), v);
offset += WireFormatting.WriteString(span[offset..], k);
offset += WireFormatting.WriteString(span[offset..], v);
}
}

Expand Down
Loading

0 comments on commit 025a12a

Please sign in to comment.