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

Constant folding is skipped in named expressions #86448

Closed
ncoghlan opened this issue Nov 7, 2020 · 3 comments
Closed

Constant folding is skipped in named expressions #86448

ncoghlan opened this issue Nov 7, 2020 · 3 comments
Assignees
Labels
3.10 only security fixes performance Performance or resource usage

Comments

@ncoghlan
Copy link
Contributor

ncoghlan commented Nov 7, 2020

BPO 42282
Nosy @ncoghlan
PRs
  • bpo-42282: Fold constants inside named expressions #23190
  • 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 = 'https://github.com/ncoghlan'
    closed_at = <Date 2020-11-07.12:36:30.609>
    created_at = <Date 2020-11-07.06:59:44.846>
    labels = ['3.10', 'performance']
    title = 'Constant folding is skipped in named expressions'
    updated_at = <Date 2020-11-07.12:36:30.609>
    user = 'https://github.com/ncoghlan'

    bugs.python.org fields:

    activity = <Date 2020-11-07.12:36:30.609>
    actor = 'ncoghlan'
    assignee = 'ncoghlan'
    closed = True
    closed_date = <Date 2020-11-07.12:36:30.609>
    closer = 'ncoghlan'
    components = []
    creation = <Date 2020-11-07.06:59:44.846>
    creator = 'ncoghlan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 42282
    keywords = ['patch']
    message_count = 3.0
    messages = ['380494', '380505', '380506']
    nosy_count = 1.0
    nosy_names = ['ncoghlan']
    pr_nums = ['23190']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue42282'
    versions = ['Python 3.10']

    @ncoghlan
    Copy link
    Contributor Author

    ncoghlan commented Nov 7, 2020

    While working on the PEP-642 reference implementation I removed the "default:" case from the switch statement in astfold_expr as part of making sure the new SkippedBinding node was being handled everywhere it needed to be.

    This change picked up that NamedExpr_kind wasn't being handled either, and a check with the dis module confirmed that using the walrus operator turns off constant folding:

    [ncoghlan@thechalk cpython]$ ./python
    Python 3.10.0a2+ (heads/pep-642-constraint-patterns-dirty:4db2fbd609, Nov  7 2020, 16:42:19) 
    [GCC 10.2.1 20201016 (Red Hat 10.2.1-6)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import dis
    >>> dis.dis("1 + 1")
      1           0 LOAD_CONST               0 (2)
                  2 RETURN_VALUE
    >>> dis.dis("(x := 1 + 1)")
      1           0 LOAD_CONST               0 (1)
                  2 LOAD_CONST               0 (1)
                  4 BINARY_ADD
                  6 DUP_TOP
                  8 STORE_NAME               0 (x)
                 10 RETURN_VALUE
    >>>
    

    The missing switch statement entry is just:

        case NamedExpr_kind:
            CALL(astfold_expr, expr_ty, node_->v.NamedExpr.value);
            break;
    

    Which gives the expected result:

    [ncoghlan@thechalk cpython]$ ./python -c "import dis; dis.dis('(x := 1 +1)')"
      1           0 LOAD_CONST               0 (2)
                  2 DUP_TOP
                  4 STORE_NAME               0 (x)
                  6 RETURN_VALUE
    

    @ncoghlan ncoghlan added 3.10 only security fixes performance Performance or resource usage labels Nov 7, 2020
    @ncoghlan
    Copy link
    Contributor Author

    ncoghlan commented Nov 7, 2020

    New changeset 8805a4d by Nick Coghlan in branch 'master':
    bpo-42282: Fold constants inside named expressions (GH-23190)
    8805a4d

    @ncoghlan
    Copy link
    Contributor Author

    ncoghlan commented Nov 7, 2020

    Since this was only a performance issue, I'm not planning to backport it to earlier releases.

    @ncoghlan ncoghlan closed this as completed Nov 7, 2020
    @ncoghlan ncoghlan closed this as completed Nov 7, 2020
    @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.10 only security fixes performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant