Skip to content

Commit

Permalink
v2
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaszRozmej authored and benaadams committed May 4, 2023
1 parent df320a9 commit 345d2d2
Showing 1 changed file with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System;
using System.Collections;
using System.Threading;
using Nethermind.Blockchain;
using Nethermind.Core;
using Nethermind.Core.Collections;

namespace Nethermind.Synchronization.FastBlocks
{
Expand All @@ -27,8 +29,9 @@ public SyncStatusList(IBlockTree blockTree, long pivotNumber, long? lowestInsert

public void GetInfosForBatch(BlockInfo?[] blockInfos)
{
int collected = 0;
using ArrayPoolList<(int collected, long currentNumber)> toSent = new(blockInfos.Length);

int collected = 0;
long currentNumber = LowestInsertWithoutGaps;
lock (_statuses)
{
Expand All @@ -43,30 +46,16 @@ public void GetInfosForBatch(BlockInfo?[] blockInfos)
switch (_statuses[currentNumber])
{
case FastBlockStatus.Unknown:
BlockInfo? blockInfo = null;
// Release the lock while performing the longer storage operation
// to reduce lock contention
Monitor.Exit(_statuses);
try
{
blockInfo = _blockTree.FindCanonicalBlockInfo(currentNumber);
}
finally
{
// Re-enter the lock
Monitor.Enter(_statuses);
}
blockInfos[collected] = blockInfo;
toSent.Add((collected, currentNumber));
_statuses[currentNumber] = FastBlockStatus.Sent;
collected++;
break;
case FastBlockStatus.Inserted:
if (currentNumber == LowestInsertWithoutGaps)
{
LowestInsertWithoutGaps--;
Interlocked.Decrement(ref _queueSize);
_queueSize--;
}

break;
case FastBlockStatus.Sent:
break;
Expand All @@ -77,13 +66,19 @@ public void GetInfosForBatch(BlockInfo?[] blockInfos)
currentNumber--;
}
}

for (int index = 0; index < toSent.Count; index++)
{
(int collected, long currentNumber) sent = toSent[index];
blockInfos[sent.collected] = _blockTree.FindCanonicalBlockInfo(sent.currentNumber);
}
}

public void MarkInserted(in long blockNumber)
{
Interlocked.Increment(ref _queueSize);
lock (_statuses)
{
_queueSize++;
_statuses[blockNumber] = FastBlockStatus.Inserted;
}
}
Expand Down

0 comments on commit 345d2d2

Please sign in to comment.