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: Constant folding is skipped in named expressions
Type: performance Stage: resolved
Components: Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ncoghlan Nosy List: ncoghlan
Priority: normal Keywords: patch

Created on 2020-11-07 06:59 by ncoghlan, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23190 merged ncoghlan, 2020-11-07 11:09
Messages (3)
msg380494 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2020-11-07 06:59
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
```
msg380505 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2020-11-07 12:35
New changeset 8805a4dad201473599416b2c265802b8885f69b8 by Nick Coghlan in branch 'master':
bpo-42282: Fold constants inside named expressions (GH-23190)
https://github.com/python/cpython/commit/8805a4dad201473599416b2c265802b8885f69b8
msg380506 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2020-11-07 12:36
Since this was only a performance issue, I'm not planning to backport it to earlier releases.
History
Date User Action Args
2022-04-11 14:59:37adminsetgithub: 86448
2021-04-27 17:24:06serhiy.storchakalinkissue43947 superseder
2020-11-07 12:36:30ncoghlansetstatus: open -> closed
resolution: fixed
2020-11-07 12:36:21ncoghlansetmessages: + msg380506
stage: patch review -> resolved
2020-11-07 12:35:36ncoghlansetmessages: + msg380505
2020-11-07 11:09:13ncoghlansetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request22093
2020-11-07 09:49:55ncoghlansetassignee: ncoghlan
2020-11-07 06:59:44ncoghlancreate