From e0e1cb6ee9a6be95c11c156089a2e5e2807806e6 Mon Sep 17 00:00:00 2001 From: Nikolay Baraboshkin Date: Wed, 4 Dec 2024 21:54:09 +0400 Subject: [PATCH] improve the performance of RollingWindow.GetEnumerator Closes #8443 --- Common/Indicators/RollingWindow.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Common/Indicators/RollingWindow.cs b/Common/Indicators/RollingWindow.cs index d2f7ed526813..605f13ad2397 100644 --- a/Common/Indicators/RollingWindow.cs +++ b/Common/Indicators/RollingWindow.cs @@ -244,18 +244,20 @@ public bool IsReady /// 1 public IEnumerator GetEnumerator() { - // we make a copy on purpose so the enumerator isn't tied - // to a mutable object, well it is still mutable but out of scope - var temp = new List(_list.Count); try { _listLock.EnterReadLock(); - for (int i = 0; i < _list.Count; i++) + // we make a copy on purpose so the enumerator isn't tied + // to a mutable object, well it is still mutable but out of scope + var count = _list.Count; + var temp = new T[count]; + for (int i = count - 1; i >= 0; i--) { - temp.Add(this[i]); + temp[count - 1 - i] = _list[(_tail + i) % count]; } - return temp.GetEnumerator(); + + return ((IEnumerable) temp).GetEnumerator(); } finally {