Skip to content

Commit 22c7fbf

Browse files
committed
Simplify some ArraySegment and Span code.
1 parent 0bb5885 commit 22c7fbf

File tree

11 files changed

+21
-17
lines changed

11 files changed

+21
-17
lines changed

src/MySqlConnector/Core/BatchedCommandPayloadCreator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public bool WriteQueryCommand(ref CommandListPosition commandListPosition, IDict
2525

2626
// write command length
2727
var commandLength = writer.Position - position - padding.Length;
28-
var span = writer.ArraySegment.AsSpan().Slice(position);
28+
var span = writer.ArraySegment.AsSpan(position);
2929
span[0] = 0xFE;
3030
BinaryPrimitives.WriteUInt64LittleEndian(span[1..], (ulong) commandLength);
3131
} while (wroteCommand);

src/MySqlConnector/Core/ResultSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static int ReadColumnCount(ReadOnlySpan<byte> span)
140140
// 'Session.ReceiveReplyAsync' reuses a shared buffer; make a copy so that the column definitions can always be safely read at any future point
141141
if (m_columnDefinitionPayloadUsedBytes + payloadLength > m_columnDefinitionPayloads.Count)
142142
Utility.Resize(ref m_columnDefinitionPayloads, m_columnDefinitionPayloadUsedBytes + payloadLength);
143-
payload.Span.CopyTo(m_columnDefinitionPayloads.Array.AsSpan().Slice(m_columnDefinitionPayloadUsedBytes));
143+
payload.Span.CopyTo(m_columnDefinitionPayloads.AsSpan(m_columnDefinitionPayloadUsedBytes));
144144

145145
var columnDefinition = ColumnDefinitionPayload.Create(new ResizableArraySegment<byte>(m_columnDefinitionPayloads, m_columnDefinitionPayloadUsedBytes, payloadLength));
146146
ColumnDefinitions[column] = columnDefinition;

src/MySqlConnector/Core/Row.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public long GetBytes(int ordinal, long dataOffset, byte[]? buffer, int bufferOff
135135
var offset = (int) dataOffset;
136136
var lengthToCopy = Math.Max(0, Math.Min(m_dataLengths[ordinal] - offset, length));
137137
if (lengthToCopy > 0)
138-
m_data.Slice(m_dataOffsets[ordinal] + offset, lengthToCopy).Span.CopyTo(buffer.AsSpan()[bufferOffset..]);
138+
m_data.Slice(m_dataOffsets[ordinal] + offset, lengthToCopy).Span.CopyTo(buffer.AsSpan(bufferOffset));
139139
return lengthToCopy;
140140
}
141141

src/MySqlConnector/Core/ServerSession.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public async Task PrepareAsync(IMySqlCommand command, IOBehavior ioBehavior, Can
238238
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
239239
var payloadLength = payload.Span.Length;
240240
Utility.Resize(ref columnsAndParameters, columnsAndParametersSize + payloadLength);
241-
payload.Span.CopyTo(columnsAndParameters.Array.AsSpan()[columnsAndParametersSize..]);
241+
payload.Span.CopyTo(columnsAndParameters.AsSpan(columnsAndParametersSize));
242242
parameters[i] = ColumnDefinitionPayload.Create(new(columnsAndParameters, columnsAndParametersSize, payloadLength));
243243
columnsAndParametersSize += payloadLength;
244244
}
@@ -258,7 +258,7 @@ public async Task PrepareAsync(IMySqlCommand command, IOBehavior ioBehavior, Can
258258
payload = await ReceiveReplyAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
259259
var payloadLength = payload.Span.Length;
260260
Utility.Resize(ref columnsAndParameters, columnsAndParametersSize + payloadLength);
261-
payload.Span.CopyTo(columnsAndParameters.Array.AsSpan()[columnsAndParametersSize..]);
261+
payload.Span.CopyTo(columnsAndParameters.AsSpan(columnsAndParametersSize));
262262
columns[i] = ColumnDefinitionPayload.Create(new(columnsAndParameters, columnsAndParametersSize, payloadLength));
263263
columnsAndParametersSize += payloadLength;
264264
}

src/MySqlConnector/Core/StatementPreparer.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ public ParsedStatements SplitStatements()
2323
var parser = new PreparedCommandSqlParser(this, statements, statementStartEndIndexes, writer);
2424
parser.Parse(m_commandText);
2525
for (var i = 0; i < statements.Count; i++)
26+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
27+
statements[i].StatementBytes = writer.ArraySegment[statementStartEndIndexes[i * 2]..statementStartEndIndexes[i * 2 + 1]];
28+
#else
2629
statements[i].StatementBytes = writer.ArraySegment.Slice(statementStartEndIndexes[i * 2], statementStartEndIndexes[i * 2 + 1] - statementStartEndIndexes[i * 2]);
30+
#endif
2731
return new ParsedStatements(statements, writer.ToPayloadData());
2832
}
2933

src/MySqlConnector/Protocol/Serialization/ByteBufferWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void Advance(int count)
4646
public void TrimEnd(int byteCount)
4747
{
4848
Debug.Assert(byteCount <= m_output.Length, "byteCount <= m_output.Length");
49-
m_output = m_buffer.AsMemory().Slice(Position - byteCount);
49+
m_output = m_buffer.AsMemory(Position - byteCount);
5050
}
5151

5252
public void Write(byte value)

src/MySqlConnector/Protocol/Serialization/CompressedPayloadHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ private ValueTask<int> ReadBytesAsync(Memory<byte> buffer, ProtocolErrorBehavior
8282
ValueTaskExtensions.FromException<int>(new EndOfStreamException("Wanted to read 7 bytes but only read {0} when reading compressed packet header".FormatInvariant(headerReadBytes.Count)));
8383
}
8484

85-
var payloadLength = (int) SerializationUtility.ReadUInt32(headerReadBytes.Array!, headerReadBytes.Offset, 3);
85+
var payloadLength = (int) SerializationUtility.ReadUInt32(headerReadBytes.AsSpan(0, 3));
8686
var packetSequenceNumber = headerReadBytes.Array![headerReadBytes.Offset + 3];
87-
var uncompressedLength = (int) SerializationUtility.ReadUInt32(headerReadBytes.Array, headerReadBytes.Offset + 4, 3);
87+
var uncompressedLength = (int) SerializationUtility.ReadUInt32(headerReadBytes.AsSpan(4, 3));
8888

8989
// verify the compressed packet sequence number
9090
var expectedSequenceNumber = GetNextCompressedSequenceNumber();

src/MySqlConnector/Protocol/Serialization/ProtocolUtility.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ private static ValueTask<Packet> ReadPacketAfterHeader(ReadOnlySpan<byte> header
417417
ValueTaskExtensions.FromException<Packet>(new EndOfStreamException("Expected to read 4 header bytes but only received {0}.".FormatInvariant(headerBytes.Length)));
418418
}
419419

420-
var payloadLength = (int) SerializationUtility.ReadUInt32(headerBytes.Slice(0, 3));
420+
var payloadLength = (int) SerializationUtility.ReadUInt32(headerBytes[..3]);
421421
int packetSequenceNumber = headerBytes[3];
422422

423423
Exception? packetOutOfOrderException = null;
@@ -441,7 +441,11 @@ private static ValueTask<Packet> CreatePacketFromPayload(ArraySegment<byte> payl
441441
if (protocolErrorBehavior == ProtocolErrorBehavior.Ignore)
442442
return default;
443443

444+
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
445+
if (payloadBytes is [ ErrorPayload.Signature, .. ])
446+
#else
444447
if (payloadBytes.Count > 0 && payloadBytes.AsSpan()[0] == ErrorPayload.Signature)
448+
#endif
445449
return new ValueTask<Packet>(new Packet(payloadBytes));
446450

447451
return ValueTaskExtensions.FromException<Packet>(exception);

src/MySqlConnector/Protocol/Serialization/SerializationUtility.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@ namespace MySqlConnector.Protocol.Serialization;
22

33
internal static class SerializationUtility
44
{
5-
public static uint ReadUInt32(byte[] buffer, int offset, int count)
6-
{
7-
uint value = 0;
8-
for (int i = 0; i < count; i++)
9-
value |= ((uint) buffer[offset + i]) << (8 * i);
10-
return value;
11-
}
12-
135
public static uint ReadUInt32(ReadOnlySpan<byte> span)
146
{
157
uint value = 0;

src/MySqlConnector/Utilities/ResizableArray.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ internal sealed class ResizableArray<T>
99
public T[]? Array => m_array;
1010
public int Count => m_array?.Length ?? 0;
1111

12+
public Span<T> AsSpan(int start) => m_array.AsSpan(start);
13+
1214
/// <summary>
1315
/// Do not call this method directly; use <see cref="Utility.Resize{T}"/>.
1416
/// </summary>

0 commit comments

Comments
 (0)