Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock-free latest_l2_height in gas price service #2546

Open
wants to merge 2 commits into
base: chore/add-tests-for-v1-gas-service
Choose a base branch
from

Conversation

rafal-ch
Copy link
Contributor

Description

This PR removes the Mutex around the latest_l2_height in the gas price service.

Before requesting review

  • I have reviewed the code myself

@rafal-ch rafal-ch added no changelog Skip the CI check of the changelog modification release labels Jan 10, 2025
.latest_l2_height
.lock()
.map_err(|err| anyhow::anyhow!("lock error: {:?}", err))?;
let latest_l2_height = self.latest_l2_height.load(Ordering::SeqCst);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let latest_l2_height = self.latest_l2_height.load(Ordering::SeqCst);
let latest_l2_height = self.latest_l2_height.load(Ordering::Acquire);

SeqCst is not needed unless you have at least two atomic variables. The purpose of SeqCst is to make sure that different threads do not see updates to different variables in different order. See https://stackoverflow.com/questions/14861822/acquire-release-versus-sequentially-consistent-memory-order for an example.

(Just mentioning this, overall I think we can keep SeqCst)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in 5eeba9e

@@ -103,7 +107,7 @@ where
.filter_costs_that_have_values_greater_than_l2_block_height(da_block_costs)?;
tracing::debug!(
"the latest l2 height is: {:?}",
*self.latest_l2_height.lock().unwrap()
self.latest_l2_height.load(Ordering::SeqCst)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.latest_l2_height.load(Ordering::SeqCst)
self.latest_l2_height.load(Ordering::Acquire)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in 5eeba9e

.lock()
.map_err(|err| anyhow!("Error locking latest L2 block: {:?}", err))?;
*latest_l2_block = BlockHeight::from(height);
self.latest_l2_block.store(height, Ordering::SeqCst);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.latest_l2_block.store(height, Ordering::SeqCst);
self.latest_l2_block.store(height, Ordering::Release);

(see the corresponding comment on the load operation)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in 5eeba9e

@acerone85
Copy link
Contributor

small comments because I am picky about memory ordering :) , but overall LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no changelog Skip the CI check of the changelog modification release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants