-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Compiler: avoid creating Set in merge_if_vars #12433
Conversation
It's easier to review this with "Hide whitespace" |
then_var = then_vars[name]? | ||
else_var = else_vars[name]? | ||
next if then_var | ||
|
||
# Check whether the var didn't change at all | ||
next if then_var.same?(else_var) | ||
merge_if_var(name, node, cond_vars, then_var, else_var, before_then_vars, before_else_vars, then_unreachable, else_unreachable) |
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.
then_var
must be nil
after next
. So I'm wondering if this tiny refactor might be a bit better? It just avoids fetching the actual then_var, because the value is not used.
then_var = then_vars[name]? | |
else_var = else_vars[name]? | |
next if then_var | |
# Check whether the var didn't change at all | |
next if then_var.same?(else_var) | |
merge_if_var(name, node, cond_vars, then_var, else_var, before_then_vars, before_else_vars, then_unreachable, else_unreachable) | |
next if then_vars.has_key?(name) | |
merge_if_var(name, node, cond_vars, nil, else_var, before_then_vars, before_else_vars, then_unreachable, else_unreachable) |
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.
The diff looks very weird. I don't know why... Just ignore the first 5 deleted lines in the suggestion.
* Don't create `MetaVar` unless needed * Make check earlier to avoid extra Hash lookups * Even less extra Hash lookups * Compiler: avoid creating Set in merge_if_vars (#12433)
Yes another optimization in
merge_if_vars
. This one has a bit more impact: less memory allocated, and slightly better performance.When merging
if
vars we need to traverse all vars defined in thethen
andelse
branches. We were doing this by creating a Set will all var names from both branches and then traversing that.However, it's more efficient if we first traverse the vars in
then
. Then when traversing the vars inelse
we can skip that if we already traversed it in thethen
branch.