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

Improve MutableState / Machines #2130

Merged
merged 10 commits into from
Nov 22, 2024
Merged

Improve MutableState / Machines #2130

merged 10 commits into from
Nov 22, 2024

Conversation

chriseth
Copy link
Member

@chriseth chriseth commented Nov 22, 2024

MutableState is the main way to get access to sub-machines during witness generation. We used to create copies of MutableState for each lookup, extracting the "current machine" where we need mutable access and creating copies of references to the other machines. This causes an allocation for each lookup (including fixed lookups, I think) which is bad for performance.

This PR changes the approach to use RefCell instead: MutableState now owns the machines and for each call to a machine, we mutably borrow that machine. The RefCell mechanism ensures that there are no recursive calls to machines and also avoids allocations.

I also changed the query callback to use non-mut references.

The first and third commits only move code around.

@chriseth chriseth marked this pull request as ready for review November 22, 2024 13:54
Copy link
Collaborator

@georgwiese georgwiese left a comment

Choose a reason for hiding this comment

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

Awesome! 🤩

My main question is about lifetimes:

  1. I don't understand why MutableData needs two different lifetimes.
  2. I'm pretty sure the 3 lifetimes explained in the Processor docstring can be reduced to 2 now, which is great!

I feel like fixing this would likely lead to a lot of changes all over the witgen codebase, so probably this should be done in a follow-up PR.

Comment on lines +18 to +19
machines: Vec<RefCell<KnownMachine<'a, T>>>,
identity_to_machine_index: BTreeMap<u64, usize>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
machines: Vec<RefCell<KnownMachine<'a, T>>>,
identity_to_machine_index: BTreeMap<u64, usize>,
identity_to_machine: BTreeMap<u64, RefCell<KnownMachine<'a, T>>>,

Why not this?

Copy link
Member Author

Choose a reason for hiding this comment

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

take_witness_col_values uses the given order of machines. Also If we do it like that, then multiple identities calling the same machine will lead to a copy of the machine, which we don't want to have.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Two good reasons!

pub struct MutableState<'a, 'b, T: FieldElement, Q: QueryCallback<T>> {
machines: Vec<RefCell<KnownMachine<'a, T>>>,
identity_to_machine_index: BTreeMap<u64, usize>,
query_callback: &'b Q,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Couldn't this be 'a as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably yes, but it's more annoying lifetime changes because it will lead to Processor requiring fewer lifetimes etc. Will do it in another pr.

@@ -98,7 +99,7 @@ pub struct Processor<'a, 'b, 'c, T: FieldElement, Q: QueryCallback<T>> {
/// The values of the publics
publics: BTreeMap<&'a str, T>,
/// The mutable state
mutable_state: &'c mut MutableState<'a, 'b, T, Q>,
mutable_state: &'c MutableState<'a, 'b, T, Q>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

A few lines above, there is a comment explaining the 3 lifetimes, including:

/// - `'b`: The duration of this machine's call (e.g. the mutable references of the other machines)

I think this is no longer accurate?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's the lifetime of the query callback.

@georgwiese georgwiese added this pull request to the merge queue Nov 22, 2024
Merged via the queue into main with commit 81196ae Nov 22, 2024
14 checks passed
@georgwiese georgwiese deleted the improve_mutable_state branch November 22, 2024 18:06
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