Skip to content

Missing some obvious ast optimizations #123080

@wrongnull

Description

@wrongnull

Feature or enhancement

Proposal:

suppose the following code

1 in [1, 2]

it produces such byte code

  0           0 RESUME                   0

  1           2 LOAD_CONST               0 (1)
              4 LOAD_CONST               1 ((1, 2))
              6 CONTAINS_OP              0
              8 RETURN_VALUE

however, interpreter can easily fold such an expression to a constant in the ast_opt stage. This only applies if both ast nodes are Constant_kind. But there is one obstacle. Types are not yet ready and have incomplete mro at the ast_opt stage. So we can't use functions like PyObject_RichCompare for this. We can compare pointers to PyObject instead, since we know that each object in a "constant expression" is internally a singleton (interned strings, integers from -5 to 255, etc.). Thus, for them, the == operator is semantically equal to the is operator. I'm going to make pull request for this

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions