From 44cb7bff485f32c8cabdaf0a00298b56be4914b1 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 2 Jul 2019 22:53:36 -0400 Subject: [PATCH] Avoid extra allocation and interface calls in Append (dotnet/corefx#39045) Commit migrated from https://github.com/dotnet/corefx/commit/2ead5143ce31d6d25089a1f16e9619a374d48d4b --- .../System.Linq/src/System/Linq/AppendPrepend.SpeedOpt.cs | 8 +++----- src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs | 2 +- src/libraries/System.Linq/src/System/Linq/SingleLinkedNode.cs | 11 +---------- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/libraries/System.Linq/src/System/Linq/AppendPrepend.SpeedOpt.cs b/src/libraries/System.Linq/src/System/Linq/AppendPrepend.SpeedOpt.cs index a8d3688..a7d3532 100644 --- a/src/libraries/System.Linq/src/System/Linq/AppendPrepend.SpeedOpt.cs +++ b/src/libraries/System.Linq/src/System/Linq/AppendPrepend.SpeedOpt.cs @@ -181,19 +181,17 @@ namespace System.Linq { int count = GetCount(onlyIfCheap: true); List list = count == -1 ? new List() : new List(count); + for (SingleLinkedNode node = _prepended; node != null; node = node.Linked) { list.Add(node.Item); } list.AddRange(_source); + if (_appended != null) { - IEnumerator e = _appended.GetEnumerator(_appendCount); - while (e.MoveNext()) - { - list.Add(e.Current); - } + list.AddRange(_appended.ToArray(_appendCount)); } return list; diff --git a/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs b/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs index a9bf0a6..08fe6af 100644 --- a/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs +++ b/src/libraries/System.Linq/src/System/Linq/AppendPrepend.cs @@ -220,7 +220,7 @@ namespace System.Linq return false; } - _enumerator = _appended.GetEnumerator(_appendCount); + _enumerator = ((IEnumerable)_appended.ToArray(_appendCount)).GetEnumerator(); _state = 4; goto case 4; case 4: diff --git a/src/libraries/System.Linq/src/System/Linq/SingleLinkedNode.cs b/src/libraries/System.Linq/src/System/Linq/SingleLinkedNode.cs index dd85bd8..fdcb433 100644 --- a/src/libraries/System.Linq/src/System/Linq/SingleLinkedNode.cs +++ b/src/libraries/System.Linq/src/System/Linq/SingleLinkedNode.cs @@ -65,15 +65,6 @@ namespace System.Linq } /// - /// Gets an that enumerates the items of this node's singly-linked list in reverse. - /// - /// The number of items in this node. - public IEnumerator GetEnumerator(int count) - { - return ((IEnumerable)ToArray(count)).GetEnumerator(); - } - - /// /// Gets the node at a logical index by walking the linked list. /// /// The logical index. @@ -98,7 +89,7 @@ namespace System.Linq /// Returns an that contains the items of this node's singly-linked list in reverse. /// /// The number of items in this node. - private TSource[] ToArray(int count) + public TSource[] ToArray(int count) { Debug.Assert(count == GetCount()); -- 2.7.4