diff --git a/WindivertDotnet/WindivertBufferWriter.cs b/WindivertDotnet/WindivertBufferWriter.cs index b4d2ce1..16c8c74 100644 --- a/WindivertDotnet/WindivertBufferWriter.cs +++ b/WindivertDotnet/WindivertBufferWriter.cs @@ -31,12 +31,13 @@ public WindivertBufferWriter(WinDivertPacket packet, int offset) public unsafe void WriteReverse(TValue value) where TValue : unmanaged { var valueSpan = new Span(&value, sizeof(TValue)); - Span 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); } ///