-
Notifications
You must be signed in to change notification settings - Fork 120
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
Conversation
c0d55b7
to
38ed8bf
Compare
b235864
to
ebeb9c5
Compare
} | ||
|
||
pub fn iter(&self) -> impl Iterator<Item = (&usize, &T)> + '_ { | ||
self.values.iter().map(|(key, value)| (key, value)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
81596a1
to
6718014
Compare
6718014
to
61af305
Compare
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