Skip to content

Commit

Permalink
WriteReverse优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Oct 19, 2022
1 parent b99ea6a commit b445578
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions WindivertDotnet/WindivertBufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public WindivertBufferWriter(WinDivertPacket packet, int offset)
public unsafe void WriteReverse<TValue>(TValue value) where TValue : unmanaged
{
var valueSpan = new Span<byte>(&value, sizeof(TValue));
Span<byte> reverseSpan = stackalloc byte[valueSpan.Length];
var span = this.GetSpan(valueSpan.Length);

valueSpan.CopyTo(reverseSpan);
reverseSpan.Reverse();
// 先写入后翻转,减少必要的分配
valueSpan.CopyTo(span);
span[..valueSpan.Length].Reverse();

this.Write(reverseSpan);
this.Advance(valueSpan.Length);
}

/// <summary>
Expand Down

0 comments on commit b445578

Please sign in to comment.