python / cpython

The Python programming language
https://www.python.org
Other
63.37k stars 30.33k forks source link

lambdas stored with assignment expressions are unoptimized #88113

Closed df79943f-4aee-4531-a00d-c6b12816eb70 closed 3 years ago

df79943f-4aee-4531-a00d-c6b12816eb70 commented 3 years ago
BPO 43947
Nosy @serhiy-storchaka, @mr-nfamous, @Fidget-Spinner
Superseder
  • bpo-42282: Constant folding is skipped in named expressions
  • 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: ```python assignee = None closed_at = created_at = labels = ['interpreter-core', '3.8', '3.9', 'performance'] title = 'lambdas stored with assignment expressions are unoptimized' updated_at = user = 'https://github.com/mr-nfamous' ``` bugs.python.org fields: ```python activity = actor = 'serhiy.storchaka' assignee = 'none' closed = True closed_date = closer = 'serhiy.storchaka' components = ['Interpreter Core'] creation = creator = 'bup' dependencies = [] files = [] hgrepos = [] issue_num = 43947 keywords = [] message_count = 3.0 messages = ['392015', '392084', '392108'] nosy_count = 3.0 nosy_names = ['serhiy.storchaka', 'bup', 'kj'] pr_nums = [] priority = 'normal' resolution = 'duplicate' stage = 'resolved' status = 'closed' superseder = '42282' type = 'performance' url = 'https://bugs.python.org/issue43947' versions = ['Python 3.8', 'Python 3.9'] ```

    df79943f-4aee-4531-a00d-c6b12816eb70 commented 3 years ago
    >>> import dis
    >>> @dis.dis
    ... def funcdef(k): return k in {None}                          ...                                                               2           0 LOAD_FAST                0 (k)
                  2 LOAD_CONST               1 (frozenset({None}))                4 COMPARE_OP               6 (in)                               6 RETURN_VALUE
    >>> regass = lambda k: k in {None}
    >>> dis.dis(regass)
      1           0 LOAD_FAST                0 (k)
                  2 LOAD_CONST               1 (frozenset({None}))
                  4 COMPARE_OP               6 (in)
                  6 RETURN_VALUE                                    >>> dis.dis(walrus:=lambda k: k in {None})
      1           0 LOAD_FAST                0 (k)
                  2 LOAD_CONST               0 (None)
                  4 BUILD_SET                1
                  6 COMPARE_OP               6 (in)
                  8 RETURN_VALUE

    As the third example demonstrates, the code for the anonymous function assigned to walrus by an assignment expression isn't receiving the relatively recent frozenset optimization.

    Fidget-Spinner commented 3 years ago

    I'm unable to reproduce it on 3.10:

    >>> regass = lambda k: k in {None}
    >>> dis.dis(regass)
      1           0 LOAD_FAST                0 (k)
                  2 LOAD_CONST               1 (frozenset({None}))
                  4 CONTAINS_OP              0
                  6 RETURN_VALUE
    >>> dis.dis(walrus:=lambda k: k in {None})
      1           0 LOAD_FAST                0 (k)
                  2 LOAD_CONST               1 (frozenset({None}))
                  4 CONTAINS_OP              0
                  6 RETURN_VALUE

    On 3.9 and 3.8, I'm able to reproduce your bug.

    serhiy-storchaka commented 3 years ago

    It was fixed in bpo-42282.