From df320a99abd081795ebad939a479341e16041120 Mon Sep 17 00:00:00 2001 From: benaadams Date: Wed, 3 May 2023 01:25:54 +0100 Subject: [PATCH 1/3] Reduce SyncStatusList lock contention --- .../FastBlocks/SyncStatusList.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs b/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs index 4831760de40..56880cfe019 100644 --- a/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs +++ b/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs @@ -43,7 +43,20 @@ public void GetInfosForBatch(BlockInfo?[] blockInfos) switch (_statuses[currentNumber]) { case FastBlockStatus.Unknown: - blockInfos[collected] = _blockTree.FindCanonicalBlockInfo(currentNumber); + 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; _statuses[currentNumber] = FastBlockStatus.Sent; collected++; break; From 345d2d27adc58f2f62571b3a76ccd7c908775fbd Mon Sep 17 00:00:00 2001 From: "lukasz.rozmej" Date: Wed, 3 May 2023 13:31:17 +0200 Subject: [PATCH 2/3] 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; } } From 343966f0d8899c8fc181fd21fd8a5095ea8dc2bb Mon Sep 17 00:00:00 2001 From: "lukasz.rozmej" Date: Wed, 3 May 2023 14:41:38 +0200 Subject: [PATCH 3/3] add exception handling --- .../FastBlocks/SyncStatusList.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs b/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs index a293c4be718..e1426beaf6f 100644 --- a/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs +++ b/src/Nethermind/Nethermind.Synchronization/FastBlocks/SyncStatusList.cs @@ -70,7 +70,15 @@ public void GetInfosForBatch(BlockInfo?[] blockInfos) for (int index = 0; index < toSent.Count; index++) { (int collected, long currentNumber) sent = toSent[index]; - blockInfos[sent.collected] = _blockTree.FindCanonicalBlockInfo(sent.currentNumber); + try + { + blockInfos[sent.collected] = _blockTree.FindCanonicalBlockInfo(sent.currentNumber); + } + catch + { + _statuses[sent.currentNumber] = FastBlockStatus.Unknown; + throw; + } } }