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 the bytecode for float(0) ? #77863

Closed
matrixise opened this issue May 29, 2018 · 5 comments
Closed

Optimize the bytecode for float(0) ? #77863

matrixise opened this issue May 29, 2018 · 5 comments
Labels
3.8 only security fixes

Comments

@matrixise
Copy link
Member

BPO 33682
Nosy @vstinner, @stevendaprano, @matrixise

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:

assignee = None
closed_at = <Date 2018-05-29.13:01:34.963>
created_at = <Date 2018-05-29.12:47:12.095>
labels = ['invalid', '3.8']
title = 'Optimize the bytecode for float(0) ?'
updated_at = <Date 2018-05-29.13:01:34.961>
user = 'https://github.com/matrixise'

bugs.python.org fields:

activity = <Date 2018-05-29.13:01:34.961>
actor = 'matrixise'
assignee = 'none'
closed = True
closed_date = <Date 2018-05-29.13:01:34.963>
closer = 'matrixise'
components = []
creation = <Date 2018-05-29.12:47:12.095>
creator = 'matrixise'
dependencies = []
files = []
hgrepos = []
issue_num = 33682
keywords = []
message_count = 5.0
messages = ['318016', '318017', '318019', '318020', '318021']
nosy_count = 3.0
nosy_names = ['vstinner', 'steven.daprano', 'matrixise']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue33682'
versions = ['Python 3.8']

@matrixise
Copy link
Member Author

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

@matrixise matrixise added the 3.8 only security fixes label May 29, 2018
@matrixise
Copy link
Member Author

Victor, if you confirm, maybe you could help me for a mentoring for this issue?

@vstinner
Copy link
Member

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

@stevendaprano
Copy link
Member

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.

@matrixise
Copy link
Member Author

@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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.8 only security fixes
Projects
None yet
Development

No branches or pull requests

3 participants