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

Optimize and/or #854

Closed
gilch opened this issue Jul 25, 2015 · 1 comment · Fixed by #2514
Closed

Optimize and/or #854

gilch opened this issue Jul 25, 2015 · 1 comment · Fixed by #2514
Labels

Comments

@gilch
Copy link
Member

gilch commented Jul 25, 2015

Depends on #824.
Related #842.

Even in the case that and/or contains a statement, it may have several expressions in a row that don't need to be converted to if branches. Hy could avoid redundant assignments by keeping those runs as expressions and only assigning the result to an anonymous variable.

For example:

=> (print (and 1 2 3 (setv x 1) 4 5 6))
_hy_anon_var_1 = 1 and 2 and 3
if _hy_anon_var_1:
    x = 1
    _hy_anon_var_1 = x and 4 and 5 and 6
print(_hy_anon_var_1)
6
@gilch
Copy link
Member Author

gilch commented Aug 11, 2015

For comparison, this is what Hy is doing currently to the above example. (#824 has been merged)

=> (print (and 1 2 3 (setv x 1) 4 5 6))
_hy_anon_var_1 = 1
if _hy_anon_var_1:
    _hy_anon_var_1 = 2
    if _hy_anon_var_1:
        _hy_anon_var_1 = 3
        if _hy_anon_var_1:
            x = 1
            _hy_anon_var_1 = x
            if _hy_anon_var_1:
                _hy_anon_var_1 = 4
                if _hy_anon_var_1:
                    _hy_anon_var_1 = 5
                    if _hy_anon_var_1:
                        _hy_anon_var_1 = 6
print(_hy_anon_var_1)
6

Notice there are eight assignments, but the optimized version only needs three.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants