This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Optimize the bytecode for float(0) ?
Type: Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: matrixise, steven.daprano, vstinner
Priority: normal Keywords:

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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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) * (Python committer) 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:01adminsetgithub: 77863
2018-05-29 13:01:34matrixisesetstatus: open -> closed
resolution: not a bug
stage: resolved
2018-05-29 13:01:15matrixisesetmessages: + msg318021
2018-05-29 12:54:24steven.dapranosetnosy: + steven.daprano
messages: + msg318020
2018-05-29 12:50:46vstinnersetmessages: + msg318019
2018-05-29 12:48:04matrixisesetmessages: + msg318017
2018-05-29 12:47:12matrixisecreate