Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit e331b86

Browse files
committed
Initalize SocketOutput Queues, Reuse WriteContexts
1 parent dca9d60 commit e331b86

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ public class SocketOutput : ISocketOutput
3232
private Exception _lastWriteError;
3333
private WriteContext _nextWriteContext;
3434
private readonly Queue<TaskCompletionSource<object>> _tasksPending;
35+
private readonly Queue<WriteContext> _writeContexts;
3536

3637
public SocketOutput(KestrelThread thread, UvStreamHandle socket, long connectionId, IKestrelTrace log)
3738
{
3839
_thread = thread;
3940
_socket = socket;
4041
_connectionId = connectionId;
4142
_log = log;
42-
_tasksPending = new Queue<TaskCompletionSource<object>>();
43+
_tasksPending = new Queue<TaskCompletionSource<object>>(16);
44+
_writeContexts = new Queue<WriteContext>(16);
4345
}
4446

4547
public Task WriteAsync(
@@ -63,7 +65,14 @@ public Task WriteAsync(
6365
{
6466
if (_nextWriteContext == null)
6567
{
66-
_nextWriteContext = new WriteContext(this);
68+
if (_writeContexts.Count > 0)
69+
{
70+
_nextWriteContext = _writeContexts.Dequeue();
71+
}
72+
else
73+
{
74+
_nextWriteContext = new WriteContext(this);
75+
}
6776
}
6877

6978
if (buffer.Array != null)
@@ -172,13 +181,13 @@ private void WriteAllPending()
172181
}
173182

174183
// This is called on the libuv event loop
175-
private void OnWriteCompleted(Queue<ArraySegment<byte>> writtenBuffers, int status, Exception error)
184+
private void OnWriteCompleted(WriteContext write)
176185
{
177-
_log.ConnectionWriteCallback(_connectionId, status);
186+
var status = write.WriteStatus;
178187

179188
lock (_lockObj)
180189
{
181-
_lastWriteError = error;
190+
_lastWriteError = write.WriteError;
182191

183192
if (_nextWriteContext != null)
184193
{
@@ -189,7 +198,7 @@ private void OnWriteCompleted(Queue<ArraySegment<byte>> writtenBuffers, int stat
189198
_writesPending--;
190199
}
191200

192-
foreach (var writeBuffer in writtenBuffers)
201+
foreach (var writeBuffer in write.Buffers)
193202
{
194203
// _numBytesPreCompleted can temporarily go negative in the event there are
195204
// completed writes that we haven't triggered callbacks for yet.
@@ -208,24 +217,30 @@ private void OnWriteCompleted(Queue<ArraySegment<byte>> writtenBuffers, int stat
208217
_numBytesPreCompleted += bytesToWrite;
209218
bytesLeftToBuffer -= bytesToWrite;
210219

211-
if (error == null)
220+
if (write.WriteError == null)
212221
{
213222
ThreadPool.QueueUserWorkItem(
214223
(o) => ((TaskCompletionSource<object>)o).SetResult(null),
215224
tcs);
216225
}
217226
else
218227
{
228+
var error = write.WriteError;
219229
// error is closure captured
220230
ThreadPool.QueueUserWorkItem(
221231
(o) => ((TaskCompletionSource<object>)o).SetException(error),
222232
tcs);
223233
}
224234
}
225235

236+
write.Reset();
237+
_writeContexts.Enqueue(write);
238+
226239
// Now that the while loop has completed the following invariants should hold true:
227240
Debug.Assert(_numBytesPreCompleted >= 0);
228241
}
242+
243+
_log.ConnectionWriteCallback(_connectionId, status);
229244
}
230245

231246
void ISocketOutput.Write(ArraySegment<byte> buffer, bool immediate)
@@ -263,7 +278,7 @@ private class WriteContext
263278
public WriteContext(SocketOutput self)
264279
{
265280
Self = self;
266-
Buffers = new Queue<ArraySegment<byte>>();
281+
Buffers = new Queue<ArraySegment<byte>>(8);
267282
}
268283

269284
/// <summary>
@@ -340,7 +355,17 @@ public void DoDisconnectIfNeeded()
340355

341356
public void Complete()
342357
{
343-
Self.OnWriteCompleted(Buffers, WriteStatus, WriteError);
358+
Self.OnWriteCompleted(this);
359+
}
360+
361+
public void Reset()
362+
{
363+
Buffers.Clear();
364+
SocketDisconnect = false;
365+
SocketShutdownSend = false;
366+
WriteStatus = 0;
367+
WriteError = null;
368+
ShutdownSendStatus = 0;
344369
}
345370
}
346371
}

0 commit comments

Comments
 (0)