Skip to content

Commit 0becdfb

Browse files
committed
ETCM-463: Iterate instead of access by each index.
1 parent ce72804 commit 0becdfb

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

src/main/scala/io/iohk/ethereum/network/TimeSlotStats.scala

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,11 @@ class TimeSlotStats[K, V: Monoid] private (
5858

5959
private def fold[A](init: A, window: Duration)(f: (A, Map[K, V]) => A) = {
6060
val (start, end) = slotRange(currentTimeMillis, window)
61-
62-
def loop(idx: Int, acc: A): A = {
63-
val entry = buffer(idx)
64-
if (entry.slotId < start || end < entry.slotId)
65-
acc
66-
else {
67-
val nextAcc = f(acc, entry.slotStats)
68-
val nextIdx = pred(idx)
69-
if (nextIdx == lastIdx) nextAcc else loop(nextIdx, nextAcc)
61+
buffer.values
62+
.collect {
63+
case entry if start <= entry.slotId && entry.slotId <= end => entry.slotStats
7064
}
71-
}
72-
73-
loop(lastIdx, init)
65+
.foldLeft(init)(f)
7466
}
7567

7668
private def currentTimeMillis: Timestamp =

0 commit comments

Comments
 (0)