Skip to content

Commit

Permalink
improve the performance of RollingWindow.GetEnumerator
Browse files Browse the repository at this point in the history
Closes #8443
  • Loading branch information
starteleport committed Dec 4, 2024
1 parent a1d0b6c commit e0e1cb6
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Common/Indicators/RollingWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,20 @@ public bool IsReady
/// <filterpriority>1</filterpriority>
public IEnumerator<T> 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<T>(_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<T>) temp).GetEnumerator();
}
finally
{
Expand Down

0 comments on commit e0e1cb6

Please sign in to comment.