Use for rather than foreach on List (#1523)

List enumerator is full fat
This commit is contained in:
Ben Adams 2017-03-21 10:26:58 +00:00 committed by David Fowler
parent f546f16356
commit 24ed93288e
1 changed files with 16 additions and 11 deletions

View File

@ -125,7 +125,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Networking
{ {
_callback = null; _callback = null;
_state = null; _state = null;
Unpin(this); UnpinGcHandles();
throw; throw;
} }
} }
@ -200,31 +200,36 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Networking
{ {
_callback = null; _callback = null;
_state = null; _state = null;
Unpin(this); UnpinGcHandles();
throw; throw;
} }
} }
private static void Unpin(UvWriteReq req) // Safe handle has instance method called Unpin
// so using UnpinGcHandles to avoid conflict
private void UnpinGcHandles()
{ {
foreach (var pin in req._pins) var pinList = _pins;
var count = pinList.Count;
for (var i = 0; i < count; i++)
{ {
pin.Free(); pinList[i].Free();
} }
pinList.Clear();
foreach (var handle in req._handles) var handleList = _handles;
count = handleList.Count;
for (var i = 0; i < count; i++)
{ {
handle.Free(); handleList[i].Free();
} }
handleList.Clear();
req._pins.Clear();
req._handles.Clear();
} }
private static void UvWriteCb(IntPtr ptr, int status) private static void UvWriteCb(IntPtr ptr, int status)
{ {
var req = FromIntPtr<UvWriteReq>(ptr); var req = FromIntPtr<UvWriteReq>(ptr);
Unpin(req); req.UnpinGcHandles();
var callback = req._callback; var callback = req._callback;
req._callback = null; req._callback = null;