Issue33682
Created on 2018-05-29 12:47 by matrixise, last changed 2022-04-11 14:59 by admin. This issue is now closed.
| Messages (5) | |||
|---|---|---|---|
| msg318016 - (view) | Author: Stéphane Wirtel (matrixise) * ![]() |
Date: 2018-05-29 12:47 | |
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
|
|||
| msg318017 - (view) | Author: Stéphane Wirtel (matrixise) * ![]() |
Date: 2018-05-29 12:48 | |
Victor, if you confirm, maybe you could help me for a mentoring for this issue? |
|||
| msg318019 - (view) | Author: STINNER Victor (vstinner) * ![]() |
Date: 2018-05-29 12:50 | |
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 http://fatoptimizer.readthedocs.io/en/latest/semantics.html#builtin-functions-replaced-in-the-middle-of-a-function 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: https://faster-cpython.readthedocs.io/fat_python.html |
|||
| msg318020 - (view) | Author: Steven D'Aprano (steven.daprano) * ![]() |
Date: 2018-05-29 12:54 | |
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.
|
|||
| msg318021 - (view) | Author: Stéphane Wirtel (matrixise) * ![]() |
Date: 2018-05-29 13:01 | |
@Victor, Thanks and you are right, int and float are not keywords of Python, in this case, we can override them. @Steven, in fact, the optimization was, when you see float/int (if they are keywords), don't call the function via the bytecode. Thanks and I close this issue. |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:01 | admin | set | github: 77863 |
| 2018-05-29 13:01:34 | matrixise | set | status: open -> closed resolution: not a bug stage: resolved |
| 2018-05-29 13:01:15 | matrixise | set | messages: + msg318021 |
| 2018-05-29 12:54:24 | steven.daprano | set | nosy:
+ steven.daprano messages: + msg318020 |
| 2018-05-29 12:50:46 | vstinner | set | messages: + msg318019 |
| 2018-05-29 12:48:04 | matrixise | set | messages: + msg318017 |
| 2018-05-29 12:47:12 | matrixise | create | |
