Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce SyncStatusList lock contention v2 #5647

Merged
merged 3 commits into from
May 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,17 +46,16 @@ public void GetInfosForBatch(BlockInfo?[] blockInfos)
switch (_statuses[currentNumber])
{
case FastBlockStatus.Unknown:
blockInfos[collected] = _blockTree.FindCanonicalBlockInfo(currentNumber);
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 @@ -64,13 +66,27 @@ public void GetInfosForBatch(BlockInfo?[] blockInfos)
currentNumber--;
}
}

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

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