@@ -38,6 +38,7 @@ public class HeadersSyncFeed : ActivatedSyncFeed<HeadersSyncBatch?>
38
38
private readonly Lock _handlerLock = new ( ) ;
39
39
40
40
private readonly int _headersRequestSize = GethSyncLimits . MaxHeaderFetch ;
41
+ private readonly ulong _fastHeadersMemoryBudget ;
41
42
protected long _lowestRequestedHeaderNumber ;
42
43
43
44
protected Hash256 _nextHeaderHash ;
@@ -67,7 +68,6 @@ public class HeadersSyncFeed : ActivatedSyncFeed<HeadersSyncBatch?>
67
68
/// </summary>
68
69
private readonly ReaderWriterLockSlim _resetLock = new ( ) ;
69
70
70
- private IEnumerator < KeyValuePair < long , HeadersSyncBatch > > ? _enumerator ;
71
71
private ulong _memoryEstimate ;
72
72
private long _headersEstimate ;
73
73
@@ -104,19 +104,14 @@ private long HeadersInQueue
104
104
private long CalculateHeadersInQueue ( )
105
105
{
106
106
// Reuse the enumerator
107
- var enumerator = Interlocked . Exchange ( ref _enumerator , null ) ?? _dependencies . GetEnumerator ( ) ;
107
+ using var enumerator = _dependencies . GetEnumerator ( ) ;
108
108
109
109
long count = 0 ;
110
110
while ( enumerator . MoveNext ( ) )
111
111
{
112
112
count += enumerator . Current . Value . Response ? . Count ?? 0 ;
113
113
}
114
114
115
- // Stop gap method to reduce allocations from non-struct enumerator
116
- // https://github.com/dotnet/runtime/pull/38296
117
- enumerator . Reset ( ) ;
118
- _enumerator = enumerator ;
119
-
120
115
return count ;
121
116
}
122
117
@@ -138,19 +133,14 @@ private ulong MemoryInQueue
138
133
private ulong CalculateMemoryInQueue ( )
139
134
{
140
135
// Reuse the enumerator
141
- var enumerator = Interlocked . Exchange ( ref _enumerator , null ) ?? _dependencies . GetEnumerator ( ) ;
136
+ using var enumerator = _dependencies . GetEnumerator ( ) ;
142
137
143
138
ulong amount = 0 ;
144
139
while ( enumerator . MoveNext ( ) )
145
140
{
146
141
amount += ( ulong ) enumerator . Current . Value ? . ResponseSizeEstimate ;
147
142
}
148
143
149
- // Stop gap method to reduce allocations from non-struct enumerator
150
- // https://github.com/dotnet/runtime/pull/38296
151
- enumerator . Reset ( ) ;
152
- _enumerator = enumerator ;
153
-
154
144
return amount ;
155
145
}
156
146
@@ -169,6 +159,7 @@ public HeadersSyncFeed(
169
159
_syncConfig = syncConfig ?? throw new ArgumentNullException ( nameof ( syncConfig ) ) ;
170
160
_logger = logManager ? . GetClassLogger < HeadersSyncFeed > ( ) ?? throw new ArgumentNullException ( nameof ( HeadersSyncFeed ) ) ;
171
161
_totalDifficultyStrategy = totalDifficultyStrategy ?? new CumulativeTotalDifficultyStrategy ( ) ;
162
+ _fastHeadersMemoryBudget = syncConfig . FastHeadersMemoryBudget ;
172
163
173
164
if ( ! _syncConfig . UseGethLimitsInFastBlocks )
174
165
{
@@ -229,7 +220,7 @@ private bool ShouldBuildANewBatch()
229
220
230
221
bool noBatchesLeft = AllHeadersDownloaded
231
222
|| destinationHeaderRequested
232
- || MemoryInQueue >= MemoryAllowance . FastBlocksMemory
223
+ || MemoryInQueue >= _fastHeadersMemoryBudget
233
224
|| isImmediateSync && AnyHeaderDownloaded ;
234
225
235
226
if ( noBatchesLeft )
@@ -512,15 +503,18 @@ private void EnqueueBatch(HeadersSyncBatch batch, bool skipPersisted = false)
512
503
}
513
504
514
505
headers . AsSpan ( ) . Reverse ( ) ;
515
-
516
- using HeadersSyncBatch newBatchToProcess = new HeadersSyncBatch ( ) ;
517
- newBatchToProcess . StartNumber = lastHeader . Number ;
518
- newBatchToProcess . RequestSize = headers . Count ;
519
- newBatchToProcess . Response = headers ;
520
- if ( _logger . IsDebug ) _logger . Debug ( $ "Handling header portion { newBatchToProcess . StartNumber } to { newBatchToProcess . EndNumber } with persisted headers.") ;
521
- InsertHeaders ( newBatchToProcess ) ;
522
-
523
506
int newRequestSize = batch . RequestSize - headers . Count ;
507
+ if ( headers . Count > 0 )
508
+ {
509
+ using HeadersSyncBatch newBatchToProcess = new HeadersSyncBatch ( ) ;
510
+ newBatchToProcess . StartNumber = lastHeader . Number ;
511
+ newBatchToProcess . RequestSize = headers . Count ;
512
+ newBatchToProcess . Response = headers ;
513
+ if ( _logger . IsDebug ) _logger . Debug ( $ "Handling header portion { newBatchToProcess . StartNumber } to { newBatchToProcess . EndNumber } with persisted headers.") ;
514
+ InsertHeaders ( newBatchToProcess ) ;
515
+ MarkDirty ( ) ;
516
+ HeadersSyncQueueReport . Update ( HeadersInQueue ) ;
517
+ }
524
518
525
519
if ( newRequestSize == 0 ) return null ;
526
520
0 commit comments