From 24ed93288e083c935bb64d676c22dfbfc9c8ffa8 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Tue, 21 Mar 2017 10:26:58 +0000 Subject: [PATCH] Use for rather than foreach on List (#1523) List enumerator is full fat --- .../Internal/Networking/UvWriteReq.cs | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Networking/UvWriteReq.cs b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Networking/UvWriteReq.cs index 1134e71eaa..c454859e3c 100644 --- a/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Networking/UvWriteReq.cs +++ b/src/Microsoft.AspNetCore.Server.Kestrel/Internal/Networking/UvWriteReq.cs @@ -125,7 +125,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Networking { _callback = null; _state = null; - Unpin(this); + UnpinGcHandles(); throw; } } @@ -200,31 +200,36 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Networking { _callback = null; _state = null; - Unpin(this); + UnpinGcHandles(); 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(); } - - req._pins.Clear(); - req._handles.Clear(); + handleList.Clear(); } private static void UvWriteCb(IntPtr ptr, int status) { var req = FromIntPtr(ptr); - Unpin(req); + req.UnpinGcHandles(); var callback = req._callback; req._callback = null;