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

SVM: API method for setting cache fork graph #3169

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

buffalojoec
Copy link

Problem

When creating a new TransactionBatchProcessor instance, you must configure the fork graph of the program cache in order for the cache to work properly. This can trip up many consumers of the SVM API.

We typically set this value on the program cache manually through something like this...

let fork_graph = Arc::new(RwLock::new(MockForkGraph {}));
let program_cache = batch_processor.program_cache.write().unwrap();
program_cache.fork_graph = Arc::downgrade(&fork_graph);

... which is just, awful.

Summary of Changes

Add an API method for setting the cache's fork graph, and use it everywhere.

dmakarov
dmakarov previously approved these changes Oct 15, 2024
Copy link

@LucasSte LucasSte left a comment

Choose a reason for hiding this comment

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

I know most changes were in tests, but be careful that acquiring a write lock is an expensive operation, so preferably acquire it only once.

Comment on lines +206 to 208
transaction_processor.set_fork_graph_in_program_cache(fork_graph);

let mut program_cache = transaction_processor.program_cache.write().unwrap();

Choose a reason for hiding this comment

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

Maintaining the older method here avoids acquiring the write lock twice.

Comment on lines +59 to -64
// Initialize the mocked fork graph.
processor.set_fork_graph_in_program_cache(fork_graph);

{
let mut cache = processor.program_cache.write().unwrap();

// Initialize the mocked fork graph.
cache.fork_graph = Some(Arc::downgrade(&fork_graph));

Choose a reason for hiding this comment

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

Two consecutive write locks here again.

Comment on lines +217 to +221
pub fn set_fork_graph_in_program_cache(&self, fork_graph: Arc<RwLock<FG>>) {
self.program_cache
.write()
.unwrap()
.set_fork_graph(Arc::downgrade(&fork_graph));

Choose a reason for hiding this comment

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

I believe this function can receive a reference to a fork graph, can't it? Like

pub fn set_fork_graph_in_program_cache(&self, fork_graph: &Arc<RwLock<FG>>)

Or maybe:

pub fn set_fork_graph_in_program_cache(&self, fork_graph: &RwLock<FG>)

Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately the cache's method requires a Weak<RwLock<FG>>, and from what I could tell, you can only obtain a Weak from an Arc.

Comment on lines +248 to 251
batch_processor.set_fork_graph_in_program_cache(Arc::new(RwLock::new(MockForkGraph {})));

let fork_graph = Arc::new(RwLock::new(MockForkGraph {}));
{
let mut program_cache = batch_processor.program_cache.write().unwrap();

Choose a reason for hiding this comment

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

Consecutive write locks.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, this kinda stinks. Do you suggest closing this PR then?

Copy link
Author

Choose a reason for hiding this comment

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

Perhaps this is better? #3203

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.

3 participants