Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 83a5b06

Browse files
committed
Merge pull request #2871 from hackcraft/change_buffer_fill_loop
Don't do null check on every loop when filling buffer.
2 parents 2d90bb2 + 6cf2b4c commit 83a5b06

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/System.Linq/src/System/Linq/Enumerable.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3408,18 +3408,21 @@ internal Buffer(IEnumerable<TElement> source, bool queryInterfaces = true)
34083408

34093409
if (items == null)
34103410
{
3411-
foreach (TElement item in source)
3411+
using (IEnumerator<TElement> e = source.GetEnumerator())
34123412
{
3413-
if (items == null)
3413+
if (e.MoveNext())
34143414
{
34153415
items = new TElement[4];
3416+
items[0] = e.Current;
3417+
count = 1;
3418+
3419+
while (e.MoveNext())
3420+
{
3421+
if (items.Length == count) Array.Resize(ref items, checked(count * 2));
3422+
items[count] = e.Current;
3423+
++count;
3424+
}
34163425
}
3417-
else if (items.Length == count)
3418-
{
3419-
Array.Resize(ref items, checked(count * 2));
3420-
}
3421-
items[count] = item;
3422-
count++;
34233426
}
34243427
}
34253428

0 commit comments

Comments
 (0)