Skip to content

Commit

Permalink
Optimize the implementation of SwapBytes.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Aug 13, 2023
1 parent 3fd4d04 commit ecbfccf
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/MySqlConnector/Utilities/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Authentication;
using System.Security.Cryptography;
Expand Down Expand Up @@ -512,9 +513,8 @@ public static Task WriteAsync(this Stream stream, ReadOnlyMemory<byte> data)

public static void SwapBytes(Span<byte> bytes, int offset1, int offset2)
{
byte swap = bytes[offset1];
bytes[offset1] = bytes[offset2];
bytes[offset2] = swap;
ref var first = ref Unsafe.AsRef(bytes[0]);
(Unsafe.Add(ref first, offset2), Unsafe.Add(ref first, offset1)) = (Unsafe.Add(ref first, offset1), Unsafe.Add(ref first, offset2));
}

#if NET462
Expand Down

0 comments on commit ecbfccf

Please sign in to comment.