-
Notifications
You must be signed in to change notification settings - Fork 19
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
Global DAG extraction #21
Conversation
|
||
extractors = sorted(set(j["extractor"] for j in js)) | ||
|
||
for i in range(len(extractors)): |
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.
This PR also makes the plot output data for each combination of extractors.
This creates a lot of output- maybe we don't want this change, but I find it helpful.
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.
Mostly minor comments, but want some clarification on a few points.
(let me know if getting this in is urgent and I can still approve and we can fix later).
size: 1, | ||
}); | ||
self.hash_cons.insert(term, next_id); | ||
Some(next_id) |
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.
optional:
I think when the if/else are this long it can help to unnest after the "easy case", namely:
if children.is_empty() {
...
return Some(next_id);
}
// check if children contains this node
for child in &children {
...
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.
eh, I like nesting
return
considered harmful (only half joking)
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.
Up to you! I suggested this because there's already a good amount of early-return in this function (78, 92, 99, for example).
I personally think it is good sometimes, and not good other times. For code like this that feels pretty imperative either way I think it can make the code clearer. Feel free to keep as is though
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.
Ah I see
re-examining my head it seems that early return from a big code block makes me sad
src/extract/global_greedy_dag.rs
Outdated
self.hash_cons.insert(term, next_id); | ||
Some(next_id) | ||
} else { | ||
// check if children contains this node |
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.
Could you explain a bit why cycle detection here is "sound": afaik the standard greedy-dag/bottom-up doesn't have this check (does it need it? was it added later?)
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.
This cycle detection is sound because reachable
is the set of reachable eclasses from this term. We won't pick any e-nodes that rely on extracting this e-class.
The greedy-dag algorithm does have cycle detection on line 37
src/extract/global_greedy_dag.rs
Outdated
.unwrap(); | ||
|
||
let mut cost = node_cost + self.total_cost(children[biggest_child]); | ||
let mut reachable = Box::new(self.info[children[biggest_child]].reachable.clone()); |
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.
Why Box
?
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.
This is my hack to keep track of a persistent set which we are "mutating" during recursive calls. We can't mutate the container itself, so we need a wrapper as far as I know.
Is there a preferred solution in rust?
Alternatively, the recursive function could also return the new set each time.
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.
- I'm not against having subcalls return a new set.
- I still don't understand why you need a box here. Why can't you pass a
&mut Reachable
and then reassign as you are doing now?
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.
Wow TIL that &mut
literally means pass by reference. I thought &mut
was a mutable reference but you couldn't assign to it.
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.
Updated the code
// Also return the cost of the new nodes. | ||
fn get_cost(&self, shared: &mut Box<Reachable>, id: TermId) -> NotNan<f64> { | ||
let eclass = self.info[id].eclass.clone(); | ||
if shared.contains(&eclass) { |
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.
I think this is the key "why" for how this is better than greedy-dag: instead of doing an
Is that right? If so, I think it's worth a comment :) (If not, I'd definitely appreciate a comment)
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.
Exactly right- I left a comment
// This is the key to why this algorithm is faster than greedy_dag.
// While doing the set union between reachable sets, we can stop early
// if we find a shared term.
// Since the term with id
is shared, the reachable set of id
will already
// be in shared
.
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.
Thanks! Left some follow-ups but feel free to resolve as you see fit.
size: 1, | ||
}); | ||
self.hash_cons.insert(term, next_id); | ||
Some(next_id) |
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.
Up to you! I suggested this because there's already a good amount of early-return in this function (78, 92, 99, for example).
I personally think it is good sometimes, and not good other times. For code like this that feels pretty imperative either way I think it can make the code clearer. Feel free to keep as is though
src/extract/global_greedy_dag.rs
Outdated
.unwrap(); | ||
|
||
let mut cost = node_cost + self.total_cost(children[biggest_child]); | ||
let mut reachable = Box::new(self.info[children[biggest_child]].reachable.clone()); |
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.
- I'm not against having subcalls return a new set.
- I still don't understand why you need a box here. Why can't you pass a
&mut Reachable
and then reassign as you are doing now?
This PR adds another greedy DAG extractor which improves the performance of calculating a term's cost.
It does this by sharing subterms in a global DAG, and exiting early when doing set unions.