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

Commit 24ed932

Browse files
benaadamsdavidfowl
authored andcommitted
Use for rather than foreach on List (#1523)
List enumerator is full fat
1 parent f546f16 commit 24ed932

File tree

1 file changed

+16
-11
lines changed
  • src/Microsoft.AspNetCore.Server.Kestrel/Internal/Networking

1 file changed

+16
-11
lines changed

src/Microsoft.AspNetCore.Server.Kestrel/Internal/Networking/UvWriteReq.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private unsafe void Write(
125125
{
126126
_callback = null;
127127
_state = null;
128-
Unpin(this);
128+
UnpinGcHandles();
129129
throw;
130130
}
131131
}
@@ -200,31 +200,36 @@ private unsafe void WriteArraySegmentInternal(
200200
{
201201
_callback = null;
202202
_state = null;
203-
Unpin(this);
203+
UnpinGcHandles();
204204
throw;
205205
}
206206
}
207207

208-
private static void Unpin(UvWriteReq req)
208+
// Safe handle has instance method called Unpin
209+
// so using UnpinGcHandles to avoid conflict
210+
private void UnpinGcHandles()
209211
{
210-
foreach (var pin in req._pins)
212+
var pinList = _pins;
213+
var count = pinList.Count;
214+
for (var i = 0; i < count; i++)
211215
{
212-
pin.Free();
216+
pinList[i].Free();
213217
}
218+
pinList.Clear();
214219

215-
foreach (var handle in req._handles)
220+
var handleList = _handles;
221+
count = handleList.Count;
222+
for (var i = 0; i < count; i++)
216223
{
217-
handle.Free();
224+
handleList[i].Free();
218225
}
219-
220-
req._pins.Clear();
221-
req._handles.Clear();
226+
handleList.Clear();
222227
}
223228

224229
private static void UvWriteCb(IntPtr ptr, int status)
225230
{
226231
var req = FromIntPtr<UvWriteReq>(ptr);
227-
Unpin(req);
232+
req.UnpinGcHandles();
228233

229234
var callback = req._callback;
230235
req._callback = null;

0 commit comments

Comments
 (0)