Skip to content

Conversation

bhartnett
Copy link
Contributor

@bhartnett bhartnett commented Oct 7, 2025

This implements dynamic adjustment of the forked chain persist batch size so that we can set the best size to optimize for performance while also taking into account how long we can safely block the event loop when computing the stateroot and persisting to the database.

The idea here is that we have a target run time of 1 second and we increase the batch size if the run time is below the target and decrease it if the run time is over the target. It looks like the various p2p networking timeouts are set to between 5-10 seconds and so using a target of 1 second seems reasonable to avoid hitting these timeouts.

runTime = finishTime - startTime


if runTime < targetTime and c.persistBatchSize <= batchSizeUpperBound:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EthTime.now() resolution is one second. runTime < targetTime(1 second) will not works. And persistBatchsize will never increased, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does work because if the run time is less than one second then runTime = 0. In other words it appears to truncate or round down the value since the precision only supports seconds.

I've been testing it and verified that both increasing the size and decreasing the batch size does indeed work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add some comment to avoid confusion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I might update this to measure the time in milliseconds instead of seconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the code to use milliseconds precision instead.

@bhartnett
Copy link
Contributor Author

After testing these changes I found that increasing/decreasing the batch size by a factor of 2 produced better results than just increasing/decreasing by 5. This was because it allows the batch size to grow or shrink fast enough in response to changes in the size of the blocks during the sync process.

I'm also going to leave this feature disabled by default for now considering that it is a bit experimental. It can be manually enabled and can be set as the default in a future PR if all goes well.

@bhartnett bhartnett requested a review from jangko October 8, 2025 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants