@@ -13,6 +13,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
13
13
{
14
14
internal sealed class SocketSender : SocketAwaitableEventArgs
15
15
{
16
+ private List < ArraySegment < byte > > ? _bufferList ;
17
+
16
18
public SocketSender ( PipeScheduler scheduler ) : base ( scheduler )
17
19
{
18
20
}
@@ -24,11 +26,6 @@ public SocketAwaitableEventArgs SendAsync(Socket socket, in ReadOnlySequence<byt
24
26
return SendAsync ( socket , buffers . First ) ;
25
27
}
26
28
27
- if ( ! MemoryBuffer . Equals ( Memory < byte > . Empty ) )
28
- {
29
- SetBuffer ( null , 0 , 0 ) ;
30
- }
31
-
32
29
SetBufferList ( buffers ) ;
33
30
34
31
if ( ! socket . SendAsync ( this ) )
@@ -41,19 +38,18 @@ public SocketAwaitableEventArgs SendAsync(Socket socket, in ReadOnlySequence<byt
41
38
42
39
public void Reset ( )
43
40
{
44
- // TODO: Consider clearing the buffer and buffer list before we put it back into the pool
45
- // it's a performance hit but it removes the confusion when looking at dumps to see this still
41
+ // We clear the buffer and buffer list before we put it back into the pool
42
+ // it's a small performance hit but it removes the confusion when looking at dumps to see this still
46
43
// holder onto the buffer when it's back in the pool
44
+ BufferList = null ;
45
+
46
+ SetBuffer ( null , 0 , 0 ) ;
47
+
48
+ _bufferList ? . Clear ( ) ;
47
49
}
48
50
49
51
private SocketAwaitableEventArgs SendAsync ( Socket socket , ReadOnlyMemory < byte > memory )
50
52
{
51
- // The BufferList getter is much less expensive then the setter.
52
- if ( BufferList != null )
53
- {
54
- BufferList = null ;
55
- }
56
-
57
53
SetBuffer ( MemoryMarshal . AsMemory ( memory ) ) ;
58
54
59
55
if ( ! socket . SendAsync ( this ) )
@@ -69,25 +65,23 @@ private void SetBufferList(in ReadOnlySequence<byte> buffer)
69
65
Debug . Assert ( ! buffer . IsEmpty ) ;
70
66
Debug . Assert ( ! buffer . IsSingleSegment ) ;
71
67
72
- var buffers = BufferList ;
73
-
74
- if ( buffers == null )
68
+ if ( _bufferList == null )
75
69
{
76
- buffers = new List < ArraySegment < byte > > ( ) ;
70
+ _bufferList = new List < ArraySegment < byte > > ( ) ;
77
71
}
78
72
else
79
73
{
80
74
// Buffers are pooled, so it's OK to root them until the next multi-buffer write.
81
- buffers . Clear ( ) ;
75
+ _bufferList . Clear ( ) ;
82
76
}
83
77
84
78
foreach ( var b in buffer )
85
79
{
86
- buffers . Add ( b . GetArray ( ) ) ;
80
+ _bufferList . Add ( b . GetArray ( ) ) ;
87
81
}
88
82
89
83
// The act of setting this list, sets the buffers in the internal buffer list
90
- BufferList = buffers ;
84
+ BufferList = _bufferList ;
91
85
}
92
86
}
93
87
}
0 commit comments