3
3
// See the LICENSE file in the project root for more information.
4
4
5
5
#nullable enable
6
+ using NetworkToolkit . Http . Primitives ;
6
7
using System ;
7
8
using System . Buffers ;
8
9
using System . Diagnostics ;
@@ -27,10 +28,13 @@ namespace NetworkToolkit
27
28
[ StructLayout ( LayoutKind . Auto ) ]
28
29
internal struct VectorArrayBuffer : IDisposable
29
30
{
30
- // Pad the front and back of the array with enough bytes to ensure we
31
+ // It is assumed that (due to how virtual memory works) we do
32
+ // not need to add padding to the front of the buffer.
33
+ private const int PrefixPaddingLength = 0 ;
34
+ // Pad the back of the array with enough bytes to ensure we
31
35
// can always safely read an Vector256 at any index within returned spans.
32
- private static int PerSidePaddingLength => Vector256 < byte > . Count - 1 ;
33
- private static int TotalPaddingLength => PerSidePaddingLength * 2 ;
36
+ private static int SuffixPaddingLength => Http1Connection . HeaderBufferPadding ;
37
+ private static int TotalPaddingLength => PrefixPaddingLength + SuffixPaddingLength ;
34
38
35
39
private byte [ ] _bytes ;
36
40
private int _activeStart ;
@@ -61,12 +65,12 @@ public void Dispose()
61
65
}
62
66
63
67
public int ActiveLength => _availableStart - _activeStart ;
64
- public Span < byte > ActiveSpan => new Span < byte > ( _bytes , _activeStart + PerSidePaddingLength , ActiveLength ) ;
65
- public Memory < byte > ActiveMemory => new Memory < byte > ( _bytes , _activeStart + PerSidePaddingLength , ActiveLength ) ;
68
+ public Span < byte > ActiveSpan => new Span < byte > ( _bytes , _activeStart + PrefixPaddingLength , ActiveLength ) ;
69
+ public Memory < byte > ActiveMemory => new Memory < byte > ( _bytes , _activeStart + PrefixPaddingLength , ActiveLength ) ;
66
70
67
71
public int AvailableLength => Capacity - _availableStart ;
68
- public Span < byte > AvailableSpan => new Span < byte > ( _bytes , _availableStart + PerSidePaddingLength , AvailableLength ) ;
69
- public Memory < byte > AvailableMemory => new Memory < byte > ( _bytes , _availableStart + PerSidePaddingLength , AvailableLength ) ;
72
+ public Span < byte > AvailableSpan => new Span < byte > ( _bytes , _availableStart + PrefixPaddingLength , AvailableLength ) ;
73
+ public Memory < byte > AvailableMemory => new Memory < byte > ( _bytes , _availableStart + PrefixPaddingLength , AvailableLength ) ;
70
74
71
75
public int Capacity => _bytes . Length - TotalPaddingLength ;
72
76
@@ -76,8 +80,14 @@ private static byte[] Rent(int byteCount)
76
80
array . AsSpan ( ) . Clear ( ) ;
77
81
78
82
// zero out the prefix and suffix padding, so they won't match any compares later on.
79
- array . AsSpan ( 0 , PerSidePaddingLength ) . Clear ( ) ;
80
- array . AsSpan ( array . Length - PerSidePaddingLength , PerSidePaddingLength ) . Clear ( ) ;
83
+ if ( PrefixPaddingLength != 0 )
84
+ {
85
+ #pragma warning disable CS0162 // Unreachable code detected
86
+ array . AsSpan ( 0 , PrefixPaddingLength ) . Clear ( ) ;
87
+ #pragma warning restore CS0162 // Unreachable code detected
88
+ }
89
+
90
+ array . AsSpan ( array . Length - SuffixPaddingLength , SuffixPaddingLength ) . Clear ( ) ;
81
91
82
92
return array ;
83
93
}
@@ -115,7 +125,7 @@ private void EnsureAvailableSpaceSlow(int byteCount)
115
125
if ( byteCount <= totalFree )
116
126
{
117
127
// We can free up enough space by just shifting the bytes down, so do so.
118
- Buffer . BlockCopy ( _bytes , _activeStart + PerSidePaddingLength , _bytes , PerSidePaddingLength , ActiveLength ) ;
128
+ Buffer . BlockCopy ( _bytes , _activeStart + PrefixPaddingLength , _bytes , PrefixPaddingLength , ActiveLength ) ;
119
129
_availableStart = ActiveLength ;
120
130
_activeStart = 0 ;
121
131
Debug . Assert ( byteCount <= AvailableLength ) ;
@@ -136,7 +146,7 @@ private void EnsureAvailableSpaceSlow(int byteCount)
136
146
137
147
if ( ActiveLength != 0 )
138
148
{
139
- Buffer . BlockCopy ( oldBytes , _activeStart + PerSidePaddingLength , newBytes , PerSidePaddingLength , ActiveLength ) ;
149
+ Buffer . BlockCopy ( oldBytes , _activeStart + PrefixPaddingLength , newBytes , PrefixPaddingLength , ActiveLength ) ;
140
150
}
141
151
142
152
_availableStart = ActiveLength ;
0 commit comments