-
Notifications
You must be signed in to change notification settings - Fork 372
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
Conditionals waste assignments. #842
Comments
I'm toying with just fixing up |
I thought #920 was supposed to fix this, but it seems to be broken again. After that PR, we had => (if 1 1 (if 2 (setv x 2) (if 3 3)))
if 1:
_hy_anon_var_1 = 1
elif 2:
x = 2
_hy_anon_var_1 = x
else:
_hy_anon_var_1 = (3 if 3 else None) But now, on master, => (if 1 1 (if 2 (setv x 2) (if 3 3)))
if 1:
_hy_anon_var_2 = 1
else:
if 2:
x = 2
_hy_anon_var_1 = None
else:
_hy_anon_var_1 = (3 if 3 else None)
_hy_anon_var_2 = _hy_anon_var_1
_hy_anon_var_2
1 Though now we'd use |
That doesn't seem right. The equivalent => (cond [1 1] [2 (setv x 2)] [3 3])
if 1:
_hy_anon_var_2 = 1
else:
if 2:
x = 2
_hy_anon_var_1 = None
else:
_hy_anon_var_1 = (3 if 3 else None)
_hy_anon_var_2 = _hy_anon_var_1
_hy_anon_var_2 But #962 didn't touch |
Actually, no. When the compiler looks at the code, it's unexpanded, so it needs to now be checking for |
Special-casing a macro name seems bad. It might be better to move |
AFAIK IMO I wouldn't find moving |
Or maybe we could rewrite the |
These days,
becomes
We should perhaps use a single |
Conditionals containing statements are doing unnecessary work. I know, Python is not renowned for its performance, but let's not make it worse. This kind of thing can matter inside nested loops.
Currently a
cond
like this:Turns into this:
But wouldn't it would work just as well like this?
Half the assignments for the same effect. And the
hy --spy
output is much more readable. I don't think CPython can automatically optimize this kind of thing. I know not all of them happen for any given branch, but the deeper ones will still do more assignments than they should.Python has no switch/case. Sometimes you can get away with dictionary lookups instead, but if you need side effects or performance, you're supposed to use cascading
elif
where you would have used a switch.Currently there's no
elif
form in Hy.cond
is the closest we've got. Now I could write nestedif
s instead (or a macro to do it for me) but look at what it turns into:Not quite as bad as the
cond
since we have a finalelse
, but still twice what is necessary:Maybe it's too hard to optimize nested
if
forms like that? Iscond
defined in terms of Hy'sif
form in the first place? I didn't implementcond
but you could totally do it as a macro in terms ofif
.Why not extend Hy's
if
to work like Arc Lisp's (as I proposed in #830 ), which can accept two or more arguments:etc., for any number of inner
elif
s you need.if
form, instead of optimizing nested forms.if
act more like Python's cascadingelif
.cond
can be redefined in the same way, and get the same benefits.The text was updated successfully, but these errors were encountered: