From 345d2d27adc58f2f62571b3a76ccd7c908775fbd Mon Sep 17 00:00:00 2001 From: "lukasz.rozmej" Date: Wed, 3 May 2023 13:31:17 +0200 Subject: [PATCH] v2 --- .../FastBlocks/SyncStatusList.cs | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs b/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs index 56880cfe019..a293c4be718 100644 --- a/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs +++ b/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs @@ -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 { @@ -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) { @@ -43,20 +46,7 @@ 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; @@ -64,9 +54,8 @@ public void GetInfosForBatch(BlockInfo?[] blockInfos) if (currentNumber == LowestInsertWithoutGaps) { LowestInsertWithoutGaps--; - Interlocked.Decrement(ref _queueSize); + _queueSize--; } - break; case FastBlockStatus.Sent: break; @@ -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; } }