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

Compiler: avoid creating Set in merge_if_vars #12433

Merged
merged 2 commits into from
Sep 2, 2022

Conversation

asterite
Copy link
Member

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 the then and else 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 in else we can skip that if we already traversed it in the then branch.

@asterite
Copy link
Member Author

It's easier to review this with "Hide whitespace"

Comment on lines 1897 to 1900
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)
Copy link
Member

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.

Suggested change
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)

Copy link
Member

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.

@straight-shoota straight-shoota added this to the 1.6.0 milestone Aug 31, 2022
@straight-shoota straight-shoota merged commit e6882ff into opt/merge_if_vars Sep 2, 2022
@straight-shoota straight-shoota deleted the opt/merge_if_vars_no_set branch September 2, 2022 07:53
asterite added a commit that referenced this pull request Sep 5, 2022
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants