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

perf: Optimize LinearCombination #216

Merged
merged 4 commits into from
Sep 27, 2021
Merged

perf: Optimize LinearCombination #216

merged 4 commits into from
Sep 27, 2021

Conversation

dignifiedquire
Copy link

The original implementation was using a hashmap, but we only need to store numbers, so we can avoid all hashing. The technique is based on storing sparse matricies and gives overall a speedup of 10-40% in circuit synthesis. The variance comes from the fact that LinearCombination is used in very different sizes and amounts between circuits

}

pub fn iter(&self) -> impl Iterator<Item = (&usize, &T)> + '_ {
self.values.iter().map(|(key, value)| (key, value))
Copy link

Choose a reason for hiding this comment

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

Question out of interest. Why is the map() needed, why isn't iter() enough?

Copy link
Author

Choose a reason for hiding this comment

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

because iter gives &(usize, T)

F: FnOnce() -> T,
G: FnOnce(&mut T),
{
if let Some((last_index, last_key)) = self.last_inserted {
Copy link

Choose a reason for hiding this comment

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

It took me a while to understand what's going on. So perhaps a comment would be good. If i understand it correctly the whole if part is an optimization for a case that often happens, which prevents doing a full binary search.

Copy link
Author

Choose a reason for hiding this comment

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

done

@dignifiedquire dignifiedquire merged commit f46302e into master Sep 27, 2021
@dignifiedquire dignifiedquire deleted the perf-lc branch September 27, 2021 20:14
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