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 the bytecode for float(0) ? #77863
Comments
Hi, Maybe already discussed with Victor but I think there is no optimization when we have this simple case for float(X) and int(X) Example: >>> import dis
>>> dis.dis("x = float(0)")
1 0 LOAD_NAME 0 (float)
2 LOAD_CONST 0 (0)
4 CALL_FUNCTION 1
6 STORE_NAME 1 (x)
8 LOAD_CONST 1 (None)
10 RETURN_VALUE
>>> dis.dis("x = 1 + 1")
1 0 LOAD_CONST 0 (2)
2 STORE_NAME 0 (x)
4 LOAD_CONST 1 (None)
6 RETURN_VALUE
>>> dis.dis("x = int(0)")
1 0 LOAD_NAME 0 (int)
2 LOAD_CONST 0 (0)
4 CALL_FUNCTION 1
6 STORE_NAME 1 (x)
8 LOAD_CONST 1 (None)
10 RETURN_VALUE
>>> There is an optim for x = 1 + 1 |
Victor, if you confirm, maybe you could help me for a mentoring for this issue? |
float and int names can be replaced in the current namespace, so you cannot implement such optimization :-( http://fatoptimizer.readthedocs.io/en/latest/optimizations.html#call-pure Example in the REPL: >>> float=bool
>>> float(0)
False
>>> int=len
>>> int("hello world!")
12 I suggest to close this issue as NOTABUG. You need to implement guards at runtime to implement such optimizations without breaking the Python semantics. It is exactly what I implemented in my FAT Python project: |
I'm sorry, it isn't clear what optimizations for float(X) and int(X) you are referring to. I can only guess that you want to optimize: float(0) to use LOAD_CONST 0.0 instead of calling the float() function. If that is what you want, we can't do that because float() may have been shadowed or replaced. Optimizing 1+1 is safe because it involves only literals and operators, no name look-ups. If that is not what you want, please explain what optimization you are referring to. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: