From 8d8c4d2ba9e928eec67f2814e39cec7dc3f078e1 Mon Sep 17 00:00:00 2001 From: gop Date: Mon, 11 Sep 2023 18:01:59 -0500 Subject: [PATCH] bugfix: Added lock around block writes to reduce memory pressure Since downloader insert is an asynchronous process, writing thousands of large blocks into the DB at the same time is very memory intensive. Adding this lock will make sure that only one block is written at a time and from the testing in the garden environment this has shown improved performance in memory use while syncing --- core/core.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/core.go b/core/core.go index 25a50c190c..cb3d5b3864 100644 --- a/core/core.go +++ b/core/core.go @@ -53,6 +53,8 @@ type Core struct { appendQueue *lru.Cache processingCache *lru.Cache + writeBlockLock sync.RWMutex + quit chan struct{} // core quit channel } @@ -352,6 +354,9 @@ func (c *Core) Stop() { // WriteBlock write the block to the bodydb database func (c *Core) WriteBlock(block *types.Block) { + c.writeBlockLock.Lock() + defer c.writeBlockLock.Unlock() + if c.sl.IsBlockHashABadHash(block.Hash()) { return }