@@ -26,9 +26,16 @@ import (
2626 "github.com/CortexFoundation/CortexTheseus/rlp"
2727)
2828
29- // This is the maximum amount of data that will be buffered in memory
30- // for a single freezer table batch.
31- const freezerBatchBufferLimit = 2 * 1024 * 1024
29+ const (
30+ // This is the maximum amount of data that will be buffered in memory
31+ // for a single freezer table batch.
32+ freezerBatchBufferLimit = 2 * 1024 * 1024
33+
34+ // freezerTableFlushThreshold defines the threshold for triggering a freezer
35+ // table sync operation. If the number of accumulated uncommitted items exceeds
36+ // this value, a sync will be scheduled.
37+ freezerTableFlushThreshold = 512
38+ )
3239
3340// freezerBatch is a write operation of multiple items on a freezer.
3441type freezerBatch struct {
@@ -202,14 +209,17 @@ func (batch *freezerTableBatch) commit() error {
202209
203210 // Update headBytes of table.
204211 batch .t .headBytes += dataSize
212+ items := batch .curItem - batch .t .items .Load ()
205213 batch .t .items .Store (batch .curItem )
206214
207215 // Update metrics.
208216 batch .t .sizeGauge .Inc (dataSize + indexSize )
209217 batch .t .writeMeter .Mark (dataSize + indexSize )
210218
211219 // Periodically sync the table, todo (rjl493456442) make it configurable?
212- if time .Since (batch .t .lastSync ) > 30 * time .Second {
220+ batch .t .uncommitted += items
221+ if batch .t .uncommitted > freezerTableFlushThreshold && time .Since (batch .t .lastSync ) > 30 * time .Second {
222+ batch .t .uncommitted = 0
213223 batch .t .lastSync = time .Now ()
214224 return batch .t .Sync ()
215225 }
0 commit comments